简体   繁体   English

使用Python / Matplotlib更新图-交互式图

[英]Update figure with Python/Matplotlib - interactive plot

I'm trying to create an interactive figure with Python/Matplotlib which simply shifts a circle to the right upon a button click. 我正在尝试使用Python / Matplotlib创建一个交互式图形,该图形只需单击按钮即可向右移动一个圆圈。 My code is partially working in that pressing the button updates the circle coordinates and shifts the circle to the right on the plot. 我的代码部分起作用,因为按下按钮可以更新圆坐标,并将圆向右移到绘图上。 However the old circles remain plotted, which is not what I want. 但是,旧的圆圈仍然保留,这不是我想要的。 I want there to only be the new, updated circle plotted on the graph. 我只希望图表上绘制新的更新圆。

This is what currently happens before button click: 这是单击按钮之前当前发生的情况:

img

and after a couple button clicks: 然后单击几个按钮:

img

I have tried using plt.draw() but have not been very successful. 我曾尝试使用plt.draw()但不是很成功。

I first define my axes: 我首先定义我的轴:

"Create Figure and plot circle Subplot"
figure1 = plt.figure()                                    # Main Figure to contain all subplots and button panel
CircSubplot = figure1.add_subplot(1,1,1, aspect= 'equal')

# -- Axes with centred spines --
CircSubplot.spines['left'].set_position('center')
CircSubplot.spines['right'].set_color('none')
CircSubplot.spines['bottom'].set_position('center')
CircSubplot.spines['top'].set_color('none')
CircSubplot.spines['left'] #.set_smart_bounds(True)
CircSubplot.spines['bottom'] #.set_smart_bounds(True)
CircSubplot.xaxis.set_ticks_position('bottom')
CircSubplot.yaxis.set_ticks_position('left')

# -- Sets scale for axes --
axesScaleFactor = 3         # Sets the scale of the axis wrt to circle radius
axCircSubplot = plt.gca()   # get current axes object
axCircSubplot.set_xlim([-circRadius*axesScaleFactor,circRadius*axesScaleFactor])    #set x-axes range
axCircSubplot.set_ylim([-circRadius*axesScaleFactor,circRadius*axesScaleFactor])    #set y-axes range 

and then later I define a callback function which runs when the button is pressed: 然后再定义一个回调函数,该函数在按下按钮时运行:

class Index(object):
    xinc = 0

    def right(self, event):
        self.xinc += 1
        i = self.xinc
        circCentre = [i,0] #Increments circle x co-ordinates by 1
        UpdatedCircVec = CircleVector(circCentre[0],circCentre[1],circRadius,circPoints)     #CircleVector function returns a 2D array containing X and Y co-ordinates for the circle 
        Upper = CircUpper(UpdatedCircVec)          #Function splits the CircleVector into portions for upper circle segment
        Lower = CircLower(UpdatedCircVec)          #Function splits the CircleVector into portions for lower circle segment
        CircSubplot.plot(Upper[0],Upper[1],'b')     #Plot upper circle segment
        CircSubplot.plot(Lower[0],Lower[1],'b')     #Plot lower circle segment
        plt.draw()

Any help would be appreciated. 任何帮助,将不胜感激。

Clear the axes before you plot the newly shifted circle. 在绘制新移动的圆之前,请清除轴。

There are a few ways to do this, but the easiest will be to insert a plt.cla() before you plot the new data. 有几种方法可以做到这一点,但是最简单的方法是在绘制新数据之前插入一个plt.cla() This should give you a clean "current axes." 这应该给您一个干净的“当前轴”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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