【发布时间】:2020-02-09 03:14:39
【问题描述】:
我正在尝试为图形线和 x 轴之间的空间着色。颜色应以线上对应点的值为准。有点像第一张图: (https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/fill_between_demo.html)
如果高于 100,则应为红色,如果低于 100,则应为绿色。我要找的图表应该是红绿交替的。
这是我目前拥有的:
import matplotlib.pyplot as plt
lenght = 120
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]
x = [x for x in range(lenght)]
lvl = lenght * [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()
值小于 100 的区域应该是绿色的。但是线和 x 轴之间的空间始终是基于数组中第一个值的颜色(在本例中为红色)。我该如何解决这个问题?
【问题讨论】:
标签: python-3.x matplotlib graph colors