How to Draw a Line with Matplotlib

How to draw a line with matplotlib?

As of matplotlib 3.3, you can do this with plt.axline((x1, y1), (x2, y2)).

How to draw lines between mouseclicks on a matplotlib plot?

The API says:
**line_kwargs (kwargs) – Line2D objects (from ax.plot) are used to generate the markers. line_kwargs will be passed through to all of the ax.plot calls.

So, let's set the linestyle:

klicker = clicker(ax, ["event"], markers=["x"], **{"linestyle": "--"})

Sample output:

Sample Image

Other Line2D keywords can be found here.

How to draw lines on a plot of points in python

The code would not plot properly since plt.plot and plt.show() is looping. You might want to store your update i & y values in a list before you plot it.

import matplotlib.pyplot as plt
x = []
y = []
for i in range(1, 10):
x.append(i)
y.append(i**2)

plt.plot(x,y)
plt.show()

Plot a horizontal line on a given plot

You are correct, I think the [0,len(xs)] is throwing you off. You'll want to reuse the original x-axis variable xs and plot that with another numpy array of the same length that has your variable in it.

annual = np.arange(1,21,1)
l = np.array(value_list) # a list with 20 values
spl = UnivariateSpline(annual,l)
xs = np.linspace(1,21,200)
plt.plot(xs,spl(xs),'b')

#####horizontal line
horiz_line_data = np.array([40 for i in xrange(len(xs))])
plt.plot(xs, horiz_line_data, 'r--')
###########plt.plot([0,len(xs)],[40,40],'r--',lw=2)
pylab.ylim([0,200])
plt.show()

Hopefully that fixes the problem!

Draw a line between points, ignoring missing data, with matplotlib

You can use a simple function to keep only the valid data:

x=[0, 0.02, 0.05, 0.08, 0.11, 0.14]
y1=[31.15, None, 15.24, 11.65, 13.54, 9.55]
y2=[20.3, 14.2, 5.6, 3.10, 8.8, 10.45]

def dropnone(X, Y):
return zip(*((a,b) for a,b in zip(X,Y) if None not in [a,b]))

plt.plot(*dropnone(x, y1), linestyle='-',marker='o',color='red')
plt.plot(*dropnone(x, y2), linestyle='-',marker='o',color='blue')

output:

Sample Image

How to draw a line between a data point and an axis in matplotlib?

In my opinion, this is simple enough (4 lines):

fig, ax = plt.subplots()
x = np.arange(10)
y = np.arange(10)
ax.plot(x, y)
# starting from here
ymin, ymax = ax.get_ylim()
for i in range(len(x)):
ax.vlines(x[i], ymin, y[i])
ax.set_ylim(ymin, ymax)

Output:

Sample Image



Related Topics



Leave a reply



Submit