Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw rounded line ends using matplotlib

Say I am plotting a complex value like this:

a=-0.49+1j*1.14
plt.polar([0,angle(x)],[0,abs(x)],linewidth=5)

Giving

enter image description here

Is there a setting I can use to get rounded line ends, like the red line in the following example (drawn in paint)?

enter image description here

like image 925
atomh33ls Avatar asked May 19 '15 13:05

atomh33ls


People also ask

How do I draw a curve in matplotlib?

The matplotlib. pyplot. plot() function by default produces a curve by joining two adjacent points in the data with a straight line, and hence the matplotlib.

How do I change the shape of a line in matplotlib?

The default linestyle while plotting data is solid linestyle in matplotlib. We can change this linestyle by using linestyle or ls argument of plot() method.


1 Answers

The line proprty solid_capstyle (docs). There is also a dash_capstyle which controls the line ends on every dash.

import matplotlib.pyplot as plt
import numpy as np

x = y = np.arange(5)

fig, ax = plt. subplots()

ln, = ax.plot(x, y, lw=10, solid_capstyle='round')
ln2, = ax.plot(x, 4-y, lw=10)
ln2.set_solid_capstyle('round')
ax.margins(.2)

enter image description here

This will work equally will with plt.polar, which is a convenience method for creating a polar axes and calling plot on it, and the the Line2D object returned by it.

like image 145
tacaswell Avatar answered Oct 24 '22 13:10

tacaswell