Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Closed
Labels
Milestone
Description
Problem
ax.stackplot()
is used for conveniently plotting stacked area plot.
The documentation says that any**kwargs
is passed on toax.fill_between
.
So, it all works fine if I work with colours, but not so much with hatching pattern.
If I want to add hatching, this is what I get by default:
importmatplotlib.pyplotaspltimportnumpyasnp# Datacols=10rows=4data= (np.reshape(np.arange(0,cols,1), (1,-1))**2+np.reshape(np.arange(0,rows), (-1,1))+np.random.random((rows,cols))*5)# Plotfig,ax=plt.subplots(figsize=(10,6),facecolor="white",layout="constrained")fig.suptitle("with hatching",fontsize=25,weight="bold")x=range(data.shape[1])ax.stackplot(x,data,hatch="x")ax.set_xlim(0,9)ax.set_ylim(0,350)
But I would be more interested in getting something like this:
Proposed solution
The calling signature would be the same as before with exception that thehatch
parameter would accept e.g. a list withhatching
styles you want to use.
Example:
ax.stackplot(x,data,hatch=["x","\\",".","-"])
The example I've used above in the problem was create using the code below and in this case it varies the hatching by the density:
fig,ax=plt.subplots(figsize=(10,6),facecolor="white",layout="constrained")fig.suptitle("with individual hatching",fontsize=25,weight="bold")x=range(data.shape[1])stack_baseline=np.zeros((data.shape[1]))fori,yinenumerate(data):ax.fill_between(x,stack_baseline,y+stack_baseline,facecolor=(1,1,1,0),hatch="x"*(data.shape[0]-i) )stack_baseline+=yax.set_xlim(0,9)ax.set_ylim(0,350)