Reputation: 147
I have a bar chart with bad (i.e. negative) and good values (i.e. positive). These values are decided by the threshold. Please refer to Postive_Negative_Circles
which displays: Bad= 3472, Good = 664 and threshold = 164.094
If I change the threshold, these values should change. Here is what I have done so far:
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)
The above code works fine and the bar chart also changes, but unfortunately, I am unable to display the value of the bar chart after the Slider update. I can only display the value for the threshold.
Now, I have set the threshold to 114.24 using the Slider widget from matplotlib, and the bar chart should display values: Good = 2543 and Bad= 1593. As you can see that the change in the threshold value is displayed, but not the bar chart values
Please ignore the Reset button on top of the Slider. I tried to change the position of the Reset Button, but it doesn't work. I guess there is a problem with the %matplotlib notebook.
Could someone help me out here? I looked online for the solution (like matplotlib demo or StackOverflow etc), but couldn't find what I was looking for. There are few StackOverflow questions on Slider update for the bar chart, but none talks about the bar chart value. Also, let me know if you need any further information on the code.
If you know any good source or the solution, please let me know. Thank you
UPDATE:
This is what I tried and it doesn't work:
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()
Upvotes: 1
Views: 1034
Reputation: 40727
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
fig,ax = plt.subplots()
labels = ['good','bad']
colors = ['C0','C1']
values = np.random.normal(0,1,size=(1000,))
threshold = 0.
axcolor = 'lightgoldenrodyellow'
axthreshold = plt.axes([0.2, 0.001, 0.65, 0.03], facecolor=axcolor)
sthreshold = Slider(axthreshold, 'Threshold', -1., 1.,
valinit=threshold, valstep=None)
def update(val):
data = [np.sum(values <= val), np.sum(values > val)]
ax.clear()
ax.bar(labels, data, color=colors)
thr_txt = ax.text(0.5, 0, '{:.2f}'.format(val))
good_label = ax.text(0,data[0], 'good={:d}'.format(data[0]), ha='center')
bad_label = ax.text(1,data[1], 'bad={:d}'.format(data[1]),ha='center')
fig.canvas.draw_idle()
sthreshold.on_changed(update)
update(threshold)
Upvotes: 1