繁体   English   中英

如何从各种数据框创建子图

[英]How to create subplot from various data frames

我有一个数据框列表:

list_df = [df1, df2, df3, df4, df5, df6]

其中由完全相同的命令定义:

df6 = df_2016['Incident Type Description'].value_counts()[:20]

其中df1 = df_2011['Incident Type Description'].value_counts()[:20] , df2 = df_2012['Incident Type Description'].value_counts()[:20]以此类推df6 = df_2016['Incident Type Description'].value_counts()[:20] df_2011, df_2012...df_2016是我上传的 csv 文件中的命名变量。

我想要做的是加入所有这些以创建一个 2x3 子图,而不是让它们单独显示,我使用以下命令: df_2016['Incident Type Description'].value_counts()[:20].plot(kind='barh', title='Top 20 crimes in Oakland in 2016')

到目前为止,我已经尝试了一些在stackoverflow上找到的方法[ 我如何将 plot 将 Pandas DataFrames 分离为子图? 和 matplotlib 页面,但这些不起作用,我不知道为什么(我是 python 的初学者):

axes= plt.subplots(2, 3)
list_df = [df1, df2, df3, df4, df5, df6]

columns = [f'Top 20 crimes in {x}' for x in range(2011, 2017)]

list_df.plot.barh(subplots=True, layout=(2, 3), figsize=(15,7))

到目前为止ValueError出现为ValueError: Layout of 2x3 must be larger than required size 7 有任何想法吗?

我不知道您是否打算使用另一个 package。 但是 Seaborn 经常被用来很好和快速地显示不同类型的图。 您可以查看完整的 package 文档: Seaborn 文档 有一些不错的教程。

尤其是 FacetGrid 来显示 suplots。 FacetGrid 文档

Seaborn 建立在 matplot lib 之上。

编辑:您可能需要 append 所有您的 df,然后根据指定输入 df(1-6) 的列进行子图。

你可以试试:

fig, axes = plt.subplots(2,3)

for d, ax in zip(list_df, axes.ravel()):
    d.plot.barh(ax=ax)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM