Use Line2D to plot line in python

后端 未结 3 1498
予麋鹿
予麋鹿 2021-01-02 10:08

I have the data:

x = [10,24,23,23,3]
y = [12,2,3,4,2]

I want to plot it using

matplotlib.lines.Line2D(xdata, ydata)

I use

3条回答
  •  爱一瞬间的悲伤
    2021-01-02 10:43

    You should add the line to a plot and then show it:

    In [13]: import matplotlib.pyplot as plt
    
    In [15]: from matplotlib.lines import Line2D      
    
    In [16]: fig = plt.figure()
    
    In [17]: ax = fig.add_subplot(111)
    
    In [18]: x = [10,24,23,23,3]
    
    In [19]: y = [12,2,3,4,2]
    
    In [20]: line = Line2D(x, y)
    
    In [21]: ax.add_line(line)
    Out[21]: 
    
    In [22]: ax.set_xlim(min(x), max(x))
    Out[22]: (3, 24)
    
    In [23]: ax.set_ylim(min(y), max(y))
    Out[23]: (2, 12)
    
    In [24]: plt.show()
    

    The result:

    enter image description here

提交回复
热议问题