Home »
Python »
Python Data Visualization
Python | Cap Styles for Lines
In this article, we are going to explore available cap styles in matplotlib in Python.
Submitted by Anuj Singh, on August 27, 2020
There are three different cap styles that are offered by matplotlib in python. These are round, butt, and projects. The projecting cap style is set as default and does not need any external command. Apart from projecting linestyle, we need to explicitly use the command (as shown in the code) for butt and round cap styles.
The following figure shows the three cap styles as an illustration.
Illustrations:
Python code for cap styles for lines
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(8, 2))
ax.set_title('Cap Styles for Lines')
for x, style in enumerate(['projecting', 'butt', 'round']):
ax.text(x+0.25, 0.5, style, ha='center', fontsize=15)
xx = [x, x+0.5]
yy = [0, 0]
ax.plot(xx, yy, lw=24, color='tab:blue', solid_capstyle=style)
ax.plot(xx, yy, lw=1, color='black')
ax.plot(xx, yy, 'o', color='tab:red', markersize=3)
ax.set_ylim(-.5, 1.5)
ax.set_axis_off()
Output:
Output is as Figure