Matplotlib:TypeError:' AxesSubplot'对象不可调用

时间:2016-09-07 15:53:29

标签: python matplotlib slider

我是编程的新手所以请原谅我,如果这是一个新手的错误。

我正在编写这个python脚本,我已经定义了创建分布子图的函数。

我有一个滑块小部件,供最终用户更改变量值(mu和sigma)并查看对分布的影响。这是我的代码

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



#####Calculating mean and standard deviation#####

global mu_a1
mu_a1 = 1
global mu_b1
mu_b1 = 10
global mu_c1
mu_c1 = -13
global sigma_a1
sigma_a1 =  0.14
global sigma_b1
sigma_b1 =  1.16
global sigma_c1
sigma_c1 =  2.87
mu_x01 = -11
sigma_x01 =  1.9

#####_____#####



#####Generating random data#####

a1 = 0.75*mu_a1 + (1.25 - 0.75)*sigma_a1*np.random.sample(10000)
b1 = 8*mu_b1 + (12 - 8)*sigma_b1*np.random.sample(10000)
c1 = -12*mu_c1 + 2*sigma_c1*np.random.sample(10000)
x01 = (-b1 - np.sqrt(b1**2 - (4*a1*c1)))/(2*a1)

#####_____#####



#######Creating subplots & plotting distributions#####

fig = plt.figure()
plt.subplots_adjust(left=0.13,right=0.99,bottom=0.05)

def ax1():
    ax1 = fig.add_subplot(331)
    ax1.clear()
    ax1.set_xlabel('a' , fontsize = 14)
    ax1.grid(True)
    [n1,bins1,patches] = ax1.hist(a1, bins=50, color = 'red',alpha = 0.5, normed = True)
    return ax1

def ax2():
    ax2 = fig.add_subplot(334)
    ax2.clear()
    ax2.set_xlabel('b' , fontsize = 14)
    ax2.grid(True)
    [n2,bins2,patches] = ax2.hist(b1, bins=50, color = 'red',alpha = 0.5, normed = True)
    return ax2

def ax3():
    ax3 = fig.add_subplot(337)
    ax3.clear()
    ax3.set_xlabel('c' , fontsize = 14)
    ax3.grid(True)
    [n3,bins3,patches] = ax3.hist(c1, bins=50, color = 'red',alpha = 0.5, normed = True)
    return ax3

def ax4_ax5():
    ax4 = fig.add_subplot(132)
    ax4.clear()
    ax4.set_xlabel('x0' , fontsize = 14)
    ax4.grid(True)
    [n4,bins4,patches] = ax4.hist(x01, bins=50, color = 'red',alpha = 0.5, normed = True)
    ax4.axvline(np.mean(x01), color = 'black', linestyle = 'dashed', lw = 2)

    ax5 = fig.add_subplot(133)
    ax5.clear()
    ax5.set_xlabel('x0' , fontsize = 14)
    ax5.set_ylabel('CDF', fontsize = 14)
    ax5.grid(True)
    dx = bins4[1] - bins4[0]
    CDF = np.cumsum(n4)*dx
    ax5.plot(bins4[1:], CDF, color = 'red')
    return ax4,ax5

#######_____#####

ax1 = ax1()
ax2 = ax2()
ax3 = ax3()
ax4, ax5 = ax4_ax5()

#####Creating Sliders#####

axcolor = 'lightgoldenrodyellow'
axmu_331 = plt.axes([0.015, 0.67, 0.07, 0.015], axisbg=axcolor)
axsigma_331 = plt.axes([0.015, 0.65, 0.07, 0.015], axisbg=axcolor)
smu_331 = Slider(axmu_331, 'M', -13.0 , 10.0, valinit = 1.0)
ssigma_331 = Slider(axsigma_331, 'SD', -3.0 , 3.0, valinit =1.0)

#####_____#####



#####Updating Sliders#####

def update_slider_331(val):
    mu_a1 = smu_331.val
    print mu_a1
    sigma_a1 = ssigma_331.val
    print sigma_a1
    ax1()
    ax4_ax5()
    plt.draw()

smu_331.on_changed(update_slider_331)
ssigma_331.on_changed(update_slider_331)

#####_____#####


plt.show()

我希望每次更改滑块值时都会更新分布。但是,当我更改滑块值时,我收到此错误

  Traceback (most recent call last):
  File "C:\Program Files (x86)\python27\lib\site-packages\matplotlib\backends\backend_qt4.py", line 175, in mousePressEvent
    FigureCanvasBase.button_press_event( self, x, y, button )
  File "C:\Program Files (x86)\python27\lib\site-packages\matplotlib\backend_bases.py", line 1632, in button_press_event
    self.callbacks.process(s, mouseevent)
  File "C:\Program Files (x86)\python27\lib\site-packages\matplotlib\cbook.py", line 262, in process
    proxy(*args, **kwargs)
  File "C:\Program Files (x86)\python27\lib\site-packages\matplotlib\cbook.py", line 192, in __call__
    return mtd(*args, **kwargs)
  File "C:\Program Files (x86)\python27\lib\site-packages\matplotlib\widgets.py", line 315, in _update
    self.set_val(val)
  File "C:\Program Files (x86)\python27\lib\site-packages\matplotlib\widgets.py", line 327, in set_val
    func(val)
  File "H:/UQ&M/GUI Demos/WIP/Stack Overflow_func.py", line 118, in update_slider_331
    ax1()
TypeError: 'AxesSubplot' object is not callable

我也有一个鼠标点击事件(上面的代码中未显示),我称之为' ax'功能,他们工作正常。想知道他们为什么不使用滑块更新功能。

提前致谢: - )

0 个答案:

没有答案