1

I'm new to matplotlib and I'm trying to learn how to shade the area between two horizontal lines in a bar chart, and I wanna know how I can make it reach the edges. Here is an example:

import numpy as np
import matplotlib.pyplot as plt

N = 8
A = np.random.random(N)
B = np.random.random(N)
X = np.arange(N)

plt.bar(X, A, color = 'b')
plt.fill_between(X,0.4,0.6, facecolor = 'thistle', zorder = 2, alpha = 0.5)
plt.show()

This shows this: image

Matsukazi Issy
  • 109
  • 2
  • 8

1 Answers1

0

Not sure if this is the "correct" way to do this, but one option:

width=0.8
plt.bar(X, A, color = 'b', width=width)

X = X.astype(float)
X[0] -= width/2
X[-1] += width/2

plt.fill_between(X,0.4,0.6, facecolor = 'thistle', zorder = 2, alpha = 0.5)

Output:

enter image description here

BigBen
  • 46,229
  • 7
  • 24
  • 40