【问题标题】:matplotlib stacked bar chart with dynamic threshold line具有动态阈值线的matplotlib堆积条形图
【发布时间】: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


【解决方案1】:

我认为您可以使用堆积条形图而不是带阈值的条形图。您可以在 example 中看到它(只需消除错误栏)。

【讨论】:

  • 但我想用虚线显示阈值线。
猜你喜欢
  • 1970-01-01
  • 2018-01-17
  • 2019-11-28
  • 2021-02-27
  • 2018-11-30
  • 2017-11-02
  • 1970-01-01
相关资源
最近更新 更多