簡體   English   中英

有條件地改變背景顏色 matplotlib plot

[英]Conditionally changing background color in matplotlib plot

我正在繪制 Python / Matplotlib 中的股票價格和移動平均線。現在我想將背景顏色更改為紅色,如果收盤價低於移動平均線 -5%,當收盤價高於移動平均線 5% 以上時,我想將背景顏色更改為綠色移動平均線。 為了使事情更簡單,dataframe 已經包含一個列“above_below”:如果價格高於 MA >5%,則 =1,如果價格低於 MA < -5%,則為 -1。 如何根據此列實現條件背景?

這是我到目前為止所擁有的:

from yahoo_fin import stock_info as si
import talib
import numpy as np
import matplotlib.pyplot as plt

sym = 'SBUX'
data = si.get_data(sym, start_date='01/01/2022')
data['ma10'] = talib.EMA(data['close'], 10)
conditions = [(data['close'] / data['ma10'] -1 > 0.05), (data['close'] / data['ma10'] -1 <-0.05)]
values = [1,-1]

data['above_below'] = np.select(conditions, values, default = np.nan)
fig, ax = plt.subplots()
ax.plot(data.close)
ax.plot(data.ma10)
ax.pcolorfast(ax.get_xlim(), ax.get_ylim(), data['above_below'].values[np.newaxis], cmap='RdYlGn', alpha=0.3)
plt.show()

然而結果似乎並不正確。 dataframe 看起來不錯:

                  open        high         low       close    adjclose    volume ticker        ma10  above_below
2022-01-03  116.470001  117.800003  114.779999  116.680000  114.626328   5475700   SBUX         NaN          NaN
2022-01-04  116.900002  117.050003  114.169998  114.239998  112.229271   8367600   SBUX         NaN          NaN
2022-01-05  114.400002  114.959999  110.400002  110.440002  108.496162   8662300   SBUX         NaN          NaN
2022-01-06  110.000000  111.879997  109.989998  111.139999  109.183838   6099900   SBUX         NaN          NaN
2022-01-07  108.220001  109.709999  107.480003  107.570000  105.676674  11266400   SBUX         NaN          NaN
2022-01-10  106.620003  107.010002  104.419998  106.029999  104.163773   8499400   SBUX         NaN          NaN
2022-01-11  106.040001  106.169998  103.709999  104.040001  102.208801  13073200   SBUX         NaN          NaN
2022-01-12  104.440002  105.320000  103.680000  103.870003  102.041801  11810500   SBUX         NaN          NaN
2022-01-13  104.150002  104.669998  102.089996  102.400002  100.597672   9818500   SBUX         NaN          NaN
2022-01-14  101.910004  101.910004   99.089996  100.120003   98.357796  13703200   SBUX  107.653001         -1.0
2022-01-18   99.169998   99.360001   97.510002   97.730003   96.009865  11396000   SBUX  105.848819         -1.0
2022-01-19   97.940002   98.389999   96.779999   96.870003   95.165001  10856800   SBUX  104.216307         -1.0
2022-01-20   97.489998   98.940002   95.589996   95.720001   94.035240  20311500   SBUX  102.671524         -1.0
2022-01-21   95.900002   98.410004   95.470001   96.309998   94.614853  13438300   SBUX  101.514883         -1.0
2022-01-24   94.750000   98.349998   94.410004   98.099998   96.373344  17201400   SBUX  100.893995          NaN
2022-01-25   96.669998   98.089996   95.129997   97.010002   95.302536  12368500   SBUX  100.187814          NaN
2022-01-26   97.699997   98.639999   94.900002   95.580002   93.897705  11863000   SBUX   99.350030          NaN

MA 需要至少 10 天的數據來計算,這似乎沒問題。 所以我希望紅色/綠色背景將從移動平均線開始。 然而在圖表中,背景 colors 似乎有一些偏移。 前幾個甚至還沒有移動平均線的數據點有紅色背景。 最后,當價格低於 MA 時,它顯示為綠色。

當我用其他股票代碼檢查它時,它也有點偏離......

這個問題的原因可能是什么?

我想我找到了一個簡單的解決方案,只需在 fig, ax = plt.subplots(): ax.margins(0) 之后添加以下行在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM