Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've written a bit of code that does what you're after. From the snippet in your question, it looks like you already know the height (<code>max_v</code>) of your distribution given your binning scheme, so I worked under that assumption. Depending on the data you're applying this to, this might not actually be the case, in which case the following will fail (it's only as good as your guess/knowledge of the height of the distributions). For the purposes of my example data, I've just taken a reasonable guess (based on a quick plot) for the values of <code>max_v1</code> and <code>max_v2</code>. Switching the <code>c1</code> and <code>c2</code> I've defined for the commented versions should reproduce your original problem.</p> <pre><code>import scipy import matplotlib.pyplot as pyplot import matplotlib.colors import math #need to know the height of the distributions a priori max_v1 = 850 #approximate height of distribution 1 (defined below) with binning defined below max_v2 = 400 #approximate height of distribution 2 (defined below) with binning defined below max_v = max(max_v1,max_v2) #make 2 differently sized datasets (so will require different normalizations) #all normal distributions with assorted means/variances x1 = scipy.randn(50000)/6.0+0.5 y1 = scipy.randn(50000)/3.0+0.5 x2 = scipy.randn(100000)/2.0-0.5 y2 = scipy.randn(100000)/2.0-0.5 #c1 = scipy.ones(len(x1)) #I don't assign meaningful weights here #c2 = scipy.ones(len(x2)) #I don't assign meaningful weights here c1 = scipy.ones(len(x1))*(max_v/max_v1) #highest distribution: no net change in normalization here c2 = scipy.ones(len(x2))*(max_v/max_v2) #renormalized to same height as highest distribution #define plot boundaries xmin=-2.0 xmax=2.0 ymin=-2.0 ymax=2.0 #custom colormap cmap = matplotlib.colors.ListedColormap(['grey','#6A92D4','#1049A9','#052C6E']) #the bounds of 1sigma, 2sigma, etc. regions bounds = [0,max_v*math.exp(-(3**2)/2),max_v*math.exp(-2),max_v*math.exp(-0.5),max_v] norm = matplotlib.colors.BoundaryNorm(bounds, ncolors=4) #make the hexbin plot normalized = pyplot hexplot = normalized.subplot(111) normalized.hexbin(scipy.concatenate((x1,x2)), scipy.concatenate((y1,y2)), C=scipy.concatenate((c1,c2)), cmap=cmap, mincnt=1, extent=(xmin,xmax,ymin,ymax),gridsize=50, reduce_C_function=scipy.sum, norm=norm) #combine distributions and weights hexplot.axis([xmin,xmax,ymin,ymax]) cax = pyplot.axes([0.86, 0.1, 0.03, 0.85]) clims = cax.axis() cb = normalized.colorbar(cax=cax) cax.set_yticklabels([' ','3','2','1',' ']) normalized.subplots_adjust(wspace=0, hspace=0, bottom=0.1, right=0.78, top=0.95, left=0.12) normalized.show() </code></pre> <p>Here's the <a href="http://i.stack.imgur.com/ZvZXC.png" rel="nofollow">result</a> without the fix (commented <code>c1</code> and <code>c2</code> used), and the <a href="http://i.stack.imgur.com/o7pOh.png" rel="nofollow">result</a> with the fix (code as-is); (seems my low rep prevents my posting actual images).</p> <p>Hope that helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload