简体   繁体   中英

How can I get the radio button in Matplotlib to update 4 subplots together?

I would highly appreciate your help here. I have 3 .csv files with some data in them. Each .csv file creates 4 histograms. I have created 4 subplots for each histogram.

I have also created a radio button with the 3 data sets as variables. I would like the subplots to be updated with the data set I click on in the radio button tab. However, I am unable to do that.

I am new to programming and learning as I progress with this. Here's the code that I created

import csv
import numpy as np
from scipy.stats import norm, lognorm
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from matplotlib.widgets import Slider, Button, RadioButtons



#####Importing Data from csv file#####

dataset1 = np.genfromtxt('dataSet1.csv', dtype = float, delimiter = ',', skip_header = 1, names = ['a', 'b', 'c', 'x0'])
dataset2 = np.genfromtxt('dataSet2.csv', dtype = float, delimiter = ',', skip_header = 1, names = ['a', 'b', 'c', 'x0'])
dataset3 = np.genfromtxt('dataSet3.csv', dtype = float, delimiter = ',', skip_header = 1, names = ['a', 'b', 'c', 'x0'])         

#####_____#####



#####Creating Subplots#####

fig = plt.figure()
plt.subplots_adjust(left=0.15, bottom=0.1)

ax1 = fig.add_subplot(321)                                                  #Subplot 1
ax1.set_xlabel('a', fontsize = 14)
#ax1.set_ylabel('PDF', fontsize = 14)
##ax1.set_title('Probability Density Function', fontsize = 10)
##ax1.text(-2, 0.4, r'$\mu=0,\ \sigma^2=1$')
ax1.grid(True)

ax2 = fig.add_subplot(323)                                                  #Subplot 2
ax2.set_xlabel('b', fontsize = 14)
#ax2.set_ylabel('PDF', fontsize = 14)
##ax2.set_title('Probability Density Function', fontsize = 10)
##ax2.text(-2, 0.4, r'$\mu=0,\ \sigma^2=1$')
ax2.grid(True)

ax3 = fig.add_subplot(325)                                                  #Subplot 3
ax3.set_xlabel('c', fontsize = 14)
#ax3.set_ylabel('PDF', fontsize = 14)
##ax3.set_title('Probability Density Function', fontsize = 10)
##ax3.text(-2, 0.4, r'$\mu=0,\ \sigma^2=1$')
ax3.grid(True)

ax4 = fig.add_subplot(122)                                                  #Subplot 4
ax4.set_xlabel('x0', fontsize = 14)
ax4.set_ylabel('PDF', fontsize = 14)
##ax4.set_title('Probability Density Function', fontsize = 10)
##ax4.text(-2, 0.4, r'$\mu=0,\ \sigma^2=1$')
ax4.grid(True)

#####_____#####



#####Plotting Distributions#####

ax1 = ax1.hist(dataset1['a'], bins=50, color = 'red',alpha = 0.5, normed = True)
ax2 = ax2.hist(dataset1['b'], bins=50, color = 'red',alpha = 0.5, normed = True)
ax3 = ax3.hist(dataset1['c'], bins=50, color = 'red',alpha = 0.5, normed = True)
ax4 = ax4.hist(dataset1['x0'], bins=50, color = 'red',alpha = 0.5, normed = True)

#####_____#####



#######Creating Radio Button#####

axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.03, 0.48, 0.06, 0.09], axisbg=axcolor)
radio = RadioButtons(rax, ('Data Set1', 'Data Set2', 'Data Set3'))

#####_____#####



#####Updating Radio Button#####

radio_label = 'Data Set1'
func = {'Data Set1': dataset1, 'Data Set2': dataset2, 'Data Set3': dataset3}
axcl = {'Data Set1': 'red', 'Data Set2': 'blue', 'Data Set3': 'green'}

def update_radio(label):
    global radio_label             #so we can overwrite the variable defined above and not create a local one
    radio_label = label
    print radio_label
    if label == 'Data Set2':
        plt.hist(func[radio_label]['a'], bins=50, color = 'red',alpha = 0.5)
        plt.hist(func[radio_label]['b'], bins=50, color = 'red',alpha = 0.5)
        plt.hist(func[radio_label]['c'], bins=50, color = 'red',alpha = 0.5)
        plt.hist(func[radio_label]['x0'], bins=50, color = 'red',alpha = 0.5)
    elif label == 'Data Set3':
        plt.hist(func[radio_label]['a'], bins=50, color = 'red',alpha = 0.5)
        plt.hist(func[radio_label]['b'], bins=50, color = 'red',alpha = 0.5)
        plt.hist(func[radio_label]['c'], bins=50, color = 'red',alpha = 0.5)
        plt.hist(func[radio_label]['x0'], bins=50, color = 'red',alpha = 0.5)


    plt.draw()



