2

I want to build a hexbin plot in matplotlib where the width of the line for each hexagon is a function of the number of observations that fall in that hexagon. I think this will be useful for adding an additional level of inference to a hexbin plot.

So far, I have the following code:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.uniform(0,1,1000)
y = np.random.uniform(0,1,1000)
z = np.random.uniform(0,1,1000)

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.hexbin(x , y, C = z, gridsize=50, edgecolors='white')

This creates a hexbin plot where the color of the bin is a function of the average z value for x,y pairs in the bin.

Is there a way to set the line widths of the hexes to be a function the the number of observations in each in?

Ultimately I want thicker lines for less populated hexes. This should result in smaller visible hexes.

Bradley
  • 2,057
  • 3
  • 14
  • 17

1 Answers1

-1

Just give the linewidths as an array with the same length as x, y.

import numpy as np
import matplotlib.pyplot as plt

x = np.random.uniform(0,1,10)
y = np.random.uniform(0,1,10)
z = np.random.uniform(0,1,10)
LineWidths = np.random.uniform(1, 6, 10)

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.hexbin(x , y, C = z, gridsize=50, edgecolors='black', 
          linewidths=LineWidths)

enter image description here

I've made them black to illustrate the effect. You will need to define the line widths to suit the application you have in mind.

I will mention that controlling the size of the marker is much easier when using a scatter plot as demonstrated in this SO post. So unless you have other uses for the hexplot (I confess to ignorance of their importance) this would be much easier.

Community
  • 1
  • 1
Greg
  • 11,654
  • 3
  • 44
  • 50
  • This makes sense but doesn't quite answer my questions. The `hexbin` plot groups observations together to make a sort of aggregate scatter plot. The canvas of a graph is divided equally into hexes. All observations that fall within that hex are aggregated and the color of the hex is adjusted to reflect the average value of all observations on a specific variable. When no specific variable is given, color is use to represent the number of observations in each hex. I'm trying to create a graph that contains information on both number of observations (line width) and 3rd variable (color) – Bradley Apr 29 '14 at 18:22