簡體   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