1

Surprisingly nobody took the pain to make an example in the bokeh gallery for 2D histogram plotting

histogram2d of numpy gives the raw material, but would be nice to have an example as it happens for matplotlib

Any idea for a short way to make one?

Following up a proposed answer let me attach a case in which hexbin does not the job because exagons are not a good fit for the job. Also check out matplotlib result.

Of course I am not saying bokeh cannot do this, but it seem not straightfoward. Would be enough to change the hexbin plot into a square bin plot, but quad(left, right, top, bottom, **kwargs) seems not to do this, nor hexbin to have an option to change "tile" shapes.

hexbin enter image description here

Rho Phi
  • 1,182
  • 1
  • 12
  • 21

1 Answers1

2

You can make something close with relatively few lines of code (comapring with this example from the matplotib gallery). Note bokeh has some examples for hex binning in the gallery here and here. Adapting those and the example provided in the numpy docs you can get the below:

import numpy as np

from bokeh.plotting import figure, show
from bokeh.layouts import row

# normal distribution center at x=0 and y=5
x = np.random.randn(100000)
y = np.random.randn(100000) + 5

H, xe, ye = np.histogram2d(x, y, bins=100)

# produce an image of the 2d histogram
p = figure(x_range=(min(xe), max(xe)), y_range=(min(ye), max(ye)), title='Image')

p.image(image=[H], x=xe[0], y=ye[0], dw=xe[-1] - xe[0], dh=ye[-1] - ye[0], palette="Spectral11")

# produce hexbin plot
p2 = figure(title="Hexbin", match_aspect=True)
p.grid.visible = False

r, bins = p2.hexbin(x, y, size=0.1, hover_color="pink", hover_alpha=0.8, palette='Spectral11')

show(row(p, p2))

2d histogram with bokeh

Anthonydouc
  • 3,334
  • 1
  • 16
  • 29
  • Thanks @Anthonydouc I should have mentioned that hexbin plots does almost what I asked for. I am huge fan of hexbin plot indeed. Now I have edited my question to show a particular case where hexbin really cannot cope. – Rho Phi Jun 23 '18 at 15:51
  • I think it is `image=[np.transpose(H)]` but please check. In any case thanks for suggesting `bokeh.plotting.image` – Rho Phi Jun 23 '18 at 22:04
  • In the example you put above try decrease the hexbin size, that may reduce that effect. Also image is using square bins, if you decrease the bin size you will have some very large squares.If you want to use quad or square glyphs you would need to get the coords of every square/quad. – Anthonydouc Jun 23 '18 at 22:24
  • @Anthonydouc any idea how to make the empty bins in square-bins-image white, just like it is for hexbins? I have a similar use-case where your solution would help, but the distinction between empty and non-empty bins need to be apparent. – Anirban Chakraborty Nov 11 '22 at 12:57
  • 1
    @AnirbanChakraborty if you need to use the image class to generate the histogram you will need to understand how the palette is applied and modify it. Alternatively try use a different plotting class such as square markers. – Anthonydouc Dec 21 '22 at 23:26