如何从两个或多个数据框中绘制分组条形图

Posted

技术标签:

【中文标题】如何从两个或多个数据框中绘制分组条形图【英文标题】:How to plot a grouped bar plot from two or more dataframes 【发布时间】:2020-01-28 03:56:44 【问题描述】:

我有多个数据框,我想将它们绘制在分组条形图视图中的同一图上。

这是两个非常小的数据框,我想将它们一起绘制在同一个图中。

数据框是:

我想绘制一个像这样的例子:

我试试这个,只画一张图:

fig, ax = plt.subplots()

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

plt.show()

我也试过这个:

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()

结果只是图片中单个数据帧的数据,而不是两个数据帧中的两个数据。请注意,只有两个数据帧的标题出现在同一张图片中,数据仅来自单个孤立的数据帧。

【问题讨论】:

【参考方案1】: 要创建分组条形图,必须将 DataFrame 与 pandas.mergepandas.DataFrame.merge 结合使用。 见pandas User Guide: Merge, join, concatenate and compare和SO: Pandas Merging 101。

数据:

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]) 

合并数据框:

使用pandas.merge,合并DataFrame。
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

剧情:

pandas.DataFrame.plot 绘制DataFrame。 使用对数刻度显示Casas
df.plot.bar(x='Zone', logy=True)
plt.xticks(rotation=0)
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.show()

更新:

在提供此答案后,OP 在答案中添加了其他数据。 使用pandas.concat 组合两个以上的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()

添加注释:

不属于原始问题。
    How to plot and annotate a grouped bar chart with 3 bars in each group? How to plot a dictionary

【讨论】:

【参考方案2】:

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://***.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')

【讨论】:

以上是关于如何从两个或多个数据框中绘制分组条形图的主要内容,如果未能解决你的问题,请参考以下文章

如何从ggplot2中的两个不同的二进制值列绘制百分比堆积条形图?

在多列上分组时如何绘制条形图?

使用两个数据框绘制并排条形图

如何在条形图的每个条形顶部添加一个额外的数字

如何使熊猫分类堆积条形图比例为 100%

Pandas:如何绘制一个有两个类别和四个系列的条形图?