最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - I want to fill the gaps in PyPlot fill_betweenx calls - Stack Overflow

programmeradmin2浏览0评论

I am trying to use Python to plot some data using pyplot.fill_betweenx.

Here is an example of code without considering category:

import numpy as np
import matplotlib.pyplot as plt

category  = np.array(['A','A','A','B','B','B','C','C','C'])
elev      = np.array([100,90,80,70,60,50,40,30,20])
strength  = np.array([8,8,7,6,6,6,9,10,10])

plt.fill_betweenx(elev,strength,step='mid')
plt.show()

I want to color the plot based on the category at the same index but am having issues with gaps.

No category image

I tried this approach:

plt.fill_betweenx(elev,np.where(category == 'A',strength,np.nan),step='mid',color='firebrick')
plt.fill_betweenx(elev,np.where(category == 'B',strength,np.nan),step='mid',color='royalblue')
plt.fill_betweenx(elev,np.where(category == 'C',strength,np.nan),step='mid',color='seagreen')
plt.show()

However, it results in gaps where the category changes.

Gaps!

I understand why there are gaps but I am having trouble coming up with a solution to it. Ideally, I'd like to fill the gap with the color of the category before it such that it results in something like the image below:

No gaps!

Any suggestions? Thanks!

I am trying to use Python to plot some data using pyplot.fill_betweenx.

Here is an example of code without considering category:

import numpy as np
import matplotlib.pyplot as plt

category  = np.array(['A','A','A','B','B','B','C','C','C'])
elev      = np.array([100,90,80,70,60,50,40,30,20])
strength  = np.array([8,8,7,6,6,6,9,10,10])

plt.fill_betweenx(elev,strength,step='mid')
plt.show()

I want to color the plot based on the category at the same index but am having issues with gaps.

No category image

I tried this approach:

plt.fill_betweenx(elev,np.where(category == 'A',strength,np.nan),step='mid',color='firebrick')
plt.fill_betweenx(elev,np.where(category == 'B',strength,np.nan),step='mid',color='royalblue')
plt.fill_betweenx(elev,np.where(category == 'C',strength,np.nan),step='mid',color='seagreen')
plt.show()

However, it results in gaps where the category changes.

Gaps!

I understand why there are gaps but I am having trouble coming up with a solution to it. Ideally, I'd like to fill the gap with the color of the category before it such that it results in something like the image below:

No gaps!

Any suggestions? Thanks!

Share Improve this question edited Mar 29 at 19:38 pippo1980 3,1813 gold badges17 silver badges40 bronze badges asked Mar 29 at 18:08 user30104652user30104652 311 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 2

Not very satisfying, but it does the job: you could overlap the categories 1 step forward, that is plotting where category is the wanted one OR where the previous category was the wanted one

previous = np.roll(category,1)
previous[0]='X'
for cat, col in zip("ABC", ["firebrick", "royalblue", "seagreen"]):
    plt.fill_betweenx(elev,strength, where=(category==cat)|(previous==cat), step='mid', color=col)

plt.show()

发布评论

评论列表(0)

  1. 暂无评论