/ / Use Line2D para traçar a linha em python - python

Use Line2D para traçar a linha em python - python

Eu tenho os dados:

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

Eu quero plotá-lo usando

matplotlib.lines.Line2D (xdata, ydata)

eu uso

import matplotlib.lines

matplotlib.lines.Line2D(x, y)

Mas como mostro a linha?

Respostas:

7 para resposta № 1

Você deve adicionar a linha a um gráfico e mostrá-lo:

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

O resultado:

insira a descrição da imagem aqui


5 para resposta № 2

A abordagem mais comum (não exatamente o que o questionador perguntou) é usar o enredo interface. Isso envolve o Line2D nos bastidores.

>>> 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 para resposta № 3

Por conveniência (cole e corra diretamente):

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