/ / Použite riadok2D na vykreslenie čiary v python - python

Použite riadok Line2D na vykreslenie čiary v python - python

Mám tieto údaje:

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

Chcem to napísať pomocou

matplotlib.lines.Line2D (xdata, ydata)

používam

import matplotlib.lines

matplotlib.lines.Line2D(x, y)

Ale ako zobrazím riadok?

odpovede:

7 pre odpoveď č. 1

Mali by ste pridať riadok na graf a potom ho zobraziť:

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]: <matplotlib.lines.Line2D at 0x7f4c10732f60>

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()

Výsledok:

tu zadajte popis obrázku


5 pre odpoveď № 2

Viac spoločný prístup (nie je to presne to, čo požiadal) je použiť sprisahania rozhranie. Toto zahŕňa Line2D v zákulisí.

>>> x = [10,24,23,23,3]
>>> y = [12,2,3,4,2]
>>> import matplotlib.pyplot as plt
>>> plt.plot(x,y)
[<matplotlib.lines.Line2D object at 0x7f407c1a8ef0>]
>>> plt.show()

0 pre odpoveď č. 3

V záujme pohodlia (prilepte a spustite priamo):

fig, ax = plt.subplots(figsize=(3,3))
x = [1,2,1.5,1.5]
y = [1,1,0.5,1.5]
line = Line2D(x, y)
ax.add_line(line)
ax.axis([0, 3, 0, 3])
plt.show()