0

I have encountered some weird shifting of the filled area when using the plt.fill_between() from pythons matplotlib.pyplot.

The following is my code:

#!/usr/bin/env python3

from matplotlib import pyplot as plt
import numpy as np

perf_range = np.linspace(0, 17, 17)

print(perf_range)

hsm_low     = [22000 for i in perf_range]
hsm_high    = [35000 for i in perf_range]

system_base_price = 3000
system_low  = 2 * system_base_price * perf_range
system_high = 0.5 * system_base_price * perf_range

ax = plt.gca()
ax.set_xscale('log')


plt.plot(hsm_low)
plt.plot(hsm_high)
plt.plot(system_low)
plt.plot(system_high)

plt.fill_between(perf_range, system_low, system_high, color='green', alpha=0.5)
plt.show()

and I get the following plot:

enter image description here

First of all, as you can see, the green area is kind of shifted and does not really match the green and read line. Furthermore, I would like the green area be between the green, red and blue line. Any help?

Dr3w Br1ck13
  • 187
  • 1
  • 5
  • 14

1 Answers1

0

You forgot the x argument to plt.plot(x,y).

plt.plot(perf_range,hsm_low)
plt.plot(perf_range,hsm_high)
plt.plot(perf_range,system_low)
plt.plot(perf_range,system_high)

plt.fill_between(perf_range, system_low, system_high, color='green', alpha=0.5)

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thanks a lot! Any hint on how to restrict the area to the blue line? – Dr3w Br1ck13 Mar 13 '18 at 13:07
  • Not sure what you mean. The blue line is at 22000. The red line is at 1500. It's your data and only you know about its meaning. You may use mathematics to shift them whereever you want them to be. – ImportanceOfBeingErnest Mar 13 '18 at 13:13
  • No I mean to border the filled area with the green, the red and the blue line. So far, it is only bordered by the green and the red line. – Dr3w Br1ck13 Mar 13 '18 at 13:15
  • I see. That's a totally different question, which has an [answer here](https://stackoverflow.com/questions/16417496/matplotlib-fill-between-multiple-lines). – ImportanceOfBeingErnest Mar 13 '18 at 13:19