The Issue
I have two 100x100 arrays- one with data (which I'll call array1
) and one with some smaller, scaled data (which I'll call array2
). I created a test array to see where array2
+ array1
is above or below certain thresholds, and the results for each point should be one of three results- array1
+ array2
for that point is >5, >10, or neither (<5). Depending on the result, I created a new 100x100 array (array3) and assigned a 1., 2., or 0. to that point, respectively.
Now I want to plot array1 & array3 using plt.contourf()
, and I want the latter subplot to have hatching. I want the range of that hatching to be x=0, x=1, and x=2. Looking at the documentation I know I probably can't do it that way, so instead I'll say x<1, 1<=x,<2, and x=>2. Only problem is I don't know how to specify those ranges.
The code (working example)
Here's the full code of the problem.
#Make data array1
array1 = np.empty([10,10])
for i in range(10):
for j in range(10):
array1[i,j] = i+j
print array1
#Make data array2
array2 = np.empty([10,10])
for i in range(10):
for j in range(10):
array2[i,j] = (i*0.25)+(j*0.25)
print array2
#Make range test array3
array3 = np.empty([10,10])
for i in range(10):
for j in range(10):
if array1[i,j]+array2[i,j] > 5 and array1[i,j]+array2[i,j] > 10:
array3[i,j] = 2
elif array1[i,j]+array2[i,j] > 5:
array3[i,j] = 1
else:
array3[i,j] = 0
print array3
#Plot
from matplotlib.patches import Ellipse, Polygon
xgrid = np.arange(0,10)
ygrid = np.arange(0,10)
n_levels=2
plt.contourf(xgrid, ygrid, array1)
stip = plt.contourf(xgrid, ygrid, array3, n_levels, colors='none',
hatches=[None,'.', '/'],
extend='lower')
#create a legend for the contour set
artists, labels = stip.legend_elements()
plt.legend(artists, labels, handleheight=2)
plt.show
Note the legend. I want the ranges to be x<1, 1<=x,<2, and x=>2, not what the package automatically assigns.
The Question
How can I assign custom ranges to my hatching? I plan to get this example working so I can translate it to a Basemap plot where I have lat-lon data, and on top of that a stippled array of where that lat-lon data is compared to the data+S.D. of two other arrays. I wanted various stippling depending on, for each grid point, if that lat-lon data is > one data+S.D. array, the other data+S.D. array, or both arrays.
For more control on the contour levels use the levels
keyword.
stip = plt.contourf(xgrid, ygrid, array3, levels=[-1, 0, 1, 2],
colors='none', hatches=[None,'.', '/'])
artists, labels = stip.legend_elements()
plt.legend(artists, labels, handleheight=2)