Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 更新Matplotlib CheckButton小部件_Python 2.7_Python 3.x_Matplotlib - Fatal编程技术网

Python 2.7 更新Matplotlib CheckButton小部件

Python 2.7 更新Matplotlib CheckButton小部件,python-2.7,python-3.x,matplotlib,Python 2.7,Python 3.x,Matplotlib,我对编程、python和stackflow还不熟悉,所以请耐心听我解释 我正在编写一个python脚本,其中一个数据集中有4组值(a、b、c&x0)。我有3个这样的数据集 目前,我使用单选按钮在数据集之间切换,它工作得非常好 在以后的阶段,需要显示数据集1和2、1和3、2和3或1、2和3之间分布的比较,我使用单选按钮实现了这一点(见下图) 我现在希望使用复选按钮而不是单选按钮,因为它可以让我只使用3个标签(数据集1、数据集2和数据集3)而不是7,从而获得与单选按钮相同的结果 但是,我不知道如何

我对编程、python和stackflow还不熟悉,所以请耐心听我解释

我正在编写一个python脚本,其中一个数据集中有4组值(a、b、c&x0)。我有3个这样的数据集

目前,我使用单选按钮在数据集之间切换,它工作得非常好

在以后的阶段,需要显示数据集1和2、1和3、2和3或1、2和3之间分布的比较,我使用单选按钮实现了这一点(见下图)

我现在希望使用复选按钮而不是单选按钮,因为它可以让我只使用3个标签(数据集1、数据集2和数据集3)而不是7,从而获得与单选按钮相同的结果

但是,我不知道如何更新检查按钮以使它们执行比较

我的代码如下所示:

import numpy as np
print np.__version__
import scipy
print scipy.__version__
from scipy.stats import norm, lognorm, stats, uniform
import matplotlib
print matplotlib.__version__
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
from matplotlib.patches import Polygon



#####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.13,right=0.99,bottom=0.05)

ax1 = fig.add_subplot(331)                                                  #Subplot 1
ax1.set_xlabel('a' , fontsize = 14)
ax1.grid(True)

ax2 = fig.add_subplot(334)                                                  #Subplot 2
ax2.set_xlabel('b', fontsize = 14)
ax2.grid(True)

ax3 = fig.add_subplot(337)                                                  #Subplot 3
ax3.set_xlabel('c', fontsize = 14)
ax3.grid(True)

ax4 = fig.add_subplot(132)                                                  #Subplot 4
ax4.set_xlabel('x0', fontsize = 14)
ax4.set_ylabel('PDF', fontsize = 14)
ax4.grid(True)

ax5 = fig.add_subplot(133)                                                  #Subplot 5
ax5.set_xlabel('x0', fontsize = 14)
ax5.set_ylabel('CDF', fontsize = 14)
ax5.grid(True)

#####_____#####



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

[n1,bins1,patches] = ax1.hist(dataset1['a'], bins=50, color = 'red',alpha = 0.5, normed = True)
[n2,bins2,patches] = ax2.hist(dataset1['b'], bins=50, color = 'red',alpha = 0.5, normed = True)
[n3,bins3,patches] = ax3.hist(dataset1['c'], bins=50, color = 'red',alpha = 0.5, normed = True)
[n4,bins4,patches] = ax4.hist(dataset1['x0'], bins=50, color = 'red',alpha = 0.5, normed = True)
dx = bins4[1] - bins4[0]
CDF = np.cumsum(n4)*dx
ax5.plot(bins4[1:], CDF)

#####_____#####



#######Creating Radio Buttons#####

axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.015, 0.86, 0.08, 0.13], axisbg=axcolor)
radio1 = RadioButtons(rax, ('Data Set1', 'Data Set2', 'Data Set3', 'Data Set 1&2', 'Data Set 1&3', 'Data Set 2&3', 'Data Set 1,2&3'))

#####_____#####



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

radio1_label = 'Data Set1'
func = {'Data Set1': dataset1, 'Data Set2': dataset2, 'Data Set3': dataset3, 'Data Set 1&2': dataset1, 'Data Set 1&3': dataset1, 'Data Set 2&3': dataset1, 'Data Set 1,2&3': dataset1}
axcl = {'Data Set1': 'red', 'Data Set2': 'blue', 'Data Set3': 'green', 'Data Set 1&2': None, 'Data Set 1&3': None, 'Data Set 2&3': None, 'Data Set 1,2&3': None}

