【问题标题】:Matplotlib: personalize imshow axisMatplotlib:个性化 imshow 轴
【发布时间】:2016-03-04 08:32:00
【问题描述】:
【问题讨论】:
标签:
python-3.x
matplotlib
【解决方案1】:
扩展一点@thomas 的回答
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mi
im = np.random.rand(20, 20)
ticks = np.exp(np.linspace(0, 10, 20))
fig, ax = plt.subplots()
ax.pcolor(ticks, ticks, im, cmap='viridis')
ax.set_yscale('log')
ax.set_xscale('log')
ax.set_xlim([1, np.exp(10)])
ax.set_ylim([1, np.exp(10)])
通过让 mpl 处理非线性映射,您现在可以准确地过度绘制其他艺术家。这会影响性能(因为pcolor 的绘制成本比AxesImage 更高),但获得准确的刻度是值得的。
【解决方案2】:
您可以将刻度标签更改为更适合您的数据的内容。
例如,这里我们将每 5 个像素设置为一个指数函数:
import numpy as np
import matplotlib.pyplot as plt
im = np.random.rand(21,21)
fig,(ax1,ax2) = plt.subplots(1,2)
ax1.imshow(im)
ax2.imshow(im)
# Where we want the ticks, in pixel locations
ticks = np.linspace(0,20,5)
# What those pixel locations correspond to in data coordinates.
# Also set the float format here
ticklabels = ["{:6.2f}".format(i) for i in np.exp(ticks/5)]
ax2.set_xticks(ticks)
ax2.set_xticklabels(ticklabels)
ax2.set_yticks(ticks)
ax2.set_yticklabels(ticklabels)
plt.show()
【解决方案3】:
imshow 用于显示图像,因此不支持 x 和 y bin。
你也可以改用pcolor,
H,xedges,yedges = np.histogram2d()
plt.pcolor(xedges,yedges,H)
或使用plt.hist2d 直接绘制您的直方图。