0
votes

I have a 2D power-law like dataset:

import numpy as np
X = 1 / np.random.power(2, size=1000)
Y = 1 / np.random.power(2, size=1000)

I can plot it using a scatter plot in loglog scale

import matplotlib.pyplot as plt
plt.figure()
plt.scatter(X, Y, alpha=0.3)
plt.loglog()
plt.show()

getting:

enter image description here

However, it does not show properly the data near the origin where the density is high. Hence, I converted this plot in a heatmap. I did that:

from matplotlib.colors import LogNorm
heatmap, xedges, yedges = np.histogram2d(X, Y, bins=np.logspace(0, 2, 30))
plt.figure()
plt.imshow(heatmap.T, origin='lower', norm=LogNorm())
plt.colorbar()
plt.show()

getting:

enter image description here

The plot looks great but the axis ticks are not good. To change the scale I tried to add extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] in the imshow but it only does an affine transformation, the scale is still linear not logarithmic. Do you know how can I get the heatmap plot but with ticks of the scatter one?

1
Did you try pcolormesh instead of imshow? - JohanC

1 Answers

0
votes

You can use pcolormesh like JohanC advised.

Here is an example with you code using pcolormesh:

import numpy as np
import matplotlib.pyplot as plt

X = 1 / np.random.power(2, size=1000)
Y = 1 / np.random.power(2, size=1000)

heatmap, xedges, yedges = np.histogram2d(X, Y, bins=np.logspace(0, 2, 30))

fig = plt.figure()
ax = fig.add_subplot(111)

ax.pcolormesh(xedges, yedges, heatmap)
ax.loglog()
ax.set_xlim(1, 50)
ax.set_ylim(1, 50)

plt.show()

And the output is:

enter image description here