簡體   English   中英

Matplotlib中未用“ fill_between”正確填充的行之間的區域

[英]Areas between lines not filled correctly with “fill_between” in Matplotlib

用Matplotlib將時間序列數據集繪制為:

import matplotlib.pyplot as plt
%matplotlib inline

def WillR(ohlc, window):

    high, low, close = ohlc['High'], ohlc['Low'], ohlc['Close']    
    R = (np.nanmax(high, axis=0) - close) / (np.nanmax(high, axis=0) - np.nanmin(low, axis=0)) * -100

    return R

ohlc = df[::-1]
R = WillR(ohlc, 14)

xAxis = ohlc.index

fig = plt.figure(figsize=(18,9), dpi=100)
ax = fig.add_axes([0.0, 0.6, 1.0, 0.6])
plt.ylabel('Williams '+'%R', fontsize = 16)
ax.xaxis.grid(color='grey', linestyle=':')
ax.yaxis.grid(color='grey', linestyle=':')

ax.plot(R, color='k', alpha=0.8, linewidth=1.2)

ax.axhline(-20, color='r', linewidth=0.8)
ax.axhline(-50, color='k', linestyle='dashed', linewidth=1.2)
ax.axhline(-80, color='g', linewidth=0.8)

ax.fill_between(xAxis, R, -20, where=(R >= -20), facecolor='r', edgecolor='r', alpha=0.5)
ax.fill_between(xAxis, R, -80, where=(R <= -80), facecolor='g', edgecolor='g', alpha=0.5)

在“威廉姆斯%R”圖中,“-20”行上方的區域將用紅色填充,而“ -80”行下方的區域將用紅色填充。 我無法完全填充邊界區域-參見圖1:

圖1

我還嘗試添加參數“ interpolate = True”,結果仍然不令人滿意-見圖2:

圖2

如何使其正常工作? 謝謝

df的樣本數據如下:

                 Date   Open    High     Low  Close
Date                                               
2017-06-27 2017-06-27  10.75  10.850  10.665  10.70
2017-06-26 2017-06-26  10.57  10.740  10.570  10.69
2017-06-23 2017-06-23  10.59  10.710  10.500  10.63
2017-06-22 2017-06-22  10.56  10.720  10.530  10.65
2017-06-21 2017-06-21  10.75  10.750  10.500  10.53
2017-06-20 2017-06-20  10.88  10.925  10.780  10.81
2017-06-19 2017-06-19  10.77  10.920  10.760  10.82
2017-06-16 2017-06-16  10.74  10.910  10.740  10.78
2017-06-15 2017-06-15  10.86  10.930  10.690  10.76
2017-06-14 2017-06-14  10.96  11.040  10.910  11.02
2017-06-13 2017-06-13  10.57  10.930  10.570  10.93
2017-06-09 2017-06-09  10.59  10.650  10.500  10.57
2017-06-08 2017-06-08  10.28  10.590  10.280  10.57
2017-06-07 2017-06-07  10.35  10.420  10.290  10.35
2017-06-06 2017-06-06  10.66  10.700  10.280  10.38
2017-06-05 2017-06-05  10.80  10.850  10.560  10.70
2017-06-02 2017-06-02  11.32  11.390  11.190  11.19
2017-06-01 2017-06-01  11.26  11.330  11.160  11.20
2017-05-31 2017-05-31  11.22  11.400  11.190  11.27
2017-05-30 2017-05-30  11.10  11.260  11.070  11.23
2017-05-29 2017-05-29  11.36  11.370  11.045  11.08
2017-05-26 2017-05-26  11.55  11.590  11.320  11.36
2017-05-25 2017-05-25  11.62  11.670  11.455  11.66
2017-05-24 2017-05-24  11.67  11.755  11.590  11.62
2017-05-23 2017-05-23  11.77  11.835  11.590  11.64
2017-05-22 2017-05-22  12.06  12.110  11.800  11.83
2017-05-19 2017-05-19  12.15  12.235  11.990  12.03
2017-05-18 2017-05-18  12.00  12.130  11.910  12.13
2017-05-17 2017-05-17  12.28  12.300  12.120  12.14
2017-05-16 2017-05-16  12.23  12.385  12.220  12.30
2017-05-15 2017-05-15  12.10  12.255  12.030  12.22
2017-05-12 2017-05-12  12.30  12.300  12.050  12.12
2017-05-11 2017-05-11  12.30  12.445  12.270  12.34
2017-05-10 2017-05-10  11.99  12.415  11.950  12.25
2017-05-09 2017-05-09  12.07  12.100  11.730  11.79
2017-05-08 2017-05-08  12.14  12.230  12.060  12.10
2017-05-05 2017-05-05  12.24  12.250  12.020  12.04
2017-05-04 2017-05-04  12.23  12.270  12.050  12.14
2017-05-03 2017-05-03  12.43  12.460  12.200  12.23
2017-05-02 2017-05-02  12.44  12.470  12.310  12.46
2017-05-01 2017-05-01  12.25  12.440  12.230  12.44
2017-04-28 2017-04-28  12.47  12.480  12.230  12.32
2017-04-27 2017-04-27  12.39  12.515  12.380  12.50
2017-04-26 2017-04-26  12.03  12.510  12.030  12.37
2017-04-24 2017-04-24  12.00  12.055  11.920  12.03
2017-04-21 2017-04-21  11.88  11.990  11.840  11.88
2017-04-20 2017-04-20  11.78  11.840  11.710  11.80
2017-04-19 2017-04-19  11.70  11.745  11.610  11.74
2017-04-18 2017-04-18  11.95  11.950  11.605  11.74
2017-04-13 2017-04-13  11.95  12.010  11.920  11.95
2017-04-12 2017-04-12  12.05  12.050  11.945  12.01
2017-04-11 2017-04-11  11.95  12.140  11.945  12.08
2017-04-10 2017-04-10  11.79  11.930  11.780  11.91
2017-04-07 2017-04-07  11.83  11.830  11.690  11.78
2017-04-06 2017-04-06  11.85  11.870  11.740  11.78
2017-04-05 2017-04-05  12.06  12.140  11.800  11.96
2017-04-04 2017-04-04  12.03  12.160  11.930  12.07
2017-04-03 2017-04-03  12.14  12.180  11.860  11.98
2017-03-30 2017-03-30  12.01  12.230  11.970  12.14
2017-03-29 2017-03-29  11.99  12.050  11.900  12.03

數據需要排序。 然后,選項interpolate=True將提供所需的繪圖。

在此處輸入圖片說明

u = u""" Date Date1   Open    High     Low  Close
2017-06-27 2017-06-27  10.75  10.850  10.665  10.70
2017-06-26 2017-06-26  10.57  10.740  10.570  10.69
2017-06-23 2017-06-23  10.59  10.710  10.500  10.63
2017-06-22 2017-06-22  10.56  10.720  10.530  10.65
2017-06-21 2017-06-21  10.75  10.750  10.500  10.53
2017-06-20 2017-06-20  10.88  10.925  10.780  10.81
2017-06-19 2017-06-19  10.77  10.920  10.760  10.82
2017-06-16 2017-06-16  10.74  10.910  10.740  10.78
2017-06-15 2017-06-15  10.86  10.930  10.690  10.76
2017-06-14 2017-06-14  10.96  11.040  10.910  11.02
2017-06-13 2017-06-13  10.57  10.930  10.570  10.93
2017-06-09 2017-06-09  10.59  10.650  10.500  10.57
2017-06-08 2017-06-08  10.28  10.590  10.280  10.57
2017-06-07 2017-06-07  10.35  10.420  10.290  10.35
2017-06-06 2017-06-06  10.66  10.700  10.280  10.38
2017-06-05 2017-06-05  10.80  10.850  10.560  10.70
2017-06-02 2017-06-02  11.32  11.390  11.190  11.19
2017-06-01 2017-06-01  11.26  11.330  11.160  11.20
2017-05-31 2017-05-31  11.22  11.400  11.190  11.27
2017-05-30 2017-05-30  11.10  11.260  11.070  11.23
2017-05-29 2017-05-29  11.36  11.370  11.045  11.08
2017-05-26 2017-05-26  11.55  11.590  11.320  11.36
2017-05-25 2017-05-25  11.62  11.670  11.455  11.66
2017-05-24 2017-05-24  11.67  11.755  11.590  11.62
2017-05-23 2017-05-23  11.77  11.835  11.590  11.64
2017-05-22 2017-05-22  12.06  12.110  11.800  11.83
2017-05-19 2017-05-19  12.15  12.235  11.990  12.03
2017-05-18 2017-05-18  12.00  12.130  11.910  12.13
2017-05-17 2017-05-17  12.28  12.300  12.120  12.14
2017-05-16 2017-05-16  12.23  12.385  12.220  12.30
2017-05-15 2017-05-15  12.10  12.255  12.030  12.22
2017-05-12 2017-05-12  12.30  12.300  12.050  12.12
2017-05-11 2017-05-11  12.30  12.445  12.270  12.34
2017-05-10 2017-05-10  11.99  12.415  11.950  12.25
2017-05-09 2017-05-09  12.07  12.100  11.730  11.79
2017-05-08 2017-05-08  12.14  12.230  12.060  12.10
2017-05-05 2017-05-05  12.24  12.250  12.020  12.04
2017-05-04 2017-05-04  12.23  12.270  12.050  12.14
2017-05-03 2017-05-03  12.43  12.460  12.200  12.23
2017-05-02 2017-05-02  12.44  12.470  12.310  12.46
2017-05-01 2017-05-01  12.25  12.440  12.230  12.44
2017-04-28 2017-04-28  12.47  12.480  12.230  12.32
2017-04-27 2017-04-27  12.39  12.515  12.380  12.50
2017-04-26 2017-04-26  12.03  12.510  12.030  12.37
2017-04-24 2017-04-24  12.00  12.055  11.920  12.03
2017-04-21 2017-04-21  11.88  11.990  11.840  11.88
2017-04-20 2017-04-20  11.78  11.840  11.710  11.80
2017-04-19 2017-04-19  11.70  11.745  11.610  11.74
2017-04-18 2017-04-18  11.95  11.950  11.605  11.74
2017-04-13 2017-04-13  11.95  12.010  11.920  11.95
2017-04-12 2017-04-12  12.05  12.050  11.945  12.01
2017-04-11 2017-04-11  11.95  12.140  11.945  12.08
2017-04-10 2017-04-10  11.79  11.930  11.780  11.91
2017-04-07 2017-04-07  11.83  11.830  11.690  11.78
2017-04-06 2017-04-06  11.85  11.870  11.740  11.78
2017-04-05 2017-04-05  12.06  12.140  11.800  11.96
2017-04-04 2017-04-04  12.03  12.160  11.930  12.07
2017-04-03 2017-04-03  12.14  12.180  11.860  11.98
2017-03-30 2017-03-30  12.01  12.230  11.970  12.14
2017-03-29 2017-03-29  11.99  12.050  11.900  12.03"""

import io
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
#%matplotlib inline

def WillR(ohlc, window):
    high, low, close = ohlc['High'], ohlc['Low'], ohlc['Close']    
    R = (np.nanmax(high, axis=0) - close) / (np.nanmax(high, axis=0) - np.nanmin(low, axis=0)) * -100
    return R

df = pd.read_csv(io.StringIO(u), delim_whitespace=True, index_col=0, parse_dates=True)

ohlc = df[::-1]
R = WillR(ohlc, 14)

xAxis = ohlc.index

fig = plt.figure(figsize=(18,9), dpi=100)
ax = plt.subplot()
plt.ylabel('Williams '+'%R', fontsize = 16)
ax.xaxis.grid(color='grey', linestyle=':')
ax.yaxis.grid(color='grey', linestyle=':')

ax.plot(R, color='k', alpha=0.8, linewidth=1.2)

ax.axhline(-20, color='r', linewidth=0.8)
ax.axhline(-50, color='k', linestyle='dashed', linewidth=1.2)
ax.axhline(-80, color='g', linewidth=0.8)

ax.fill_between(xAxis, R, -20, where=(R >= -20), facecolor='r', edgecolor='r', alpha=0.5, interpolate=True)
ax.fill_between(xAxis, R, -80, where=(R <= -80), facecolor='g', edgecolor='g', alpha=0.5, interpolate=True)

plt.show()

暫無
暫無

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

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