matplotlib interactive graphing (manually drawing lines on a graph)

前端 未结 1 631
说谎
说谎 2021-02-03 10:29

I have succesfully plotted a set of date sequenced data (X axis is date) using matplotlib. However, I want to be able to manually draw lines from one (date1, y1

相关标签:
1条回答
  • 2021-02-03 11:23

    I would write something like this:

    import matplotlib.pyplot as plt
    class LineDrawer(object):
        lines = []
        def draw_line(self):
            ax = plt.gca()
            xy = plt.ginput(2)
    
            x = [p[0] for p in xy]
            y = [p[1] for p in xy]
            line = plt.plot(x,y)
            ax.figure.canvas.draw()
    
            self.lines.append(line)
    

    Using ginput() you can avoid more complicated event handling. The way it 'works' is you plot something:

    plt.plot([1,2,3,4,5])
    ld = LineDrawer()
    ld.draw_line() # here you click on the plot
    

    For saving/loading the line data to a file you can easily implement a method using pickle or shelve. You can also pass the necessary metadata by the method draw_line()

    0 讨论(0)
提交回复
热议问题