【问题标题】:How to create an Area plot in Seaborn?如何在 Seaborn 中创建区域图?
【发布时间】:2019-03-18 03:06:28
【问题描述】:

有什么方法可以在 Seaborn 中创建区域图。我检查了文档,但找不到。

这是我要绘制的数据。

year_countries.head()
Out[19]: 
state_code   China  France  Japan  Russia  United States
launch_year                                             
1957             0       0      0       2              1
1958             0       0      0       5             22
1959             0       0      0       4             18
1960             0       0      0       8             29
1961             0       0      0       9             41

我使用此代码创建了一个线图 -

sns.relplot(data=year_countries, kind='line',
            height=7, aspect=1.3,linestyle='solid')

plt.xlabel('Lanuch Year', fontsize=15)
plt.ylabel('Number of Launches', fontsize=15)
plt.title('Space Launches By Country',fontsize=17)

plt.show()

但使用折线图时,绘图不是很清楚 - Plot fig

也无法将线条设为实线并根据值按降序对图例进行排序。

谁能帮我解决这个问题?

【问题讨论】:

    标签: python pandas seaborn


    【解决方案1】:

    使用带有 Seaborn 风格的 Pandas areaplot 怎么样?

    另外,你需要自己对图例进行排序,我从here获取了示例代码。

    plt.style.use('seaborn')
    year_countries.plot.area()
    plt.xlabel('Launch Year', fontsize=15)
    plt.ylabel('Number of Launches', fontsize=15)
    plt.title('Space Launches By Country',fontsize=17)
    
    ax = plt.gca()
    handles, labels = ax.get_legend_handles_labels()
    labels, handles = zip(*sorted(zip(labels, handles), key=lambda t: t[0], reverse=True))
    ax.legend(handles, labels)
    plt.show()
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多