0

I need to make some plots with multiple parameters, and I choosed to make it more interactive with matplotlib sliders. For some practise before my actual task I tried to make it relativelly simple, but my sliders does not work. Here is the code, which is inspired from here.

Code:

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.widgets as mw
from scipy import stats


mu = 1
sigma = 3

a = 2
b = 3
axis_color = 'lightgoldenrodyellow'


x = [i for i in range(-100,100,1)]

normal_pdf = stats.norm.pdf(x, mu, sigma)
a_normal_pdf = [i*a for i in normal_pdf]
ab_normal_pdf = [i*b*a for i in normal_pdf]


fig = plt.figure()

ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax4.axis('off')
#sliders
a_slider_ax  = fig.add_axes([0.6, 0.25, 0.25, 0.03], axisbg=axis_color)
a_slider = mw.Slider(a_slider_ax, 'a', 1, 100, valinit = a)
b_slider_ax = fig.add_axes([0.6, 0.4, 0.25, .03], axisbg = axis_color)
b_slider = mw.Slider(b_slider_ax, 'b', 1, 100, valinit = b)
#function for sliders
def sliders_on_change(val):
    a_normal_pdf.set_ydata([x*a_slider for x in normal_pdf])
    ab_normal_pdf.set_ydata([x*a_slider*b_slider for x in normal_pdf])
    fig.canvas.draw_idle()
a_slider.on_changed(sliders_on_change)
b_slider.on_changed(sliders_on_change)


ax1.plot(x, normal_pdf, 'r-')
ax2.plot(x, a_normal_pdf, 'bo')
ax3.plot(x, ab_normal_pdf, 'g*')

plt.show()

I do not fully understand HOW sliders should work, so its maybe the problem instead of idle issue as here, because I tried it in spyder and in jupyter as well, no difference. I can move with sliders, but I cant change the a_normal_pdf nor ab_normal_pdf.

Community
  • 1
  • 1
Bobesh
  • 1,157
  • 2
  • 15
  • 30

1 Answers1

1

You have two issues in your code:

  1. using the slider object a_slider in place of the slider's current value a_slider.val

  2. the method set_ydata changes the y-data of a Line2D plot object (I saved it in a variable p1 to be able to modify it)

Modified code (hope this helps)

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.widgets as mw
from scipy import stats


mu = 1
sigma = 3

a = 2
b = 3
axis_color = 'lightgoldenrodyellow'


x = [i for i in range(-100,100,1)]

normal_pdf = stats.norm.pdf(x, mu, sigma)
a_normal_pdf = [i*a for i in normal_pdf]
ab_normal_pdf = [i*b*a for i in normal_pdf]


fig = plt.figure()

ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax4.axis('off')
#sliders
a_slider_ax  = fig.add_axes([0.6, 0.25, 0.25, 0.03], axisbg=axis_color)
a_slider = mw.Slider(a_slider_ax, 'a', 1, 100, valinit = a)
b_slider_ax = fig.add_axes([0.6, 0.4, 0.25, .03], axisbg = axis_color)
b_slider = mw.Slider(b_slider_ax, 'b', 1, 100, valinit = b)
#function for sliders
def sliders_on_change(val):
    p1.set_ydata([x*a_slider.val for x in normal_pdf])
    p2.set_ydata([x*a_slider.val*b_slider.val for x in normal_pdf])
    fig.canvas.draw_idle()

a_slider.on_changed(sliders_on_change)
b_slider.on_changed(sliders_on_change)


p1,=ax1.plot(x, normal_pdf, 'r-')
p2,=ax2.plot(x, a_normal_pdf, 'bo')
p3,=ax3.plot(x, ab_normal_pdf, 'g*')

plt.show()
user2314737
  • 27,088
  • 20
  • 102
  • 114
  • Great answer, but can I ask what is the difference between p1, = *** and p1 = *** ? What does the comma do? – Bobesh May 15 '17 at 10:11
  • @Bobesh It allows to assign result to a tuple (see this question: [Python code. Is it comma operator?](http://stackoverflow.com/questions/16037494/python-code-is-it-comma-operator) – user2314737 May 15 '17 at 10:16