matplotlib 中具有对数刻度的二维直方图散点图

Scatter plot over 2D-histogram in matplotlib with log-scale

我有两组值为 (x, y) 的点。一个很大(300k),一个很小(2k)。我想在对数对数比例的前者的二维直方图上显示后者的散点图。 plt.xscale('log')-like 命令不断弄乱直方图,当我只记录 x 和 y 的日志然后进行所有绘图时,我的刻度是 -3 而不是 10^-3 并且漂亮的对数次要刻度完全丢失. matplotlib 中最优雅的解决方案是什么?我必须深入到艺术家层吗?

如果您原谅一些自我宣传,您可以使用我的图书馆 physt(参见 https://github.com/janpipek/physt)。然后,你可以这样写代码:

import numpy as np
import matplotlib.pyplot as plt
from physt import h2

# Data
r1 = np.random.normal(0, 1, 20000)
r2 = np.random.normal(0, .3, 20000) + r1
x = np.exp(r1)
y = np.exp(r2)

# Plot scatter
fig, ax = plt.subplots()
ax.scatter(x[:1000], y[:1000], s=2)

H = h2(x, y, "exponential")
H.plot(ax=ax, zorder=-1)   # Necessary to put behind

希望能解决您的问题: