【发布时间】:2015-07-10 04:07:11
【问题描述】:
我有一个工作代码,可以在图表中将熊猫数据框显示为 2 个折线图。我还有一个数据框,可以在同一张图表上显示条形图。对于 2 个数据框,我有 x 轴的日期。因为这两个数据框都有日期,所以我的轴最终只有整数 (1,2,3,4,5,6...) 而不是日期。
我认为这条线 df1 = df.set_index(['date']) 已经指定了我想要的 x 轴,当我不在图表上绘制条形图时,日期显示得很好,但是当我绘制条形图时,整数显示而是在轴上。
我的 2 个数据框:
df1:
date line1 line2
2015-01-01 15.00 23.00
2015-02-01 18.00 10.00
df2:
date quant
2015-01-01 500
2015-02-01 600
我的代码:
df1 =pd.DataFrame(result, columns =[ 'date','line1', 'line2'])
df1 = df.set_index(['date'])
df2 =pd.DataFrame(quantity, columns =[ 'quant','date'])
fig = plt.figure()
ax = fig.add_subplot(111)
ax2=ax.twinx()
ax.set_ylim(0,100)
ax2.set_ylim(0,2100)
df1.line1.plot( color = 'red', ax = ax)
df1.line2.plot( color = 'blue', ax = ax)
df2.["quant"].plot(kind = 'bar', ax =ax2, width =0.4)
plt.show()
df1:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 12 entries, 0 to 11
Data columns (total 3 columns):
date 12 non-null object
line1 12 non-null float64
line2 12 non-null float64
dtypes: float64(2), object(1)
memory usage: 384.0+ bytes
None
df2
<class 'pandas.core.frame.DataFrame'>
Int64Index: 11 entries, 0 to 10
Data columns (total 2 columns):
quant 11 non-null int64
date 11 non-null object
dtypes: int64(1), object(1)
memory usage: 264.0+ bytes
None
【问题讨论】:
标签: python pandas matplotlib plot dataframe