21

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 tried:

import matplotlib.lines
matplotlib.lines.Line2D(x, y)

But how do I show the line?

5 Answers 5

30

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

The result:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I get a can not put single artist in more than one figure error when adding a line coming from another plot to ax
11

The more common approach (not exactly what the questioner asked) is to use the plot interface. This involves Line2D behind the scenes.

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

Comments

1

I have run into this problem when trying to replicate a line on two different plots. (mentioned in a comment "cannot put single artist in more than one figure) So assuming you already have a Line2D object from some other source and need it on new plot, the best way to add it to your plot is with:

line = Line2D(x, y)
plt.plot(*line.get_data(), ...)

You can also get a lot of the line's properties from its other "get" methods, found here.

Comments

0

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

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

If you want to add a Line2D instance to an existing Axes instance ax:

ax.add_line(line)

See matplotlib.axes.Axes.add_line. This will use all attributes of the line, like width and color.

If you don't have an Axes instance, you can create one on an existing Figure instance fig using:

ax = fig.add_axes((x, y, w, h))

See matplotlib.figure.Figure.add_axes. The tuple is the position in figure coordinates: x and y between 0 and 1, w and h whatever size, also in figure coordinate system.

If you don't have a figure, you can create one, or just use the static dedicated method:

new_line = plt.plot(*line.get_data())

See matplotlib.pyplot.plot. This gets the tuple (xs, ys) describing the points of the Line2D and use it to create a new Line2D inserted into a new Axes of a new Figure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.