def update_radio1(label):
    global radio1_label             #so we can overwrite the variable defined above and not create a local one
    radio1_label = label
    print radio1_label
    ax1.clear()
    ax2.clear()
    ax3.clear()
    ax4.clear()
    [n1,bins1,patches] = ax1.hist(func[radio1_label]['a'], bins=50, color = axcl[radio1_label], normed = True, alpha = 0.5)
    [n2,bins2,patches] = ax2.hist(func[radio1_label]['b'], bins=50, color = axcl[radio1_label], normed = True, alpha = 0.5)
    [n3,bins3,patches] = ax3.hist(func[radio1_label]['c'], bins=50, color = axcl[radio1_label], normed = True, alpha = 0.5)
    [n4,bins4,patches] = ax4.hist(func[radio1_label]['x0'], bins=50, color = axcl[radio1_label], normed = True, alpha = 0.5)   
    ax1.grid(True)
    ax2.grid(True)
    ax3.grid(True)
    ax4.grid(True)
    ax5.grid(True)
    if radio1_label == 'Data Set 1&2':
        ax1.clear()
        ax2.clear()
        ax3.clear()
        ax4.clear()
        ax1.grid(True)
        ax2.grid(True)
        ax3.grid(True)
        ax4.grid(True)
        ax5.grid(True)
        [n1,bins1,patches] = ax1.hist(dataset1['a'], bins=50, color = 'red', normed = True, alpha = 0.5)
        [n2,bins2,patches] = ax2.hist(dataset1['b'], bins=50, color = 'red', normed = True, alpha = 0.5)
        [n3,bins3,patches] = ax3.hist(dataset1['c'], bins=50, color = 'red', normed = True, alpha = 0.5)
        [n4,bins4,patches] = ax4.hist(dataset1['x0'], bins=50, color = 'red', normed = True, alpha = 0.5)

        [n1,bins1,patches] = ax1.hist(dataset2['a'], bins=50, color = 'blue', normed = True, alpha = 0.5)
        [n2,bins2,patches] = ax2.hist(dataset2['b'], bins=50, color = 'blue', normed = True, alpha = 0.5)
        [n3,bins3,patches] = ax3.hist(dataset2['c'], bins=50, color = 'blue', normed = True, alpha = 0.5)
        [n4,bins4,patches] = ax4.hist(dataset2['x0'], bins=50, color = 'blue', normed = True, alpha = 0.5)
    if radio1_label == 'Data Set 1&3':
        ax1.clear()
        ax2.clear()
        ax3.clear()
        ax4.clear()
        ax1.grid(True)
        ax2.grid(True)
        ax3.grid(True)
        ax4.grid(True)
        ax5.grid(True)
        [n1,bins1,patches] = ax1.hist(dataset1['a'], bins=50, color = 'red', normed = True, alpha = 0.5)
        [n2,bins2,patches] = ax2.hist(dataset1['b'], bins=50, color = 'red', normed = True, alpha = 0.5)
        [n3,bins3,patches] = ax3.hist(dataset1['c'], bins=50, color = 'red', normed = True, alpha = 0.5)
        [n4,bins4,patches] = ax4.hist(dataset1['x0'], bins=50, color = 'red', normed = True, alpha = 0.5)

        [n1,bins1,patches] = ax1.hist(dataset3['a'], bins=50, color = 'green', normed = True, alpha = 0.5)
        [n2,bins2,patches] = ax2.hist(dataset3['b'], bins=50, color = 'green', normed = True, alpha = 0.5)
        [n3,bins3,patches] = ax3.hist(dataset3['c'], bins=50, color = 'green', normed = True, alpha = 0.5)
        [n4,bins4,patches] = ax4.hist(dataset3['x0'], bins=50, color = 'green', normed = True, alpha = 0.5)
    if radio1_label == 'Data Set 2&3':
        ax1.clear()
        ax2.clear()
        ax3.clear()
        ax4.clear()
        ax1.grid(True)
        ax2.grid(True)
        ax3.grid(True)
        ax4.grid(True)
        [n1,bins1,patches] = ax1.hist(dataset2['a'], bins=50, color = 'blue', normed = True, alpha = 0.5)
        [n2,bins2,patches] = ax2.hist(dataset2['b'], bins=50, color = 'blue', normed = True, alpha = 0.5)
        [n3,bins3,patches] = ax3.hist(dataset2['c'], bins=50, color = 'blue', normed = True, alpha = 0.5)
        [n4,bins4,patches] = ax4.hist(dataset2['x0'], bins=50, color = 'blue', normed = True, alpha = 0.5)

        [n1,bins1,patches] = ax1.hist(dataset3['a'], bins=50, color = 'green', normed = True, alpha = 0.5)
        [n2,bins2,patches] = ax2.hist(dataset3['b'], bins=50, color = 'green', normed = True, alpha = 0.5)
        [n3,bins3,patches] = ax3.hist(dataset3['c'], bins=50, color = 'green', normed = True, alpha = 0.5)
        [n4,bins4,patches] = ax4.hist(dataset3['x0'], bins=50, color = 'green', normed = True, alpha = 0.5)
    if radio1_label == 'Data Set 1,2&3':
        ax1.clear()
        ax2.clear()
        ax3.clear()
        ax4.clear()
        ax1.grid(True)
        ax2.grid(True)
        ax3.grid(True)
        ax4.grid(True)
        [n1,bins1,patches] = ax1.hist(dataset1['a'], bins=50, color = 'red', normed = True, alpha = 0.5)
        [n2,bins2,patches] = ax2.hist(dataset1['b'], bins=50, color = 'red', normed = True, alpha = 0.5)
        [n3,bins3,patches] = ax3.hist(dataset1['c'], bins=50, color = 'red', normed = True, alpha = 0.5)
        [n4,bins4,patches] = ax4.hist(dataset1['x0'], bins=50, color = 'red', normed = True, alpha = 0.5)

        [n1,bins1,patches] = ax1.hist(dataset2['a'], bins=50, color = 'blue', normed = True, alpha = 0.5)
        [n2,bins2,patches] = ax2.hist(dataset2['b'], bins=50, color = 'blue', normed = True, alpha = 0.5)
        [n3,bins3,patches] = ax3.hist(dataset2['c'], bins=50, color = 'blue', normed = True, alpha = 0.5)
        [n4,bins4,patches] = ax4.hist(dataset2['x0'], bins=50, color = 'blue', normed = True, alpha = 0.5)

        [n1,bins1,patches] = ax1.hist(dataset3['a'], bins=50, color = 'green', normed = True, alpha = 0.5)
        [n2,bins2,patches] = ax2.hist(dataset3['b'], bins=50, color = 'green', normed = True, alpha = 0.5)
        [n3,bins3,patches] = ax3.hist(dataset3['c'], bins=50, color = 'green', normed = True, alpha = 0.5)
        [n4,bins4,patches] = ax4.hist(dataset3['x0'], bins=50, color = 'green', normed = True, alpha = 0.5)



    plt.draw()



