heracho
heracho

Reputation: 610

In Matplotlib, How to avoid axvspan overlap?

I noticed that two adjacent areas overlap, so in the middle appear an annoying line. I tried "capstyle = 'butt'" which I used to avoid overlap between lines, but here it doesn't do the job.

here's a minimal example:

import matplotlib.pylab as plt

ax = plt.subplot(111)

ax.axvspan(0, 0.5, color = 'red', alpha = 0.13, capstyle = 'butt')
ax.axvspan(0.5, 1, color = 'blue', alpha = 0.13, capstyle = 'butt')

plt.show()

Upvotes: 11

Views: 6509

Answers (2)

Pablo Reyes
Pablo Reyes

Reputation: 3123

Instead of color, use facecolor inside axvspan. The solution by @heltonbiker works great, it gets rid of the width of the border. But, at least in the matplotlib version I'm using, 2.0.0, using just facecolor doesn't draw the border.

import matplotlib.pylab as plt

fig,ax = plt.subplots()

ax.axvspan(0, 0.5, facecolor = 'red', alpha = 0.13)
ax.axvspan(0.5, 1, facecolor = 'blue', alpha = 0.13)

plt.show()

Using only facecolor will no draw the border: enter image description here

Using color will fill the rectangle and will draw a border: enter image description here

Upvotes: 10

heltonbiker
heltonbiker

Reputation: 27575

The problem you have is not due to line caps, since axvspan draws a polygon. The problem is that this polygon, by default, has a border with a given linewidth (one pixel, I suppose).

So, to get just the areas without that "border", set the linewidth (lw) to zero:

import matplotlib.pylab as plt

ax = plt.subplot(111)

ax.axvspan(0, 0.5, color = 'red', alpha = 0.13, lw=0)
ax.axvspan(0.5, 1, color = 'blue', alpha = 0.13, lw=0)

plt.show()

Upvotes: 16

Related Questions