Creating hexbin in python matplotlib fills a blank space on a square axis?

I am trying to use hexbin to plot some data on a square axis. I am using the following:

import matplotlib.cm as cm plt.figure() num_pts = 1000 x = rand(num_pts) * 100 y = rand(num_pts) * 250 x_min = 0 x_max = 150 x_step = 25 y_min = 50 y_max = 300 y_step = 50 s = plt.subplot(1,1,1) plt.hexbin(x,y,cmap=cm.jet,gridsize=20) plt.xticks(range(x_min,x_max+x_step,x_step)) plt.yticks(range(y_min,y_max+y_step,y_step)) # square axes s.axes.set_aspect(1/s.axes.get_data_ratio()) 

I would like the axes to be square, and I want to set my own xticks / yticks and xy limits. there will be no data axes for some values, and therefore the calculations calculated using hexbin should be zero for them. I would like hexbin to construct this as an empty space, instead of leaving it β€œwhite” / blank if you use the cm.jet color palette.

Now I get it.

How can I get it to fill the empty space using its colormap? thanks. enter image description here

+4
source share
2 answers

I believe the answer is to use the length = argument of the keyword, for example:

 plt.hexbin(x, y, cmap=cm.jet, gridsize = 20, extent=[x_min, x_max, y_min, y_max]) 
+8
source

You can also simply create an axis and set its background color to the minimum value of the color palette (or any desired color):

 fig = plt.figure() ax = fig.add_subplot(111,axisbg=cm.jet(0)) 
+2
source

Source: https://habr.com/ru/post/1385699/


All Articles