0

I'm trying to create a plot of my data that is in two dimensions, each ranging over multiple orders of magnitude. For this reason, I wanted to plot the data in my 2d histogram with logarithmic axes, however, I'm yet unable to get the axis scale and plot scale to match up.

The plots below were achieved using this code:

heatmap, X, Y = np.histogram2d(x, y, bins=N)

extent = [X[0], X[-1], Y[0], Y[-1]]

plt.clf()
plt.imshow(heatmap.T, extent=extent, origin='lower', aspect='auto')
plt.colorbar()
plt.xscale('log')   # This line was not used to create the first image
plt.yscale('log')   # This line was not used to create the first image
plt.show()

The two outputs currently look like this: enter image description here As you can see the first uses a normal scale for the axes but has the correct data scaling, and the second has the properly scaled axes (using lines 8,9 of the code above) but the data is then scaled incorrectly.

How would you achieve the effect that I'm looking for, of combining the plot of the first one with the axes of the second?

I would also like to note that I found this post that seemingly answers this problem, however, when I used this method, the colorbar normalizes to between 0 and 1 which I don't want, the histogram is upside down along with the plot being shrunk vertically, and the axis labels are wrong. (Some of the axis labels repeat as shown below)

New code required for Colorbar:

fig.colorbar(ScalarMappable(norm=None, cmap='viridis'))

enter image description here

This all seems so unbelievably complex, I just want to put the visual from the first plot onto the axes of the second plat. Is there a way to do this?

Oliver Nicholls
  • 176
  • 2
  • 15

0 Answers0