简体   繁体   English

如何从两个或多个数据帧 plot 分组条 plot

[英]How to plot a grouped bar plot from two or more dataframes

I have multiple dataframes, and I want to plot them on the same figure in the Grouped Bar Chart view.我有多个数据框,我想 plot 将它们放在分组条形图视图中的同一数字上。

These are two very small dataframes that I would like to plot together in the same figure.这是两个非常小的数据帧,我想在同一个图中将 plot 放在一起。

The dataframes are:数据框是:

我的数据框

I want to plot a figure like this example:我想 plot 一个像这样的例子:

我想要的例子

I try this, plot only one graph:我试试这个,plot 只有一张图:

fig, ax = plt.subplots()

df1.plot.bar(x='Zona',y='Total_MSP')
df4.plot.bar(x='Zona',y='NumEstCasasFavelas2017',ax=ax)

plt.show()

I tried this too:我也试过这个:

fig, ax = plt.subplots()

df1.plot.bar(x='Zona',y='Total_MSP',ax=ax)
df4.plot.bar(x='Zona',y='NumEstCasasFavelas2017',ax=ax)

plt.show()

The results are just data from a single dataframe in a picture, not two data from both dataframes.结果只是来自图片中单个 dataframe 的数据,而不是来自两个数据帧的两个数据。 Note that only the captions of both dataframes appear in the same picture, the data is only from a single isolated dataframe.请注意,只有两个数据帧的标题出现在同一张图片中,数据仅来自单个隔离的 dataframe。

我的失败

Data:数据:

import pandas as pd
import matplotlib.pyplot as plt

df1 = pd.DataFrame({'Zone': ['C', 'L', 'N', 'O', 'S'],
                    'Total_MSP': [464245, 3764942, 1877505, 1023160, 3179477]})
df2 = pd.DataFrame({'Zone': ['C', 'L', 'N', 'O', 'S'],
                    'CasasFavelas_2017': [463, 4228, 851, 1802, 2060]}) 

Merge the dataframes:合并数据框:

  • Using pandas.merge , combine the DataFrames.使用pandas.merge ,合并数据帧。
df = pd.merge(df1, df2, on='Zone')

  Zone  Total_MSP  CasasFavelas_2017
0    C     464245                463
1    L    3764942               4228
2    N    1877505                851
3    O    1023160               1802
4    S    3179477               2060

Plot: Plot:

df.plot.bar(x='Zone', logy=True)
plt.xticks(rotation=0)
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.show()

在此处输入图像描述

Update:更新:

  • The OP added additional data in an answer, after this answer was provided.提供此答案后,OP 在答案中添加了其他数据。
  • Use pandas.concat to combine more than 2 DataFrames.使用pandas.concat组合超过 2 个 DataFrame。
df12 = pd.DataFrame({'Zone': ['C', 'L', 'N', 'O', 'S'], 'Total_MSP': [464245, 3764942, 1877505, 1023160, 3179477]})
df13 = pd.DataFrame({'Zone': ['C', 'L', 'N', 'O', 'S'], 'ValorMedioDollar': [1852.27, 1291.53, 1603.44, 2095.90, 1990.10]})
df14 = pd.DataFrame({'Zone': ['C', 'L', 'N', 'O', 'S'], 'IDH2010': [0.89, 0.70, 0.79, 0.90, 0.80]})
df15 = pd.DataFrame({'Zone': ['C', 'L', 'N', 'O', 'S'], 'QtdNovasCasas': [96,1387, 561, 281, 416]})

# use concat to combine more than two DataFrames
df = pd.concat([df12.set_index('Zone'), df13.set_index('Zone'), df14.set_index('Zone'), df15.set_index('Zone')], axis=1)

      Total_MSP  ValorMedioDollar  IDH2010  QtdNovasCasas
Zone                                                     
C        464245           1852.27     0.89             96
L       3764942           1291.53     0.70           1387
N       1877505           1603.44     0.79            561
O       1023160           2095.90     0.90            281
S       3179477           1990.10     0.80            416

# plot the DataFrame
df.plot.bar(logy=True, figsize=(8, 6))
plt.xticks(rotation=0)
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.show()

在此处输入图像描述

Adding Annotations:添加注释:

  • Not part of the original question.不是原始问题的一部分。
  1. How to plot and annotate a grouped bar chart with 3 bars in each group?如何 plot 并注释分组条形图,每组有 3 个条形图?
  2. How to plot a dictionary如何 plot 字典

Graphic with four custom color dataframes and caption具有四个自定义颜色数据框和标题的图形

import pandas as pd


df12 = pd.DataFrame({'Zone': ['C', 'L', 'N', 'O', 'S'],
                    'Total_MSP': [464245, 3764942, 1877505, 1023160, 3179477]})
df13 = pd.DataFrame({'Zone': ['C', 'L', 'N', 'O', 'S'],
                    'ValorMedioDollar': [1852.27, 1291.53, 1603.44, 2095.90, 1990.10]})
df14 = pd.DataFrame({'Zone': ['C', 'L', 'N', 'O', 'S'],
                    'IDH2010': [0.89, 0.70, 0.79, 0.90, 0.80]})
df15 = pd.DataFrame({'Zone': ['C', 'L', 'N', 'O', 'S'],
                    'QtdNovasCasas': [96,1387, 561, 281, 416]})


df16 = pd.merge(df12, df13, on='Zone')
df16 = pd.merge(df16, df14, on='Zone')
df16 = pd.merge(df16, df15, on='Zone')

fig, ax = plt.subplots(figsize=(50, 20))

#https://xkcd.com/color/rgb/
colors2 = ['#448ee4', '#a9f971','#ceb301','#ffb7ce']


#For all values to be displayed, even though these scales are different, the log scale is used.
df16.plot.bar(x='Zone', logy=True, color=colors2, ax=ax,width=0.5, align = 'center'); 


#legend
#https://stackoverflow.com/questions/19125722/adding-a-legend-to-pyplot-in-matplotlib-in-the-most-simple-manner-possible
plt.gca().legend(('Total Resident Population-2017', 
                  'Median Value of square meter-Dollars US', 
                  'HDI- Human Development Index-2010',
                  'Number of new housing properties-2018'),bbox_to_anchor=(0.87, 0.89) ,fontsize=28)


plt.title('Estimated Resident Population, Average value of square meter, HDI, New housing properties in São Paulo - Brazil',fontsize=40)
plt.xlabel ('Names of the geographical subdivisions of São Paulo',fontsize=40)
plt.ylabel('Log Scale', fontsize=30)

#change the name of month on the x 
ax = plt.gca()
names = ['Zone: Center', 'Zone: East', 'Zone: North', 'Zone: West', 'Zone: South']
ax.set_xticklabels(names,fontsize=40)
x = plt.gca().xaxis



plt.rcParams['ytick.labelsize'] = 30

# rotate the tick labels for the x axis
for item in x.get_ticklabels():
    item.set_rotation(0)    

for spine in plt.gca().spines.values():
    spine.set_visible(False)

# remove all the ticks (both axes), and tick labels on the Y axis
plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='on', labelbottom='on')



# direct label each bar with Y axis values
for p in ax.patches[0:]:
    plt.gca().text(p.get_x() + p.get_width()/2, p.get_height()+0.01, str(float(p.get_height())), 
                 ha='center', va='baseline', rotation=0 ,color='black', fontsize=25)



plt.show()


fig.savefig('GraficoMultiplo.jpg')

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

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