简体   繁体   English

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

[英]How to create subplot from various data frames

I have a list of data frames:我有一个数据框列表:

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

of which are are defined by fairly identical commands:其中由完全相同的命令定义:

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

Where df1 = df_2011['Incident Type Description'].value_counts()[:20] , df2 = df_2012['Incident Type Description'].value_counts()[:20] and so on to 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 are named variables from csv files I uploaded. df_2011, df_2012...df_2016是我上传的 csv 文件中的命名变量。

What I wish to do is join all these to create a 2x3 subplot instead of having them individually displayed of which I had with the command: df_2016['Incident Type Description'].value_counts()[:20].plot(kind='barh', title='Top 20 crimes in Oakland in 2016')我想要做的是加入所有这些以创建一个 2x3 子图,而不是让它们单独显示,我使用以下命令: df_2016['Incident Type Description'].value_counts()[:20].plot(kind='barh', title='Top 20 crimes in Oakland in 2016')

I have tried a few approaches so far that I have found on stackoverflow [ How can I plot separate Pandas DataFrames as subplots?到目前为止,我已经尝试了一些在stackoverflow上找到的方法[ 我如何将 plot 将 Pandas DataFrames 分离为子图? and the matplotlib page, but these do not work and I don't know why (I'm a beginner in python):和 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))

So far ValueError comes up as ValueError: Layout of 2x3 must be larger than required size 7 .到目前为止ValueError出现为ValueError: Layout of 2x3 must be larger than required size 7 Any ideas?有任何想法吗?

I don't know if you planned to use another package.我不知道您是否打算使用另一个 package。 But Seaborn is quite often used to display nicely and quickly different kinds of plots.但是 Seaborn 经常被用来很好和快速地显示不同类型的图。 You can check the complete package doc: Seaborn Doc .您可以查看完整的 package 文档: Seaborn 文档 There are some nice tutorials.有一些不错的教程。

Especially FacetGrid to display suplots.尤其是 FacetGrid 来显示 suplots。 FacetGrid Doc FacetGrid 文档

Seaborn is built on top of matplot lib. Seaborn 建立在 matplot lib 之上。

Edit: You could maybe need to append all you df, and then subplot based on a column specifying the input df(1-6).编辑:您可能需要 append 所有您的 df,然后根据指定输入 df(1-6) 的列进行子图。

You can try:你可以试试:

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