在同一个图表上将Pandas DataFrame绘制为条形和线条 [英] Plot Pandas DataFrame as Bar and Line on the same one chart
问题描述
我试图绘制一个图表,其中第一列和第二列数据为条形,然后是第三列数据的行重叠。
我试过下面的代码,但这会创建2个单独的图表,但我想这一切在一个图表上。
left_2013 = pd.DataFrame 'month':['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov' dec'],
'2013_val':[1,2,3,4,5,6,7,8,9,10,9,6]})
right_2014 = pd .DataFrame({'month':['jan','feb'],'2014_val':[4,5]})
right_2014_target = pd.DataFrame({'month':[' jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'],
'2014_target_val':[2,3,4,5,6,7,8,9,10,11,12,13]})
df_13_14 = pd.merge (left_2013,right_2014,how ='outer')
df_13_14_target = pd.merge(df_13_14,right_2014_target,how ='outer')
df_13_14_target [['month','2013_val','2014_val' '2014_target_val']]。head(12)
plt.figure()
df_13_14_target [['month','2014_target_val']] ' - ',marker ='o')
df_13_14_target [['month','2013_val','2014_val']] plot(x ='month',kind ='bar')
这是我目前取得的
解决方案DataFrame绘制方法返回一个matplotlib
AxesSubplot
或AxesSubplots
的列表。 (请参见图表的文档< a>或 boxplot
然后,您可以将相同的轴传递给下一个绘图方法(使用
ax = ax
)绘制在同一个轴上:ax = df_13_14_target [['month','2014_target_val']] x ='month',linestyle =' - ',marker ='o')
df_13_14_target [['month','2013_val','2014_val']] bar',
ax = ax)
import pandas as pd
import matplotlib.pyplot as plt
left_2013 = pd.DataFrame(
{'month':['jan ','feb','mar','apr','may','jun','jul','aug','sep',
'oct','nov','dec'] ,
'2013_val':[1,2,3,4,5,6,7,8,9,10,9,6]})
right_2014 = pd.DataFrame {'month':['jan','feb'],'2014_val':[4,5]})
right_2014_target = pd.DataFrame(
{'month':[ 'jan','feb','mar','apr','may','jun','jul','aug','sep',
'oct','nov','dec '],
'2014_target_val':[2,3,4,5,6,7,8,9,10,11,12,13]})
df_13_14 = pd。合并(left_2013,right_2014,how ='outer')
df_13_14_target = pd.merge(df_13_14,right_2014_target,how ='outer')
ax = df_13_14_target [ 2014_target_val']]。plot(
x ='month',linestyle =' - ',marker ='o')
df_13_14_target [['month','2013_val','2014_val']] (x ='month',kind ='bar',
ax = ax)
plt.show()
I am trying to plot a chart with the 1st and 2nd columns of data as bars and then a line overlay for the 3rd column of data.
I have tried the following code but this creates 2 separate charts but I would like this all on one chart.
left_2013 = pd.DataFrame({'month': ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'], '2013_val': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 6]}) right_2014 = pd.DataFrame({'month': ['jan', 'feb'], '2014_val': [4, 5]}) right_2014_target = pd.DataFrame({'month': ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'], '2014_target_val': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]}) df_13_14 = pd.merge(left_2013, right_2014, how='outer') df_13_14_target = pd.merge(df_13_14, right_2014_target, how='outer') df_13_14_target[['month','2013_val','2014_val','2014_target_val']].head(12) plt.figure() df_13_14_target[['month','2014_target_val']].plot(x='month',linestyle='-', marker='o') df_13_14_target[['month','2013_val','2014_val']].plot(x='month', kind='bar')
This is what I currently get
解决方案The DataFrame plotting methods return a matplotlib
AxesSubplot
or list ofAxesSubplots
. (See the docs for plot, or boxplot, for instance.)You can then pass that same Axes to the next plotting method (using
ax=ax
) to draw on the same axes:ax = df_13_14_target[['month','2014_target_val']].plot(x='month',linestyle='-', marker='o') df_13_14_target[['month','2013_val','2014_val']].plot(x='month', kind='bar', ax=ax)
import pandas as pd import matplotlib.pyplot as plt left_2013 = pd.DataFrame( {'month': ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'], '2013_val': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 6]}) right_2014 = pd.DataFrame({'month': ['jan', 'feb'], '2014_val': [4, 5]}) right_2014_target = pd.DataFrame( {'month': ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'], '2014_target_val': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]}) df_13_14 = pd.merge(left_2013, right_2014, how='outer') df_13_14_target = pd.merge(df_13_14, right_2014_target, how='outer') ax = df_13_14_target[['month', '2014_target_val']].plot( x='month', linestyle='-', marker='o') df_13_14_target[['month', '2013_val', '2014_val']].plot(x='month', kind='bar', ax=ax) plt.show()
这篇关于在同一个图表上将Pandas DataFrame绘制为条形和线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!