Interactive matplotlib chart with two sliders

I used matplotlib to create some graph that depends on 8 variables. I would like to study how the plot changes when I change some of them. I have created several scripts that call matplotlib and generate different snapshots that are later converted to a movie, which is nice, but a little awkward.

  • I wonder how I could interact with the regeneration of the plot using the keyboard keys to increase / decrease the values ​​of some variables and immediately see how the plot changes.

  • What is the best approach for this?

  • Also, if you can point me to interesting links or a link to an example with two sliders?

+45
python matplotlib interactive keyboard
Jul 14 '11 at 17:10
source share
4 answers

In addition to what is mentioned in @triplepoint, look at the slider widget.

Here is an example on the matplotlib example page . This is a graphic slider, not a keyboard binding, but it is great for what you want to do.

Also note: to ensure that sliders and buttons remain responsive and not to collect garbage, object references ( amp_slider , freq_slider , etc.) must be supported by themselves.

(I create this community wiki as I simply copy the example from the example. This particular example teaches bad habits (for example, from pylab import * ), but it gets the point. The example has been fixed to avoid using pylab .)

 from numpy import pi, sin import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button, RadioButtons def signal(amp, freq): return amp * sin(2 * pi * freq * t) axis_color = 'lightgoldenrodyellow' fig = plt.figure() ax = fig.add_subplot(111) # Adjust the subplots region to leave some space for the sliders and buttons fig.subplots_adjust(left=0.25, bottom=0.25) t = np.arange(0.0, 1.0, 0.001) amp_0 = 5 freq_0 = 3 # Draw the initial plot # The 'line' variable is used for modifying the line later [line] = ax.plot(t, signal(amp_0, freq_0), linewidth=2, color='red') ax.set_xlim([0, 1]) ax.set_ylim([-10, 10]) # Add two sliders for tweaking the parameters # Define an axes area and draw a slider in it amp_slider_ax = fig.add_axes([0.25, 0.15, 0.65, 0.03], axisbg=axis_color) amp_slider = Slider(amp_slider_ax, 'Amp', 0.1, 10.0, valinit=amp_0) # Draw another slider freq_slider_ax = fig.add_axes([0.25, 0.1, 0.65, 0.03], axisbg=axis_color) freq_slider = Slider(freq_slider_ax, 'Freq', 0.1, 30.0, valinit=freq_0) # Define an action for modifying the line when any slider value changes def sliders_on_changed(val): line.set_ydata(signal(amp_slider.val, freq_slider.val)) fig.canvas.draw_idle() amp_slider.on_changed(sliders_on_changed) freq_slider.on_changed(sliders_on_changed) # Add a button for resetting the parameters reset_button_ax = fig.add_axes([0.8, 0.025, 0.1, 0.04]) reset_button = Button(reset_button_ax, 'Reset', color=axis_color, hovercolor='0.975') def reset_button_on_clicked(mouse_event): freq_slider.reset() amp_slider.reset() reset_button.on_clicked(reset_button_on_clicked) # Add a set of radio buttons for changing color color_radios_ax = fig.add_axes([0.025, 0.5, 0.15, 0.15], axisbg=axis_color) color_radios = RadioButtons(color_radios_ax, ('red', 'blue', 'green'), active=0) def color_radios_on_clicked(label): line.set_color(label) fig.canvas.draw_idle() color_radios.on_clicked(color_radios_on_clicked) plt.show() 

Example

+58
Jul 14 '11 at 17:38
source share

Matplotlib has some pretty nice gui functionality. The source tarball matplotlib has sample documentation, in / examples / user _interfaces and matplotlib> / examples / event_handling. In particular, when processing keys: http://matplotlib.sourceforge.net/examples/event_handling/keypress_demo.html

I did something similar to what you are aiming for:

 import numpy as np import pylab class plotter: def __init__(self, initial_values): self.values self.fig = pylab.figure() pylab.gray() self.ax = self.fig.add_subplot(111) self.draw() self.fig.canvas.mpl_connect('key_press_event',self.key) def draw(self): im = your_function(self.values) pylab.show() self.ax.imshow(im) def key(self, event): if event.key=='right': self.values = modify() elif event.key == 'left': self.values = modify() self.draw() self.fig.canvas.draw() 

I used this to go through the display of various images on the stack when I press keys, but you should be able to insert logic to change your values ​​using keyboard input.

If you want to do things like custom input values, I think the examples have options for dialog boxes, but if you just want to increase / decrease the number of variables, then just defining pairs of pairs for them in this way may work well

+6
Jul 14 '11 at 17:34
source share

I don’t think that just plotting with plt.plot will allow you to do this. You will need to create your own script / app GUI by inserting Matplotlib into it. Matplotlib currently supports all the main GUI tools - PyGTK +, PyQt4 and wxPython.
I use wxPython and embedding matplotlib in it is pretty simple. The same should be the case with other GUI tools. You can get all the necessary information for this in the book - enter image description here

It is available on amazon here .

+2
Jul 14 '11 at 17:27
source share

Use waitforbuttonpress(timeout=0.001) , then your mouse waitforbuttonpress(timeout=0.001) will be visible on the chart.

+2
Aug 13 2018-12-12T00:
source share



All Articles