简体   繁体   English

matplotlib 行间填充

[英]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()

结果图

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

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