是否可以用从左(绿色)到右(透明)的渐变填充“赔率”区域?我想在图中这样做以表明不确定性。
import numpy as np
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y1 = [1, 1, 2, 3, 5]
y2 = [0, 4, 2, 6, 8]
y3 = [1, 3, 5, 7, 9]
y = np.vstack([y1, y2, y3])
labels = ["Fibonacci ", "Evens", "Odds"]
fig, ax = plt.subplots()
ax.stackplot(x, y1, y2, y3, labels=labels)
ax.legend(loc='upper left')
plt.show()
fig, ax = plt.subplots()
ax.stackplot(x, y)
plt.show()
你的意思是这个吗?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
x = [1, 2, 3, 4, 5]
y1 = [1, 1, 2, 3, 5]
y2 = [0, 4, 2, 6, 8]
y3 = [1, 3, 5, 7, 9]
y = np.vstack([y1, y2, y3])
labels = ["Fibonacci", "Evens", "Odds"]
fig, ax = plt.subplots()
# Create a custom colormap
cmap = LinearSegmentedColormap.from_list("custom_cmap", ["green", "transparent"])
# Plot the first two areas normally
ax.stackplot(x, y1, y2, labels=labels[:2])
# Plot the "Odds" area with a gradient
for i in range(len(x) - 1):
ax.fill_between(x[i:i+2], y1[i:i+2] + y2[i:i+2], y1[i:i+2] + y2[i:i+2] + y3[i:i+2],
color=cmap(i / (len(x) - 1)))
ax.legend(loc='upper left')
plt.show()