radio1.on_clicked(update_radio1)

#####_____#####




plt.show()
非常感谢您的帮助


提前谢谢。

先生,您是救世主。它正是我想要它做的。非常感谢您在这方面的专家协助。我现在将通过代码来了解您所做的工作。再次感谢!!:-)
import numpy as np
print np.__version__
import scipy
print scipy.__version__
import matplotlib
print matplotlib.__version__
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons


#####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.13,right=0.99,bottom=0.05)

ax1 = fig.add_subplot(331)                                                  #Subplot 1
ax1.set_xlabel('a' , fontsize = 14)

ax2 = fig.add_subplot(334)                                                  #Subplot 2
ax2.set_xlabel('b', fontsize = 14)

ax3 = fig.add_subplot(337)                                                  #Subplot 3
ax3.set_xlabel('c', fontsize = 14)

ax4 = fig.add_subplot(132)                                                  #Subplot 4
ax4.set_xlabel('x0', fontsize = 14)
ax4.set_ylabel('PDF', fontsize = 14)

ax5 = fig.add_subplot(133)                                                  #Subplot 5
ax5.set_xlabel('x0', fontsize = 14)
ax5.set_ylabel('CDF', fontsize = 14)
ax5.grid(True)

#####_____#####

radio1_label = 'Data Set1'

properties = [
    ['Data Set1', dataset1, 'red'],
    ['Data Set2', dataset2, 'blue'],
    ['Data Set3', dataset3, 'green']
]
func = {}
axcl = {}
flags = {}
for p in properties:
    func[p[0]] = p[1]
    axcl[p[0]] = p[2]
    flags[p[0]] = p[0] == 'Data Set1'

#####Plotting Distributions#####
def plot_hists():
    ax1.clear()
    ax2.clear()
    ax3.clear()
    ax4.clear()
    for p in properties:
        lbl = p[0]
        ax1.hist(func[lbl]['a'], bins=50, color=axcl[lbl], normed=True, alpha=0.5, visible=flags[lbl])
        ax2.hist(func[lbl]['b'], bins=50, color=axcl[lbl], normed=True, alpha=0.5, visible=flags[lbl])
        ax3.hist(func[lbl]['c'], bins=50, color=axcl[lbl], normed=True, alpha=0.5, visible=flags[lbl])
        n4, bins4, patches = ax4.hist(func[lbl]['x0'], bins=50,color=axcl[lbl], normed=True, alpha=0.5, visible=flags[lbl])
    ax1.grid(True)
    ax2.grid(True)
    ax3.grid(True)
    ax4.grid(True)
    return n4, bins4

n4, bins4 = plot_hists()
dx = bins4[1] - bins4[0]
CDF = np.cumsum(n4)*dx
ax5.plot(bins4[1:], CDF)

#######Creating Check Buttons#####

axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.015, 0.86, 0.08, 0.13], axisbg=axcolor)

check = CheckButtons(rax, ('Data Set1', 'Data Set2', 'Data Set3'), (True, False, False))
#####_____#####



#####Updating Check Button#####

def update_check1(label):
    global radio1_label             #so we can overwrite the variable defined above and not create a local one
    radio1_label = label
    print radio1_label

    flags[label] = not flags[label]

    plot_hists()
    ax5.grid(True)

    plt.draw()

check.on_clicked(update_check1)
#####_____#####




plt.show()