Milor123 Milor123 - 7 months ago 30
Python Question

Matplotlib (previous and next view)

I've created multiple figures but I don't want to appear all at once, how could I use the buttons ? for example:

buttons

When I click in next, change for other figure

My code, but this show 2 figures, and I dont like this:

import matplotlib.pyplot as plt
intervals = ['52, 57', '57, 62', '62, 67', '67, 72', '72, 77', '77, 82', '82, 87']
absolute = [3, 11, 23, 27, 10, 5, 1]
pos = np.arange(len(absolute))
# figure with bars fi
width = 1.0
fig1= plt.figure()
ax1 = plt.axes()
ax1.plot(pos, absolute, '-', linewidth=2)


# figure with lines fi
fig2 = plt.figure()
pos = np.arange(len(absolute))
x = np.linspace(0, 10)
ax = plt.axes()
ax.plot(pos, absolute, '--', linewidth=2)

plt.show()


Thank you very much!

Greetings!

Answer

Using an Ipython (Jupyter notebook) this is pretty easy and nice to look at. The keyword bool is all you need to generate a checkbox. If you click it, it changes the value and the plot as shown.

import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import *
%matplotlib inline


t = np.arange(100)
f=0.01
data = np.sin(2*np.pi*f*t)

def plot(lines):
    fig1= plt.figure()
    ax = plt.axes()
    if lines:
        ax.plot(t,data, '-', linewidth=2)
    else: 
        ax.plot(t,data, '.', linewidth=2)
interact(plot, lines=bool())     

enter image description here

enter image description here