我有一个带有坏值(即负值)和好值(即正值)的条形图。这些值由阈值决定。请参考Postive_Negative_Circles
显示:Bad= 3472,Good = 664 和 threshold = 164.094
如果我改变阈值,这些值应该会改变。这是我到目前为止所做的:
import matplotlib.pyplot as plt
import pylab as p
from matplotlib.widgets import Slider, Button
axcolor = 'lightgoldenrodyellow'
axthreshold = plt.axes([0.2, 0.001, 0.65, 0.03], facecolor=axcolor)
sthreshold = Slider(axthreshold, 'Threshold', 0.0, 300,
valinit=threshold, valstep=None)
fig_text1 = p.figtext(0.5, 0.65, str(sthreshold.val))
def update(val):
thresh = int(sthreshold.val)
data = [np.sum(values <= thresh), np.sum(values > thresh)]
ax.clear ()
ax.bar(labels, data, color=colors)
np.set_printoptions(precision=2)
fig_text1.set_text(str(sthreshold.val))
fig.canvas.draw_idle()
sthreshold.on_changed(update)
resetax = plt.axes([0.7, 0.001, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
def reset(event):
sthreshold.reset()
button.on_clicked(reset)
上面的代码工作正常,条形图也发生了变化,但不幸的是,在 Slider 更新后我无法显示条形图的值。我只能显示阈值。
现在,我使用 matplotlib 中的 Slider 小部件将阈值设置为 114.24,条形图应显示值:Good = 2543 和 Bad= 1593。如您所见,显示的是阈值的变化,而不是条形图图表值
请忽略滑块顶部的重置按钮。我试图改变重置按钮的位置,但它不起作用。我猜 %matplotlib 笔记本有问题。
有人可以帮我吗?我在网上寻找解决方案(如 matplotlib 演示或 StackOverflow 等),但找不到我要找的东西。关于条形图的 Slider 更新的 StackOverflow 问题很少,但没有人谈论条形图的值。另外,如果您需要有关代码的更多信息,请告诉我。
如果您知道任何好的来源或解决方案,请告诉我。谢谢
更新:
这是我尝试过的,但它不起作用:
def update(val):
thresh = int(sthreshold.val)
print(thresh)
data = [np.sum(values <= thresh), np.sum(values > thresh)]
ax.clear ()
bars = ax.bar(labels, data, color=colors)
for rect in bars:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width()/2.0, height, '%d' %
int(height), ha='center', va='bottom')
np.set_printoptions(precision=2)
fig_text1.set_text(str(sthreshold.val))
fig.canvas.draw_idle()