本文介绍了如何创建带有阈值线的matplotlib条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何创建带有阈值线的matplotlib条形图,阈值线以上的条形部分应为红色,阈值线以下的条形部分应为绿色。请提供一个简单的例子,我在网上找不到任何东西。
I'd like to know how to create a matplotlib bar chart with a threshold line, the part of bars above threshold line should have red color, and the parts below the threshold line should be green. Please provide me a simple example, I couldn't find anything on the web.
推荐答案
将其制作成堆积的条形图,例如,但将数据分为阈值以上的部分和阈值以下的部分。示例:
Make it a stacked bar chart, like in this example, but divide your data up into the parts above your threshold and the parts below. Example:
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")
这篇关于如何创建带有阈值线的matplotlib条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!