1

I can't figure how to plot a continuous function using Matplotlib. I got how to plot a scatterplot, but I would like a continuous plot.

Here is my code:

import matplotlib.pyplot as plt
from matplotlib.pyplot import autoscale
import matplotlib.animation as animation

import numpy


class MyPlot():
    def __init__(self):
        self.index = 0
        self.setup()

    def setup(self):
        plt.ion()
        self.fig, self.ax = plt.subplots()
        self.line = self.ax.plot([],[])
        autoscale()
        plt.show()
    def anim(self, i):
        self.line.set_ydata(i)  # update the data
        return self.line,
    def add(self, val):
        print self.index, val
        self.ax.plot(self.index, val)
        animation.FuncAnimation(self.fig, self.anim, repeat=False)
        plt.pause(0.05)
        #if(self.index >= ntests):
        self.index+=1

if __name__== "__main__":
    import time
    from random import random
    p = MyPlot()

    for i in range(100):
        p.add(random())
        time.sleep(0.5)

This works, but doesn't draw anything. The plot resizes itself, though.

evtoh
  • 444
  • 3
  • 10
  • 21
Federico Ponzi
  • 2,682
  • 4
  • 34
  • 60
  • Possible duplicate of [Dynamically updating plot in matplotlib](http://stackoverflow.com/questions/10944621/dynamically-updating-plot-in-matplotlib) – Mad Physicist Jul 27 '16 at 16:05
  • 1
    fyi matplotlib is updatable but is meant for presentation-quality graphs and not really meant for real-time updated display. You may want to look at vispy http://vispy.org/ – Jason S Jul 27 '16 at 16:07
  • @JasonS. Normally take a look at comments kinda suck because they attempt to cover the commenter's inability to see a solution to the problem. Not so with this. Thanks for the link :) – Mad Physicist Jul 27 '16 at 17:46
  • You may want to consider switching your selected answer. – Mad Physicist Jul 27 '16 at 18:52
  • @MadPhysicist I'll try your answer tomorrow as soon as I get to work and I'll eventually change the selected answer. – Federico Ponzi Jul 27 '16 at 20:12

2 Answers2

2

You are only plotting a line with a single point at a time (which doesn't exist), so nothing shows up. If you replace self.ax.plot with self.ax.scatter, it plots correctly.

If you really want lines, you can just keep track of the last index and value and plot a line connecting the last index and value with the current index and value each time.

Add these two lines to add()

    self.ax.plot([self.index-1, self.index], [self.lastval, val])
    self.lastval = val

as well as a line initializing self.lastval to numpy.nan in setup()

honi
  • 946
  • 1
  • 7
  • 18
  • Thansk for the answer! Yes as I've said in the question, I know how to plot a scatterplot. I've already thought about the single point thing, since the plot gets resized anyway. As I've said in the question, i need a continuous plot. How can I actually connect the two points? Thanks – Federico Ponzi Jul 27 '16 at 15:35
  • That's great! Thanks a lot! – Federico Ponzi Jul 27 '16 at 15:44
2

You can actually append values to a line plot in matplotlib:

self.line.set_xdata(numpy.append(self.line.get_xdata(), self.index))
self.line.set_ydata(numpy.append(self.line.get_ydata(), val))

This way, you do not have to do any of the bookkeeping yourself.

More details can be found at https://stackoverflow.com/a/10944967/2988730

Community
  • 1
  • 1
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264