如何在python subplot matplotlib中设置x和y轴列


def plot(self):
plt.figure(figsize=(20, 5))
ax1 = plt.subplot(211)
ax1.plot(self.signals['CLOSE'])
ax1.set_title('Price')
ax2 = plt.subplot(212, sharex=ax1)
ax2.set_title('RSI')
ax2.plot(self.signals[['RSI']])
ax2.axhline(30, linestyle='--', alpha=0.5, color='#ff0000')
ax2.axhline(70, linestyle='--', alpha=0.5, color='#ff0000')

plt.show()

我在python应用程序中绘制两个图表。但是x轴值是索引,比如1 2 3 ....

但是我的数据框有一个列self.signals['DATA'],所以我怎么能使用它作为x轴值?

您设置的set_xticks位置,例如:

ax1.set_xticks([1, 2, 3])

使用set_xticklabels你可以定义它的内容:

ax1.set_xticklabels(['one', 'two', 'three'])

更多选项见https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.set_xticklabels.html

但x轴值是索引,如1,2,3,…

但是我的数据框架有一个列self。信号['DATA']所以我怎么能用它作为x轴值?

我假设您正在使用pandas和matplotlib。

根据matplotlib文档,您可以简单地将X和Y值传递给plot函数。

所以不调用

ax1.plot(self.signals['CLOSE'])

你可以这样做:

ax1.plot(self.signals['DATA'], self.signals['CLOSE'])

根据其他参数,这将绘制散点图或线形图。查看matplotlib文档了解更多图表的微调。

或者你甚至可以试试:

plot('DATA', 'CLOSE', data=self.signals)

引用文档:

调用签名:

plot([x], y, [fmt], *, data=None, **kwargs)

plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)

最新更新