繁体   English   中英

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

[英]How to set x and y axis columns in python subplot matplotlib

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,.... 这样的索引。

但是我的 dataframe 有一列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,... 这样的索引

但是我的 dataframe 有一列 self.signals['DATA'] 那么我怎样才能将它用作 x 轴值呢?

我假设您使用的是 pandas 和 matplotlib。

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

所以不要打电话

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

例如你可以这样做:

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

根据其他 arguments,这将执行一个散点图 plot 或一条线 plot。请参阅 matplotlib 文档以更精细地调整您的图表。

或者你甚至可以尝试:

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

引用文档:

调用签名:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM