假設我有以下資料:
x = np.array([0, 100, 300, 500, 700, 900, 1100, 1300, 1500, 1700, 1900])
y = pd.DataFrame([1.000000, 0.966757, 0.853819, 0.693619, 0.564981, 0.470379, 0.372452, 0.275968, 0.195698, 0.125962, 0.071663],
index=(0, 100, 300, 500, 700, 900, 1100, 1300, 1500, 1700, 1900))
mean = 0.5
sigma_1 = 0.34
range = 0.17
yy = np.array(y)
yyy = yy[:, 0]
我需要使用fill_between來填充我的圖的下部,直到特定值(此處標識為range),盡管我已經使用了interpolate=True它,但它會給我以下圖。
任何幫助都受到高度贊賞。
fig, ax = plt.subplots()
ax.plot(y)
ax.set_ylim(ymin=0)
plt.axhline(y=range, color='gray', linestyle='--', xmax=0.84)
plt.axhline(y=mean, color='gray', linestyle='--', xmax=0.42)
plt.axvline(x=np.interp(range, yyy[::-1], x[::-1]), ymin=0., ymax=.2, color='k', linewidth=1, lw='.8', linestyle='--')
plt.text(1500, .042, "range", fontsize=12, rotation=90)
plt.text(300, .55, 'mean', fontsize=12)
plt.text(300, .2, '1 STD', fontsize=12)
ax.fill_between(x, yyy, where=yyy >= range, interpolate=True, facecolor='red', edgecolor='0.2', alpha=.4)
plt.show()

uj5u.com熱心網友回復:
該interpolate=的選項fill_between只做有限的調整。通過大幅增加曲線的點數(通過np.interp())可以獲得更好看的結果。
這是更新的代碼。其他一些調整是:
- 更改變數名稱
range以y_range避免隱藏內置range命令 - 不
plt與面向物件的介面混合 - 更改
axhline為hlines并計算精確的交點。
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0, 100, 300, 500, 700, 900, 1100, 1300, 1500, 1700, 1900])
yy = np.array([1, 0.966757, 0.853819, 0.693619, 0.564981, 0.470379, 0.372452, 0.275968, 0.195698, 0.125962, 0.071663])
y_mean = 0.5
sigma_1 = 0.34
y_range = 0.17
fig, ax = plt.subplots()
ax.plot(x, yy)
ax.set_ylim(ymin=0)
ax.margins(x=0) # no white space left and right
x_range = np.interp(y_range, yy[::-1], x[::-1])
ax.hlines(y=y_range, xmin=0, xmax=x_range, color='gray', linestyle='--')
x_mean = np.interp(y_mean, yy[::-1], x[::-1])
ax.hlines(y=y_mean, xmin=0, xmax=x_mean, color='gray', linestyle='--')
ax.vlines(x=x_range, ymin=0., ymax=y_range, color='k', linewidth=1, linestyle='--')
ax.text(x_range, y_range / 2, 'range\n', fontsize=12, rotation=90, ha='center', va='center')
ax.text(300, y_mean, 'mean\n', fontsize=12, va='center')
ax.text(300, y_range, '1 STD\n', fontsize=12, va='center')
x_long = np.linspace(x[0], x[-1], 500)
y_long = np.interp(x_long, x, yy)
ax.fill_between(x_long, y_long, where=y_long >= y_range, interpolate=True, facecolor='red', edgecolor='0.2', alpha=.4)
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/362618.html
標籤:Python matplotlib
