我在matplotlib中具有以下数据和图表:
chart_ts = [datetime(2013, 1, 1, 17, 0),
datetime(2013, 1, 1, 17, 1),
datetime(2013, 1, 1, 17, 2),
datetime(2013, 1, 1, 17, 3),
datetime(2013, 1, 1, 17, 4),
datetime(2013, 1, 1, 17, 5),
datetime(2013, 1, 1, 17, 6),
datetime(2013, 1, 1, 17, 7),
datetime(2013, 1, 1, 17, 8),
datetime(2013, 1, 1, 17, 9)]
chart_close = [1.32034,
1.32046,
1.3205,
1.32025,
1.32025,
1.32026,
1.32034,
1.32035,
1.32035,
1.32063]
mp_close = [1.320060, 1.320055, 1.320050, 1.320045, 1.320040, 1.320035, 1.320030, 1.320025]
mp_value = [5, 6, 8, 10, 3, 5, 6, 2]
%matplotlib inline
mpl.rcParams['interactive'] = False
mpl.rcParams['figure.figsize'] = (8, 8.0)
# plot
plt.plot(chart_ts,chart_close)
plt.gcf().autofmt_xdate()
plt.show()
通过运行上面的代码,我得到了:
到目前为止,很好。
现在,我想在第一个图表上将mp_close和mp_value绘制为水平条。 预期的结果将是这样(它只是一个手绘模型,而不是比例的准确表示):
但是,如果我仅添加新图,则发现用于两个图表的X轴是不同的。我如何获得此结果?
在@importanceofbeingernest评论之后,我已经尝试过:
chart_ts = [datetime(2013, 1, 1, 17, 0),
datetime(2013, 1, 1, 17, 1),
datetime(2013, 1, 1, 17, 2),
datetime(2013, 1, 1, 17, 3),
datetime(2013, 1, 1, 17, 4),
datetime(2013, 1, 1, 17, 5),
datetime(2013, 1, 1, 17, 6),
datetime(2013, 1, 1, 17, 7),
datetime(2013, 1, 1, 17, 8),
datetime(2013, 1, 1, 17, 9)]
chart_close = [1.32034,
1.32046,
1.3205,
1.32025,
1.32025,
1.32026,
1.32034,
1.32035,
1.32035,
1.32063]
mp_close = [1.320060, 1.320055, 1.320050, 1.320045, 1.320040, 1.320035, 1.320030, 1.320025]
mp_value = [5, 6, 8, 10, 3, 5, 6, 2]
%matplotlib inline
mpl.rcParams['interactive'] = False
mpl.rcParams['figure.figsize'] = (8, 8.0)
# plot
fig, ax1 = plt.subplots()
ax1.plot(chart_ts,chart_close)
plt.gcf().autofmt_xdate()
ax2 = ax1.twiny()
ax2.barh(mp_close, mp_value, align='center')
plt.show()
但是我得到了
现在我怀疑我已经快拿到了,但是我会想念的:
对于条的宽度,我已经尝试过:
ax2.barh(mp_close, mp_value, align='center', width=0.00003)
但是我得到了
TypeError: barh() got multiple values for argument 'width'