在 Python def 中使用列名参数未显示 Altair 图

Altair plot doesn't show up using column name parameters in the Python def

这个 Python def 提供了列名,但 Altair 代码似乎没有使用它

def gapsindata( name_of_column ):
    print(outer_join_df )
    
    
     
    print("'" + name_of_column  + ":O'")
    print("'" + name_of_column  + ":N'")
    
    bars =  alt.Chart(outer_join_df).mark_bar().encode(
    alt.Y('PercentMissing:Q'),
    x= "'" + name_of_column  + ":O'",
    color= "'" + name_of_column  + ":N'",
    )

    text = bars.mark_text(
    align='left',
    baseline='middle',
    dx=1  # Nudges text to right so it doesn't appear on top of the bar
    ).encode(
    text='PercentMissing:Q'
    )

    (bars + text).properties(height=200).facet(column= "'" + name_of_column  + ":N'")
 
    
    return;


gapsindata('DfService')

使用通过 Python def 参数输入的列名时,我无法显示 Altair 图。这是输出:

Outer Join DF
   DfService|       FieldName  |MissingItemCnt  |ItemCnt  |PercentMissing
0         No|    Model Number  |             0  |     53  |      0.000000
1         No|        SKU type  |             8  |     53  |     15.094340
2         No|  Equipment Type  |             1  |     53  |      1.886792
3        Yes|    Model Number  |             0  |    204  |      0.000000
4        Yes|        SKU type  |            52  |    204  |     25.490196
5        Yes|  Equipment Type  |             1  |    204  |      0.490196
'DfService:O'
'DfService:N'

提前致谢

您的函数正在 returning None,因此您的笔记本在函数调用后显示 None

类似于这个函数:

def f(x):
  2 * x
  return None

您在调用该函数时不会看到 2 * x 结果,因为该函数未对它执行任何操作。

如果要显示图表,一个好的方法是return图表:

def gapsindata( name_of_column ):
    ...

    return (bars + text).properties(height=200).facet(column= "'" + name_of_column  + ":N'")

gapsindata('DfService')