【问题标题】:How can I get the radio button in Matplotlib to update 4 subplots together?如何让 Matplotlib 中的单选按钮一起更新 4 个子图?
【发布时间】:2016-08-22 08:44:31
【问题描述】:

非常感谢您的帮助。我有 3 个 .csv 文件,其中包含一些数据。每个 .csv 文件创建 4 个直方图。我为每个直方图创建了 4 个子图。

我还创建了一个单选按钮,其中 3 个数据集作为变量。我希望使用我在单选按钮选项卡中单击的数据集来更新子图。但是,我无法做到这一点。

随着我的进步,我对编程和学习不熟悉。这是我创建的代码

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()

.csv 文件中的数据如下所示:

[(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)]

【问题讨论】:

    标签: python python-2.7 python-3.x csv matplotlib


    【解决方案1】:
    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()
    

    【讨论】:

    • 正是我想要的。谢谢@Ophir Carmi。非常感谢:-)
    猜你喜欢
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    相关资源
    最近更新 更多