【问题标题】:How can I draw a bar graph from three different data frames using matplotlib?如何使用 matplotlib 从三个不同的数据框中绘制条形图?
【发布时间】:2019-04-06 16:59:25
【问题描述】:

我想使用 matplotLib 绘制一个包含三个不同数据框的三重条形图

DF1

index | Number    
A     | 110    
B     | 22    
D     | 52

DF2

index | Number
A     | 100
B     | 22
C     | 52

DF3

index | Number    
A     | 90    
B     | 12    
C     | 10

我正在尝试使用这段代码,但这会给出错误,因为此数据需要来自同一数据帧

ax=DF1[["Number"]].DF2[["Number"]].DF3[["Number"]].plot(kind ='bar',log=True,title = "BarGraph",figsize=(15,10),legend=True,fontsize=10)
ax.set_xlabel("Index",fontsize=12)
ax.set_ylabel("Number",fontsize=12)
plt.show()

【问题讨论】:

  • 为什么不使用 Seaborn?
  • 好的,你能指导我如何使用 seaborn 完成这件事

标签: python visualization matplotlib


【解决方案1】:

如果你愿意使用 seaborn,这个代码块适合你:

import pandas as pd
import seaborn as sns

DF1 = pd.DataFrame({'index': ['A', 'B', 'C'], 'Number': [110, 22, 52]})
DF2 = pd.DataFrame({'index': ['A', 'B', 'C'], 'Number': [100, 22, 52]})
DF3 = pd.DataFrame({'index': ['A', 'B', 'C'], 'Number': [90, 12, 10]})

DF = pd.concat([DF1, DF2, DF3])
DF['df_num'] = ['1','1','1','2','2','2','3','3','3']

sns.barplot(data = DF, x='index', y='Number', hue='df_num')

它应该产生这个情节:

【讨论】:

    猜你喜欢
    • 2019-05-24
    • 2021-02-12
    • 2020-11-30
    • 1970-01-01
    • 2023-04-05
    • 2013-03-02
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    相关资源
    最近更新 更多