Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 845
heracho Avatar asked Jan 27 '17 16:01

heracho


2 Answers

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()
like image 138
heltonbiker Avatar answered Oct 30 '22 06:10

heltonbiker


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

like image 27
Pablo Reyes Avatar answered Oct 30 '22 06:10

Pablo Reyes