How To Partial Fill_between In Matplotlib, As In Different Colors For Different Values
I'm trying to color the space between the graph line and the x-axis. The color should be based on the value of the corresponding point on the line. Kinda like the first graph: (ht
Solution 1:
Use numpy
, A > B
. Else, if not wanting to use numpy it would need to be [a > b for a,b in zip(A,B)]
.
import numpy as np
import matplotlib.pyplot as plt
y = [107, 108, 105, 109, 107, 106, 107, 109, 106, 106, 94, 93, 94, 93, 93, 94, 95, 106, 108,
109, 107, 107, 106, 108, 105, 108, 107, 106, 107, 97, 93, 96, 94, 96, 95, 94, 104, 107,
106, 108, 107, 107, 106, 107, 105, 107, 108, 105, 107, 100, 93, 94, 93, 95, 104, 107, 107,
108, 108, 107, 107, 107, 107, 104, 94, 96, 95, 96, 94, 95, 94, 100, 107, 107, 105, 107, 107,
109, 107, 108, 107, 105, 108, 108, 106, 97, 94, 94, 94, 94, 95, 94, 94, 94, 96, 108, 108, 107,
106, 107, 107, 108, 107, 106, 95, 95, 95, 94, 94, 96, 105, 108, 107, 106, 106, 108, 107,
108, 106, 107]
y = np.array(y)
x = np.arange(len(y))
lvl = 100
fig, ax = plt.subplots()
ax.plot(x, y, color="black")
ax.fill_between(x, 0, y, where=y>lvl, facecolor='red', interpolate=True)
ax.fill_between(x, 0, y, where=y<=lvl, facecolor='green', interpolate=True)
plt.show()
Post a Comment for "How To Partial Fill_between In Matplotlib, As In Different Colors For Different Values"