Home > Back-end >  Matplotlib fill_between line visible although no difference at certain point
Matplotlib fill_between line visible although no difference at certain point

Time:11-21

I have a problem with fill_between. When I plot 2 invisible lines and fill between them, then there is always a thin line visible, although the values at this point are the same. So I would expect to see nothing there (see picture). As you can see, there is a red thin line visible, although I can tell you that both values are exactly the same. Any ideas?

Thank you!

enter image description here

Code to reproduce:

import matplotlib.pyplot as plt

y1 = [10, 18, 17, 19, 11, 13, 15, 15, 13, 15, 10, 19, 18, 10, 16, 12, 12, 13, 10, 10]
y2 = [22, 29, 30, 19, 11, 13, 15, 29, 30, 31, 22, 37, 31, 26, 28, 23, 25, 23, 27, 28]
plt.fill_between(range(20), y1, color='blue')
plt.fill_between(range(20), y1, y2, color='red')

CodePudding user response:

You could change the linewidth to 0, or set the edgecolor to edgecolor='none'. Or you could use a common edgecolor for both calls.

import matplotlib.pyplot as plt
import numpy as np

# create some dummy test data
x = np.linspace(0, 1, 20)
y1 = np.random.rand(20)   1
y2 = np.random.rand(20)   1
y2[10:15] = 0  # fill a part with zeros

fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(14, 4), sharex=True, sharey=True)

ax1.fill_between(x, 0, y1, color='skyblue')
ax1.fill_between(x, y1, y1   y2, color='tomato')
ax1.set_title('default fill_between')

ax2.fill_between(x, 0, y1, color='skyblue', linewidth=0)
ax2.fill_between(x, y1, y1   y2, color='tomato', linewidth=0)
ax2.set_title('linewidth=0')

ax3.fill_between(x, 0, y1, color='skyblue')
ax3.fill_between(x, y1, y1   y2, color='tomato', edgecolor='black')
ax3.set_title("edgecolor='black'")
ax1.margins(x=0, y=0)

plt.tight_layout()
plt.show()

fill_between and linewidth

  • Related