为什么在 Spyder 上使用 Altair 时我的两个图表都没有显示?

Why are both of my graphs not showing when using Altair on Spyder?

两个图表的代码。 运行 这段代码重复了几次,出于某种原因,所有显示的都是直方图。同样重要的是要提到我也在使用 Spyder IDE,如果这有什么不同的话。哦....我也试过 graph|hist... 但什么都没有

    import altair as alt
    import pandas as pd
   
    #import csv
    acs = pd.read_csv('C:/Users/Andrew/anaconda3/mydata/acs2020.csv')
    acs.head()
    
    
    interval = alt.selection_interval()

    #build point graph
    graph = alt.Chart(acs).mark_point(opacity=1).encode(
        x = ('Trade #'),
        y = ('Balance'),
        color = alt.Color('Item',scale=alt.Scale(scheme='tableau10')),
        tooltip = [alt.Tooltip('Type'),
                   alt.Tooltip('Profit'),
                   alt.Tooltip('Ticket:N')
                  ]
    ).properties(
        width = 900
    )

    #build histogram
    hist = alt.Chart(acs).mark_bar().encode(
        x = 'count()',
        y = 'Item',
        color = 'Item'
    ).properties(
        width = 800,
        height = 80
    ).add_selection(
        interval
    )
    #show graphs
    graph&hist.show()

您需要在最后一行添加括号:(graph & hist).show().


解释:当你写类似

的东西时
graph&hist.show()

Python 规定了运算顺序,方法调用的优先级高于二元运算符。这意味着它等同于:

(graph) & (hist.show())

您正在显示 hist 图表,然后将 show() 方法的 return 值(即 None)连接到 graph图表,当关闭直方图图表时将导致 ValueError

相反,如果要显示graph & hist操作的结果,则需要使用括号来表示:

(graph & hist).show()