Matplotlib: contour plot with slider widget

后端 未结 1 1203
别那么骄傲
别那么骄傲 2021-01-13 00:15

Newbie matplotlib user here. I\'m trying to use a slider to adjust a parameter in a contour plot, but when I do so, I get:

AttributeError: QuadContourSet in         


        
相关标签:
1条回答
  • 2021-01-13 00:41

    The problem is that the QuadContourSet object has no way to update its data, since if you change the data arbitrarily, the whole thing needs to be recomputed. I don't know if there is something about your particular way of generating data that would lend itself to a simpler way to modify the contour lines, but if not, I think what you need to do is to plot the contours from scratch:

    # After your "Define model parameters" block
    
    def compute_and_plot(ax, alpha):
        #Calculate grid values
        V, W = np.meshgrid(v,w)
        Z = (V**(beta))*(W**(1-beta))
        X = x_bar + a + b*Z
        U = alpha*np.log(V) + (1-alpha)*np.log(X) - c*(W+V)
    
        CS = QuadContourSet(ax, V, W, U, 200)
        pyl.clabel(CS, inline=1, fontsize=10)
    
    # Plot
    fig = pyl.figure()
    pyl.title('Simplest default with labels')
    ax = fig.add_subplot(221)
    compute_and_plot(ax, alpha)
    
    #Define slider for alpha
    axcolor = 'lightgoldenrodyellow'
    alpha_axis  = pyl.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)
    alpha_slider = Slider(alpha_axis, 'Amp', 0, 1, valinit=.5)
    
    def update(ax, val):
        alpha = alpha_slider.val
        ax.cla()
        compute_and_plot(ax, alpha)
        pyl.draw()
    
    alpha_slider.on_changed(lambda val: update(ax, val))
    
    pyl.show()
    
    0 讨论(0)
提交回复
热议问题