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 | Cap Styles for Lines

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.