孪生线图中如何在y轴上移动?

时间:2019-07-03 12:41:43

标签: python matplotlib plot time-series data-visualization

我已经使用ax.twinx()绘制了时间序列数据,但是现在线图与主干图不对齐。

我尝试实现plt.tight_layoutfig.tight_layout,但是没有用。

我的数据看起来像这样

Date        Day_Perc_Change No. of Trades
2017-05-15  0.000000        40593
2017-05-16  -0.019184       22027
2017-05-17  -0.008550       15709
2017-05-18  -0.017699       47159
2017-05-19  -0.021748       85921
import numpy as np
import matplotlib.pyplot as plt

date_name = "Date"
stem_name = "Day_Perc_Change"
plot_name = "No. of Trades"
t = df[date_name]
data1 = df[stem_name]
data2 = df[plot_name]

fig = plt.figure(figsize=(15,6))
ax1 = plt.subplot(1,1,1)
plt.subplots_adjust()

color = 'tab:blue'
ax1.set_xlabel(date_name)
ax1.set_ylabel(stem_name, color=color, size='x-large')
ax1.stem(t, data1)
ax1.tick_params(axis='x', labelcolor=color)

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

color = 'tab:blue'
ax2.set_ylabel(plot_name, color=color, size='x-large')  # we already handled the x-label with ax1
ax2.plot(t, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)

plt.show()
plt.tight_layout()  # otherwise the right y-label is slightly clipped

enter image description here 我希望线图移至ylabel的'0'(零)线。

0 个答案:

没有答案