 # Pre-defined binning with hexbin ... impossible?

Hello,

I’ve been struggling with this for a while now and have effectively two issues. One, how can I define the range over which hexbin … bins? And two, how do I change the background color of a plot? The latter I thought would be trivial, but nothing I’ve tried has worked. Let’s take the example:

import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt

n = 100000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
#xmin = x.min()
#xmax = x.max()
#ymin = y.min()
#ymax = y.max()
xmin = 0
xmax = 10
ymin = 0
ymax = 30

plt.hexbin(x,y, cmap=cm.jet)
#plt.hexbin(x,y, cmap=cm.jet, extent=[xmin, xmax, ymin, ymax])
plt.axis([xmin, xmax, ymin, ymax])
plt.title(“Hexagon binning”)
cb = plt.colorbar()
cb.set_label(‘counts’)

plt.show()

Let’s say i’m only interested in the positive quadrant of the data, so I only want to bin data in the range of 0-10 in x and 0-30 in y. The first thing I tried was to use the extent option to only bin the specified range of data, but that gives these errors:

[tesla:~/NMR/500-Tupp/IM7] HexBin.py
Traceback (most recent call last):
File “HexBin.py”, line 28, in
plt.hexbin(x,y, cmap=cm.jet, extent=[xmin, xmax, ymin, ymax])
File “/usr/lib64/python2.5/site-packages/matplotlib/pyplot.py”, line 1920, in hexbin
ret = gca().hexbin(*args, **kwargs)
File “/usr/lib64/python2.5/site-packages/matplotlib/axes.py”, line 5447, in hexbin
collection.update(kwargs)
File “/usr/lib64/python2.5/site-packages/matplotlib/artist.py”, line 548, in update
raise AttributeError(‘Unknown property %s’%k)
AttributeError: Unknown property extent

After that, I just set the plt.axis range to what I’m interested in. However, the range not binned by hexbin shows up as white, instead of the 0-count blue. I’ve tried adding facecolor, edgecolor, axisbg, and who knows what else as part of plt.hexbin, plt.axis, and plt.savefig, But most have no consequence on the plot. plt.savefig(myname, facecolor=‘blue’) successfully changed everything blue except the white region within the plot.

So, where am I going wrong? I’m finding myself out of options and would ideally not like to trim or add false points to my data just to get the axis and colors correct.

Best,
Alex

Alexandar Hansen wrote:

Hello,

I've been struggling with this for a while now and have effectively two issues. One, how can I define the range over which hexbin ... bins? And two, how do I change the background color of a plot?

Alexandar: To change the background color to the lowest value in the colormap:

ax = plt.gca()
ax.set_axis_bgcolor(plt.cm.jet(0))

-Jeff

···

The latter I thought would be trivial, but nothing I've tried has worked. Let's take the example:

import numpy as np
import matplotlib.cm <http://matplotlib.cm> as cm
import matplotlib.pyplot as plt

n = 100000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
#xmin = x.min()
#xmax = x.max()
#ymin = y.min()
#ymax = y.max()
xmin = 0
xmax = 10
ymin = 0
ymax = 30

plt.hexbin(x,y, cmap=cm.jet)
#plt.hexbin(x,y, cmap=cm.jet, extent=[xmin, xmax, ymin, ymax])
plt.axis([xmin, xmax, ymin, ymax])
plt.title("Hexagon binning")
cb = plt.colorbar()
cb.set_label('counts')

plt.show()

Let's say i'm only interested in the positive quadrant of the data, so I only want to bin data in the range of 0-10 in x and 0-30 in y. The first thing I tried was to use the extent option to only bin the specified range of data, but that gives these errors:

[tesla:~/NMR/500-Tupp/IM7] HexBin.py
Traceback (most recent call last):
File "HexBin.py", line 28, in <module>
plt.hexbin(x,y, cmap=cm.jet, extent=[xmin, xmax, ymin, ymax])
File "/usr/lib64/python2.5/site-packages/matplotlib/pyplot.py", line 1920, in hexbin
ret = gca().hexbin(*args, **kwargs)
File "/usr/lib64/python2.5/site-packages/matplotlib/axes.py", line 5447, in hexbin
collection.update(kwargs)
File "/usr/lib64/python2.5/site-packages/matplotlib/artist.py", line 548, in update
raise AttributeError('Unknown property %s'%k)
AttributeError: Unknown property extent

After that, I just set the plt.axis range to what I'm interested in. However, the range not binned by hexbin shows up as white, instead of the 0-count blue. I've tried adding facecolor, edgecolor, axisbg, and who knows what else as part of plt.hexbin, plt.axis, and plt.savefig, But most have no consequence on the plot. plt.savefig(myname, facecolor='blue') successfully changed everything blue except the white region within the plot.

So, where am I going wrong? I'm finding myself out of options and would ideally not like to trim or add false points to my data just to get the axis and colors correct.

Best,
Alex

------------------------------------------------------------------------

------------------------------------------------------------------------------
------------------------------------------------------------------------

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users