WXC
WXC

Reputation: 1

How can I add the Line2D object to the figure in matplotlib

import matplotlib.pyplot as plt
from matplotlib.lines import Line2D   

line = plt.Line2D([1], [1])
plt.show() #it's doing nothing

Upvotes: 0

Views: 842

Answers (1)

Michael S.
Michael S.

Reputation: 3130

Are you just trying to draw a straight line? Or are you trying to plot a straight line?

To just plot a straight line do:

import matplotlib.pyplot as plt
plt.plot([1,1,1,1,1,1,1])
plt.show()

Output

enter image description here If you are set on using Line2D then you can do something like this, your problem is that you need more than 1 value for x or y in the line 2D (1 x value and 1 y value just makes a point):

from matplotlib.lines import Line2D   

x = [0,1,2,3,4]
y = [1]
fig,ax = plt.subplots()
line = Line2D(x, y)
ax.add_line(line)
plt.xlim(min(x), max(x))
plt.ylim(0, 2)
plt.show()

enter image description here

Upvotes: 1

Related Questions