如何在matplotlib pandas 中的一个图中组合两个文件的两个条形图 [英] how to combine two bar chart of two files in one diagram in matplotlib pandas

查看:86
本文介绍了如何在matplotlib pandas 中的一个图中组合两个文件的两个条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有相同列但内容不同的数据框。
我已经绘制了数字数据框。现在我想在同一张图上绘制另一个数据框 dffinal_no 以便进行比较。

I have two dataframe with the same columns but different content. I have plotted dffinal data frame. now I want to plot another dataframe dffinal_no on the same diagram to be comparable.

例如,一个条形图蓝色,而同一条形图和另一种颜色只是在y轴上有所区别

for example one bar chart in blue colour, and the same bar chart with another colour just differentiating in y-axis.

这是我绘制第一个数据框的代码的一部分。

This is part of the code in which I have plotted the first data frame.

dffinal = df[['6month','final-formula','numPatients6month']].drop_duplicates().sort_values(['6month'])

ax=dffinal.plot(kind='bar',x='6month', y='final-formula')
import matplotlib.pyplot as plt
ax2 = ax.twinx()
dffinal.plot(ax=ax2,x='6month', y='numPatients6month')
plt.show()

现在想象我还有另一个 dffinal_no 数据框,它们具有相同的列,如何在同一图中绘制它?

Now imagine I have another dffinal_no data frame with the same columns, how can I plot it in the same diagram?

这是我绘制的第一个图,我希望该图上的其他条形图具有另一种颜色。

This is my first diagram which I plotted, I want the other bar chart on this diagram with another color.

所以@Mohamed Thasin ah的答案是我想要的,除了正确的y轴不正确。

so the answer of @Mohamed Thasin ah is somehow what I want, except that the right y-axis is not correct.

我希望两个数据框基于(6个月,最终公式),但是右 y轴只是显示患者数量,作为用户的信息。

I want both data frame be based on (6month, final-formula) but the right y-axis is just showing number of patients, as an information for the user.

实际上,我不要希望基于 fin-fomula 的第一个df和基于 NumberPatients的第二个df

Actually, I DO NOT want the first df based on final-fomula and the second df be based on NumberPatients.

Update1 jast作为参考,就像我的数据框一样

dffinal = df[['6month','final-formula','numPatients6month']].drop_duplicates().sort_values(['6month'])
nocidffinal = nocidf[['6month','final-formula','numPatients6month']].drop_duplicates().sort_values(['6month'])
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.set_ylabel('final-formula')
ax2.set_ylabel('numPatients6month')
width=0.4
nocidffinal=nocidffinal.set_index('6month').sort_index()
dffinal=dffinal.set_index('6month').sort_index()
nocidffinal['final-formula'].plot(kind='bar',color='green',ax=ax1,width=width,position=0)
dffinal['numPatients6month'].plot(kind='bar',color='red',ax=ax2,width=width,position=1)

dffinal content

,6month,final-formula,numPatients6month
166047.0,1,7.794117647058823,680
82972.0,2,5.720823798627003,437
107227.0,3,5.734767025089606,558
111330.0,4,4.838709677419355,434
95591.0,5,3.3707865168539324,534
95809.0,6,3.611738148984198,443
98662.0,7,3.5523978685612785,563
192668.0,8,2.9978586723768736,467
89460.0,9,0.9708737864077669,515
192585.0,10,2.1653543307086616,508
184325.0,11,1.727447216890595,521
85068.0,12,1.0438413361169103,479

nocidffinal

,6month,final-formula,numPatients6month
137797.0,1,3.5934291581108826,974
267492.0,2,2.1705426356589146,645
269542.0,3,2.2106631989596877,769
271950.0,4,2.0,650
276638.0,5,1.5587529976019185,834
187719.0,6,1.9461077844311379,668
218512.0,7,1.1406844106463878,789
199830.0,8,0.8862629246676514,677
269469.0,9,0.3807106598984772,788
293390.0,10,0.9668508287292817,724
254783.0,11,1.2195121951219512,738
300974.0,12,0.9695290858725761,722


推荐答案

用条形图比较两个数据框结果可以尝试的是连接两个数据帧并添加 hue
例如,考虑在df下,它在df中包含相同的x和y列,并希望比较此值。要实现此目的,只需为每个df添加色相列(如下所示)即可。

to compare two data frame result with bar plot one way you could try is concatenating two data frames and adding hue. For example consider below df it contains same x and y columns in both df's and wanna compare this values. to achieve this simply add hue column for each df with differentiating constant like below.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df1=pd.DataFrame({'x':[1,2,3,4,5],'y':[10,2,454,121,34]})
df2=pd.DataFrame({'x':[4,1,2,5,3],'y':[54,12,65,12,8]})
df1['hue']=1
df2['hue']=2
res=pd.concat([df1,df2])
sns.barplot(x='x',y='y',data=res,hue='hue')
plt.show()

结果应如下所示:

要获得两个y轴,请尝试此方法,

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()

ax1.set_ylabel('final-formula')
ax2.set_ylabel('numPatients6month')

width=0.4

df1=df1.set_index('x').sort_index()
df2=df2.set_index('x').sort_index()

df1['y'].plot(kind='bar',color='blue',ax=ax1,width=width,position=1)
df2['y'].plot(kind='bar',color='green',ax=ax2,width=width,position=0)
plt.show()

实际输入:

fig = plt.figure()

ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()

ax1.set_ylabel('final-formula')
ax2.set_ylabel('numPatients6month')


width=0.4

df1=df1.set_index('6month').sort_index()
df2=df2.set_index('6month').sort_index()

df1['final-formula'].plot(kind='bar',color='blue',ax=ax1,width=width,position=1)
df2['numPatients6month'].plot(kind='bar',color='green',ax=ax2,width=width,position=0)
plt.show()

这篇关于如何在matplotlib pandas 中的一个图中组合两个文件的两个条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