【问题标题】:How do I overlay different kinds of graph in Seaborn?如何在 Seaborn 中叠加不同类型的图表?
【发布时间】:2018-10-25 11:47:45
【问题描述】:

我正在尝试使用不同的 y 轴将两个图形放入同一个图中,并且当我使用相同类型的图(例如,两个条形图或两个线图)时效果很好。使用此代码

fig, graph = plt.subplots(figsize=(75,3))
sns.lineplot(x='YearBuilt',y='SalePrice',ax=graph,data=processed_data,color='red')
graph2 = graph.twinx()
sns.lineplot(x='YearBuilt', y='AvgOverallQual',ax=graph2,data=processed_data,color='teal')

我得到了这个

但是当我尝试使用不同的种类时,像这样:

fig, graph = plt.subplots(figsize=(75,3))
sns.barplot(x='YearBuilt',y='SalePrice',ax=graph,data=processed_data,color='red')
graph2 = graph.twinx()
sns.lineplot(x='YearBuilt', y='AvgOverallQual',ax=graph2,data=processed_data,color='teal')

我的图表如下所示:

如何在 Seaborn 中叠加不同类型的图表?

【问题讨论】:

    标签: python plot graph overlay seaborn


    【解决方案1】:

    seaborn barplot 是一个分类图。第一个条将位于位置 0,第二个位于位置 1,依此类推。lineplot 是一个数字图;它会将所有点放在数字坐标给定的位置。

    这里,似乎根本不需要使用seaborn。由于 matplotlib bar 绘图也是数字的,因此仅在 matplotlib 中执行此操作将为您提供所需的叠加层

    fig, ax = plt.subplots(figsize=(75,3))
    ax.bar('YearBuilt','SalePrice', data=processed_data, color='red')
    ax2 = ax.twinx()
    ax2.plot('YearBuilt', 'AvgOverallQual', data=processed_data, color='teal')
    

    【讨论】:

    • 是的,我已经在 matplotlib 中做过,我想它可以很好地达到这个目的。但我一直在努力理解为什么它在 seaborn 中不起作用,现在它更有意义了。您的回答很有帮助,谢谢!
    猜你喜欢
    • 2021-08-10
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 2021-12-17
    • 2020-08-29
    相关资源
    最近更新 更多