简体   繁体   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()

I am plotting two charts in python application.我在 python 应用程序中绘制了两个图表。 But the x axis values are indexes like 1,2,3,....但是 x 轴值是像 1,2,3,.... 这样的索引。

But my dataframe has a column self.signals['DATA'] so how can I use it as x axis values?但是我的 dataframe 有一列self.signals['DATA']那么我怎样才能将它用作 x 轴值呢?

With set_xticks you set, where the positions are, for example:使用set_xticks设置位置,例如:

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

and with set_xticklabels you can define what it says there:使用set_xticklabels你可以定义它在那里的内容:

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

see https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.set_xticklabels.html for more options.有关更多选项,请参阅https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.set_xticklabels.html

But the x axis values are indexes like 1,2,3,...但是 x 轴值是像 1,2,3,... 这样的索引

But my dataframe has a column self.signals['DATA'] so how can I use it as x axis values?但是我的 dataframe 有一列 self.signals['DATA'] 那么我怎样才能将它用作 x 轴值呢?

I assume you are using pandas and matplotlib.我假设您使用的是 pandas 和 matplotlib。

According to matplotlib documentation , you can simply pass the X and Y values to the plot function.根据matplotlib 文档,您可以简单地将 X 和 Y 值传递给 plot function。

So instead of calling所以不要打电话

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

you can for instance do:例如你可以这样做:

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

Depending on other arguments, this will do a scatter plot or a line plot. see matplotlib documentation for more fine tuning of your charts.根据其他 arguments,这将执行一个散点图 plot 或一条线 plot。请参阅 matplotlib 文档以更精细地调整您的图表。

or you could even try:或者你甚至可以尝试:

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

Quoting documentation:引用文档:

Call signatures:调用签名:

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