Issue
Example code:
fig, ax = plt.subplots()
ax.vlines(x=0.6, ymin=0.2, ymax=0.8, linewidth=10)
ax.hlines(y=0.2, xmin=0.2, xmax=0.6, linewidth=10)
ax.hlines(y=0.8, xmin=.6, xmax=0.8, linewidth=10)
ax.vlines(x=0.8, ymin=0.8, ymax=1.1, linewidth=10)
ax.hlines(y=1.1, xmin=.5, xmax=0.8, linewidth=10)
ax.vlines(x=0.5, ymin=0.4, ymax=1.1, linewidth=10)
ax.hlines(y=.4, xmin=.2, xmax=0.5, linewidth=10)
Which produces:
In the corners are gaps though, whereas I would like these to be flush.
To be clear, the corners currently look like:
Whereas I would like them to look like:
# Edit
Side note - if there's just a rectangle to be created the following can be used:
import matplotlib.patches as pt
fig, ax = plt.subplots()
frame = pt.Rectangle((0.2,0.2),0.4,0.6, lw=10,facecolor='none', edgecolor='k')
ax.add_patch(frame)
More info on the edit here:
- How to draw a rectangle over a specific region in a matplotlib graph
- matplotlib: how to draw a rectangle on image
- Drawing rectangle with border only in matplotlib
Solution
Apart from plotting the lines as one segmented curve, an idea is to put a scatter dot at the start and end of each line segment. The size of a scatter dot is measured quadratic; a line width of 10 corresponds to a scatter size of about 70. Matplotlib also supports capstyle='round'
, which starts and ends the lines with a rounded cap. (For ax.plot
, the parameter is named solid_capstyle='round', while for
ax.vlinesit is just
capstyle='round').`
Other allowed values for capstyle
are 'projecting'
(extending the line with half its thickness) and 'butt'
(stops the lines at the end point).
import matplotlib.pyplot as plt
fig, axs = plt.subplots(ncols=3, figsize=(15, 4))
for ax, capstyle in zip(axs, ['round', 'projecting', 'butt']):
ax.vlines(x=0.6, ymin=0.2, ymax=0.8, linewidth=10, capstyle=capstyle)
ax.hlines(y=0.2, xmin=0.2, xmax=0.6, linewidth=10, capstyle=capstyle)
ax.hlines(y=0.8, xmin=.6, xmax=0.8, linewidth=10, capstyle=capstyle)
ax.vlines(x=0.8, ymin=0.8, ymax=1.1, linewidth=10, capstyle=capstyle)
ax.hlines(y=1.1, xmin=.5, xmax=0.8, linewidth=10, capstyle=capstyle)
ax.vlines(x=0.5, ymin=0.4, ymax=1.1, linewidth=10, capstyle=capstyle)
ax.hlines(y=.4, xmin=.2, xmax=0.5, linewidth=10, capstyle=capstyle)
ax.set_title(f"capstyle='{capstyle}'")
plt.show()
Answered By - JohanC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.