我想知道如何创建一个matplotlib条形图与阈值线,酒吧的一部分,阈值线以上应该是红色的,和阈值线以下的部分应该是绿色的。请给我一个简单的例子,我不能在网上找到任何东西。
tzxcd3kk1#
您可以像这样简单地使用axhline。
axhline
# For your caseplt.axhline(y=threshold,linewidth=1, color='k')# Another example - You can also define xmin and xmaxplt.axhline(y=5, xmin=0.5, xmax=3.5)
# For your case
plt.axhline(y=threshold,linewidth=1, color='k')
# Another example - You can also define xmin and xmax
plt.axhline(y=5, xmin=0.5, xmax=3.5)
thtygnil2#
将它做成堆叠条形图,就像this example一样,但要将数据分成阈值以上和阈值以下的部分。
import numpy as npimport matplotlib.pyplot as plt# some example datathreshold = 43.0values = np.array([30., 87.3, 99.9, 3.33, 50.0])x = range(len(values))# split it upabove_threshold = np.maximum(values - threshold, 0)below_threshold = np.minimum(values, threshold)# and plot itfig, 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 thresholdax.plot([0., 4.5], [threshold, threshold], "k--")fig.savefig("look-ma_a-threshold-plot.png")
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--")
fig.savefig("look-ma_a-threshold-plot.png")
2条答案
按热度按时间tzxcd3kk1#
您可以像这样简单地使用
axhline
。thtygnil2#
将它做成堆叠条形图,就像this example一样,但要将数据分成阈值以上和阈值以下的部分。