# How to create hexbin plot

Hello!

I’ve come accross the following graph by googling ‘Hexbin plotly’ but I can’t seem to find a reproducible example in the documentation. What kind of graph is it? A ‘hacked’ scatter plot?

There’s nothing in plotly.py to help make hex-bin plots directly (yet! would love to add a figure factory for this), but perhaps @empet could chime in on how this one was made.
-Jon

@Khreas, @jmmease I’ll look for the notebook where I defined the hexbin plot (two years ago or so), update it to Plotly 3.+ , and upload on Plotly cloud.

2 Likes

Any update on this? I’m trying to figure out how you built the shapes for the hexbin plot, but it seems quite difficult. Would be great to see how that works

@Khreas @sschneider I called matplotlib `plt.hexbin()` and converted the returned `PolyCollection` to Plotly shapes
https://nbviewer.jupyter.org/gist/empet/8e466955c9e30f7471b4fb45c3a0fb21.

3 Likes

if I am trying to create a hexbin plot similar to this one: https://chart-studio.plotly.com/~empet/13706/hexbin-plot/#plot

but my raw data for the scatter takes the form of integers, i.e. [0,3] or [2,2] would be a coordinate. Do i first need to normalize the data into “hexbins” which will provide the interlocking pattern as shown in the linked chart?

Thanks.

Hexbin plot is destinated to estimating bivariate continuous distributions or your data are samples from a discrete one.

ok - will take a look at the matplotlib implementation. i have continuous distributions but i had already binned them into discrete (1x1) “buckets”. will explore. thanks!

Dear empet,
I tried to reproduce your code here Jupyter Notebook Viewer and face a problem. Somehow, the hexbin-plot always has the same color as the result of get_facecolors() is converted to one single RGB-value in the pl_cell_color() method. Do you know how to fix this issue? I am using python 3.11.

@Athlete
I have Python 3.10.6 and matplotlib 3.5.3, and the code works like a charm, even though it’s written in an old plotly version. I doubt matplotlib has changed the way it defines facecolor.

Hi, @empet! Thanks for the information. I really appreciate your help! Unfortunately, still the line in the for loop leads to an index out of bounds error as cell_color only contains one value and k>0. Can you reproduce it on the https://github.com/empet/Datasets/blob/master/hexbin-data.npy dataset used in the notebook?

mpl_facecolors: [[0.12156863 0.46666667 0.70588235 1. ]]
cell_color: [‘rgb(31, 119, 180)’]
Traceback (most recent call last):
File “hexbin.py”, line 143, in
for k in range(len(offsets)):
shape, center = make_hexagon(hexagon_vertices, offsets[k], cell_color[k])
IndexError: list index out of range

@Athlete
I’ve just ran the code from the notebook with the initial data, and here is the result
https://nbviewer.org/gist/empet/49dffa9e1708b1f5b971ecb4c0a8234f.

Thank you so much! It works now! Changing the matplotlib version to 3.5.3 seemed to have solved the problem.

Hi @empet !
It is very weird that it works in the Jupyter Notebook with ipykernel 8.0.2, but not if you convert it to a script with jupyter nbconvert --to script demo/test.ipynb . Can you reproduce the original error with this command and python demo/test.py?

Solution:
Ff you add HB.figure.canvas.draw() after the HB is set then it works. If you don’t put this, then only one color is outputted by the hexbin.get_facecolors().

Thanks a lot again!

1 Like