简体   繁体   中英

matplotlib fill_between lines

I need to get the following graph :

I used:

fig_2, ax_2 = plt.subplots(figsize=(10, 10))
x1 = np.arange(-2, 2.1, 0.1)
x2 = np.arange(-5, -1.9, 0.1)
x3 = np.arange(2, 5.1, 0.1)
ax_2.hlines(y=5, xmin=-2, xmax=2)
ax_2.hlines(y=15, xmin=-2, xmax=2)
ax_2.hlines(y=30, xmin=-2, xmax=2)
ax_2.hlines(y=40, xmin=-2, xmax=2)
ax_2.vlines(x=-2, ymin=-2, ymax=55)
y1 = ax_2.vlines(x=2, ymin=-2, ymax=55)
y2 = ax_2.vlines(x=0, ymin=-2, ymax=55)
ax_2.fill_between(x1, 5, 15, facecolor="LightYellow")
ax_2.fill_between(x1, 15, 30, facecolor="LightGreen")
ax_2.fill_between(x1, 30, 40, facecolor="LightYellow")
ax_2.fill_between(x1, 40, 50, facecolor="Lightcoral")
ax_2.fill_between(x1, 0, 5, facecolor="Lightcoral")
ax_2.fill_between(x2, 0, 50, facecolor="Lightcoral")
ax_2.fill_between(x3, 0, 50, facecolor="Lightcoral")

But this method looks very cumbersome. Maybe there is a careful solution?

Maybe like this?

from matplotlib import pyplot as plt

fig_2, ax_2 = plt.subplots(figsize=(10, 10))
ax_2.hlines(y=[5, 15, 30, 40], xmin=-2, xmax=2)
ax_2.vlines(x=[-2, 0, 2], ymin=-2, ymax=55)
ax_2.fill_between([-5, 5], 0, 50, facecolor="Lightcoral")
ax_2.fill_between([-2, 2], 5, 40, facecolor="LightYellow")
ax_2.fill_between([-2, 2], 15, 30, facecolor="LightGreen")
plt.show()

结果图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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