【发布时间】:2016-03-19 01:20:54
【问题描述】:
我想要一个堆积条形图,其中有一条阈值线。高于阈值线,条颜色应为红色,低于阈值线条颜色应为绿色。问题是,阈值不是恒定的,每个 x 值可能具有不同的值。而且我想随时为某个 x 值更新此阈值。 如何做到这一点?显然,条形图的 y 参数应该是动态的,也许我应该为它们传递一个函数?请帮我解决一下这个。 另外我认为阈值也应该是一个函数,因为我也会更新它
import numpy as np
import matplotlib.pyplot as plt
# some example data
threshold = 43.0
values = np.array([30., 87.3, 99.9, 3.33, 50.0])
x = range(len(values))
# split it up
above_threshold = np.maximum(values - threshold, 0)
below_threshold = np.minimum(values, threshold)
# and plot it
fig, ax = plt.subplots()
ax.bar(x, below_threshold, 0.35, color="g")
ax.bar(x, above_threshold, 0.35, color="r",
bottom=below_threshold)
# horizontal line indicating the threshold
ax.plot([0., 4.5], [threshold, threshold], "k--")
ax.show()
【问题讨论】:
-
为什么不创建两个数据列表:一个具有低于阈值的值,另一个具有高于阈值的值并绘制类似的堆叠条形图?
-
但是,当阈值发生变化时,如何更新条形图对应的
y值?而且条形图1的bottom参数也应该更新了,但是这一切怎么办?? -
您可以按照自己喜欢的方式更改阈值,您只需更新两个列表中的值。
-
我认为
below_threshold和above_threshold参数应该是函数。我错了吗?
标签: python matplotlib charts