radio.on_clicked(update_radio)

#####_____#####




plt.show()

Data in the .csv files look like this:

[(1.0876, 10.44, -14.036, -10.795) (0.89453, 8.0052, -13.198, -10.372) (0.8199, 8.0609, -11.475, -11.093) ..., (1.2176, 10.45, -13.828, -9.7473) (0.94211, 11.82, -7.7689, -13.173) (0.83588, 11.882, -13.807, -15.295)]

import csv
import numpy as np
from scipy.stats import norm, lognorm
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from matplotlib.widgets import Slider, Button, RadioButtons



#####Importing Data from csv file#####

dataset1 = np.genfromtxt('dataSet1.csv', dtype = float, delimiter = ',', skip_header = 1, names = ['a', 'b', 'c', 'x0'])
dataset2 = np.genfromtxt('dataSet2.csv', dtype = float, delimiter = ',', skip_header = 1, names = ['a', 'b', 'c', 'x0'])
dataset3 = np.genfromtxt('dataSet3.csv', dtype = float, delimiter = ',', skip_header = 1, names = ['a', 'b', 'c', 'x0'])

#####_____#####



#####Creating Subplots#####

fig = plt.figure()
plt.subplots_adjust(left=0.15, bottom=0.1)

ax1 = fig.add_subplot(321)                                                  #Subplot 1
ax1.set_xlabel('a', fontsize = 14)
#ax1.set_ylabel('PDF', fontsize = 14)
##ax1.set_title('Probability Density Function', fontsize = 10)
##ax1.text(-2, 0.4, r'$\mu=0,\ \sigma^2=1$')
ax1.grid(True)

ax2 = fig.add_subplot(323)                                                  #Subplot 2
ax2.set_xlabel('b', fontsize = 14)
#ax2.set_ylabel('PDF', fontsize = 14)
##ax2.set_title('Probability Density Function', fontsize = 10)
##ax2.text(-2, 0.4, r'$\mu=0,\ \sigma^2=1$')
ax2.grid(True)

ax3 = fig.add_subplot(325)                                                  #Subplot 3
ax3.set_xlabel('c', fontsize = 14)
#ax3.set_ylabel('PDF', fontsize = 14)
##ax3.set_title('Probability Density Function', fontsize = 10)
##ax3.text(-2, 0.4, r'$\mu=0,\ \sigma^2=1$')
ax3.grid(True)

ax4 = fig.add_subplot(122)                                                  #Subplot 4
ax4.set_xlabel('x0', fontsize = 14)
ax4.set_ylabel('PDF', fontsize = 14)
##ax4.set_title('Probability Density Function', fontsize = 10)
##ax4.text(-2, 0.4, r'$\mu=0,\ \sigma^2=1$')
ax4.grid(True)

#####_____#####



#####Plotting Distributions#####

ax1.hist(dataset1['a'], bins=50, color = 'red',alpha = 0.5, normed = True)
ax2.hist(dataset1['b'], bins=50, color = 'red',alpha = 0.5, normed = True)
ax3.hist(dataset1['c'], bins=50, color = 'red',alpha = 0.5, normed = True)
ax4.hist(dataset1['x0'], bins=50, color = 'red',alpha = 0.5, normed = True)

#####_____#####



#######Creating Radio Button#####

axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.03, 0.48, 0.06, 0.09], axisbg=axcolor)
radio = RadioButtons(rax, ('Data Set1', 'Data Set2', 'Data Set3'))

#####_____#####



#####Updating Radio Button#####

radio_label = 'Data Set1'
func = {'Data Set1': dataset1, 'Data Set2': dataset2, 'Data Set3': dataset3}
axcl = {'Data Set1': 'red', 'Data Set2': 'blue', 'Data Set3': 'green'}

def update_radio(label):
    global radio_label             #so we can overwrite the variable defined above and not create a local one
    radio_label = label
    print(radio_label)
    ax1.clear()
    ax2.clear()
    ax3.clear()
    ax4.clear()
    ax1.hist(func[radio_label]['a'], bins=50, color = 'red',alpha = 0.5)
    ax2.hist(func[radio_label]['b'], bins=50, color = 'red',alpha = 0.5)
    ax3.hist(func[radio_label]['c'], bins=50, color = 'red',alpha = 0.5)
    ax4.hist(func[radio_label]['x0'], bins=50, color = 'red',alpha = 0.5)
    ax1.grid(True)
    ax2.grid(True)
    ax3.grid(True)
    ax4.grid(True)


    plt.draw()



radio.on_clicked(update_radio)

#####_____#####




plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM