pythonmatplotlib

How to find out what set_data expects


I'm trying to make an animation with matplotlib, in this case a 3D scatter plot. I'm hitting a problem that I absolutely always hit when I try to do this, which is that I don't know what arguments I should pass to set_data, and I don't know how to find out. In this case, it apparently expects two arguments, despite it being a 3d plot.

Since I've experienced related problems often, rather than asking about the specifics of the particular plot I'm trying to animate, I will ask the general question: given an element of a MatPlotLib plot, how can I determine what arguments its set_data method expects, either by interrogating it, or by knowing where it's documented?


Solution

  • From an example for an Animated 3D random walk from the MatPlotLib documentation:

    def update_lines(num, dataLines, lines):
        for line, data in zip(lines, dataLines):
            # NOTE: there is no .set_data() for 3 dim data...
            line.set_data(data[0:2, :num])
            line.set_3d_properties(data[2, :num])
        return lines
    

    So as confusing as you discovered it is set_data by itself is not meant for 3D data, as well as according to the docs it accepts:

    2D array (rows are x, y) or two 1D arrays

    Looking more at this example we can see that the set_3d_properties has been used altogether.

    This whole update_lines was set as a callback parameter for animation.FuncAnimation.