如何填充图中y轴附近的区域?

问题描述 投票:0回答:1

我需要绘制数据帧的两个特征,其中df ['DEPTH']应该反转,并且在y轴,而df ['SPECIES']应该在x轴。假设该图是一条变线,我想用颜色填充y轴(线的左侧)附近的区域。所以我写了一些代码:

df = pd.DataFrame({'DEPTH': [100, 150, 200, 250, 300, 350, 400, 450, 500, 550],
               'SPECIES':[12, 8, 9, 6, 10, 7, 4, 3, 1, 2]})

plt.plot(df['SPECIES'], df['DEPTH'])
plt.fill_between(df['SPECIES'], df['DEPTH'])

plt.ylabel('DEPTH')
plt.xlabel('SPECIES')

plt.ylim(np.max(df['DEPTH']), np.min(df['DEPTH']))

我尝试过'plt.fill_between',但是情节的左边部分并没有全部填满。

enter image description hereenter image description here

任何人都知道填充部分(蓝色)如何到达y轴?

python-3.x dataframe matplotlib fill
1个回答
0
投票

代替fill_between,您可以使用fill_betweenx。默认情况下,它将从0开始填充,因此您也需要将x限制也设置为0。

fill_betweenx

结果如下。

plt.plot(df['SPECIES'], df['DEPTH']) # changing fill_between to fill_betweenx -- the order also changes plt.fill_betweenx(df['DEPTH'], df['SPECIES']) plt.ylabel('DEPTH') plt.xlabel('SPECIES') plt.ylim(np.max(df['DEPTH']), np.min(df['DEPTH'])) # setting the lower limit to 0 for the filled area to reach y axis. plt.xlim(0,np.max(df['SPECIES'])) plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.