Question

I have some code to animate a scatterplot in matplotlib, everything runs smoothly, except that I end up with two output graphs at the end. One contains the desired animation, while the other is blank.

While it is probably a simple issue someone, I cannot see why, nor figure out why I am getting two graphs. Can anyone see what is giving me the "bogus" empty graph? Cheers.

    import math, numpy, time, matplotlib, pyvisa
from pyvisa import visa
from matplotlib import pyplot, animation
import os.path

class Realtime_measurement_and_scatter_plot:


    def __init__(......):  

        self.animaiton = animation.FuncAnimation(self.figure, self.take_new_data_and_update_graph, init_func=self.initialise_graph, frames = self.number_of_measurements, interval=100, blit=False)





    def initialise_graph(self):

        self.figure = matplotlib.pyplot.figure()

        self.axes = matplotlib.pyplot.axes()

        self.axes.set_title("Realtime Plot")

        self.x_data = []
        self.y_data = []

        self.scatterplot = matplotlib.pyplot.scatter(self.x_data, self.y_data)
        return self.scatterplot,

    def take_new_data_and_update_graph(self, i):
...




        averaged_voltage = numpy.average(measured_voltages )
        new_time = float(i) * float( self.time_between_measurements )

        self.x_data = numpy.append( self.x_data, averaged_voltage )
        self.y_data = numpy.append( self.y_data , new_time )

        self.scatterplot = matplotlib.pyplot.scatter(self.x_data, self.y_data)



        return self.scatterplot,


   animation_instance = Realtime_measurement_and_scatter_plot(1000000, 1 , 1, 6, "Super_FUN_TEST.0",22)
   matplotlib.pyplot.show()    
Was it helpful?

Solution

tcaswell's comment about "init uses attributes you don't create until initialize_graph" prompted me to fix that. Changing this so that init creates the figure, axes ect instead of them being created in intialize_graph removes the two output graphs problem; solving my problem!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top