Tobias Hermann
Tobias Hermann

Reputation: 10956

How to use log scale and log bins for hist2d in Matplotlib?

The following minimal example shows my problem:

import matplotlib.pyplot as plt
import numpy as np

X = np.random.normal(30, 10, 1000)
Y = np.random.normal(200, 500, 1000)

x_space = np.linspace(0, 60, 6)
print('x_space:', x_space)

y_space = np.logspace(np.log10(1.0), np.log10(1000.0), 30)
print('y_space:', y_space)

plt.hist2d(X, Y, bins=(x_space, y_space))
plt.yscale('log')
plt.show()

I expect the bins to be displayed equally-sized in the graph, because the log scale on the y axis and the log space for y should equal out. However this seems to not be the case.

enter image description here

The text output of the generated bins looks OK to me:

x_space: [ 0. 12. 24. 36. 48. 60.]
y_space: [   1.            1.268961      1.61026203    2.04335972    2.5929438
    3.29034456    4.17531894    5.29831691    6.72335754    8.53167852
   10.82636734   13.73823796   17.43328822   22.12216291   28.07216204
   35.6224789    45.20353656   57.3615251    72.78953844   92.36708572
  117.21022975  148.73521073  188.73918221  239.502662    303.91953823
  385.66204212  489.39009185  621.01694189  788.04628157 1000.        ]

But it does not correspond to what is plotted.

And I doing something wrong, or might this be a bug in the library?

(I'm using Matplotlib version 2.2.2.)

Upvotes: 4

Views: 2826

Answers (1)

Thomas Kühn
Thomas Kühn

Reputation: 9820

This appears to be a bug in version 2.2.2. In version 3.02, the output of the code looks as one would expect:

result of the OPs code with matplotlib version 3.02

Upvotes: 2

Related Questions