简体   繁体   English

在Matplotlib中,如何避免axvspan重叠?

[英]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. 我试过“capstyle ='butt'”,我曾经避免使用线条之间的重叠,但是这里没有做到这一点。

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()

The problem you have is not due to line caps, since axvspan draws a polygon. 你遇到的问题不是由于线帽,因为axvspan绘制了一个多边形。 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: 因此,要获得没有“边界”的区域,请将线宽( lw )设置为零:

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()

Instead of color , use facecolor inside axvspan . 相反的color ,使用facecoloraxvspan The solution by @heltonbiker works great, it gets rid of the width of the border. @heltonbiker的解决方案效果很好,它摆脱了边框的宽度。 But, at least in the matplotlib version I'm using, 2.0.0, using just facecolor doesn't draw the border. 但是,至少在我使用的matplotlib版本2.0.0中,仅使用facecolor不会绘制边框。

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: 仅使用facecolor将不会绘制边框: 在此输入图像描述

Using color will fill the rectangle and will draw a border: 使用color将填充矩形并绘制边框: 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM