Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing value in each bin in hist2d (matplotlib)

I would like to create a 2d histogram, where in each bin the value represented by that bin is shown in the center of that given bin. For example a hist2d of size 5x5 would have 25 values inside the final graph. This is well doable with PyROOT, but I need to use matplotlib/pyplot here.

The following has been tried according to the first answer:

fig, ax = plt.subplots()
ax.set_aspect("equal")
hist, xbins, ybins, im = ax.hist2d(x, y, bins=(4, [1,2,3,5,10,20]))
ax.text(xbins[1]+0.5,ybins[1]+0.5, "HA", color="w", ha="center", va="center", fontweight="bold")

img = StringIO.StringIO()
plt.savefig(img, format='svg')
img.seek(0)
print("%html <div style='width:500px'>" + img.getvalue() + "</div>")

There wasn't any error message but "HA" wan't displayed in the first bin at all. I am programming this in Zeppelin, thus I need to take img from buffer ...

like image 245
user3603644 Avatar asked Apr 21 '17 09:04

user3603644


People also ask

How do you read a 2D histogram?

In image processing a 2D histogram shows the relationship of intensities at the exact position between two images. The 2D histogram is mostly used to compare 2 channels in a multi-channel images, where the x-axis represent the intensities of the first channel and the y-axis the intensities of the second channel.

How do I display the count over the bar in Matplotlib histogram?

To display the count over the bar in matplotlib histogram, we can iterate each patch and use text() method to place the values over the patches.


1 Answers

To annotate a hist2d plot, just like any other plot, you may use matplotlib's text method. The values to annotate are given by the returned histogram. The positions of the annotations are given by the histogram edges (plus half the bin width). You may then loop over all bins and place a text in each bin.

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)

x = np.random.poisson(size=(160))
y = np.random.poisson(size=(160))

fig, ax = plt.subplots()
ax.set_aspect("equal")
hist, xbins, ybins, im = ax.hist2d(x,y, bins=range(6))

for i in range(len(ybins)-1):
    for j in range(len(xbins)-1):
        ax.text(xbins[j]+0.5,ybins[i]+0.5, hist.T[i,j], 
                color="w", ha="center", va="center", fontweight="bold")

plt.show()

enter image description here

If only a single annotation is required, e.g. the following

ax.text(xbins[1]+0.5,ybins[1]+0.5, "HA", 
        color="w", ha="center", va="center", fontweight="bold")

will produce

enter image description here

like image 198
ImportanceOfBeingErnest Avatar answered Nov 14 '22 22:11

ImportanceOfBeingErnest