Matplotlib:个性化 imshow 轴 [英] Matplotlib: personalize imshow axis
问题描述
我得到了(H,ranges)= numpy.histogram2d()
计算的结果,我正在尝试绘制它.
给定 H
我可以很容易地把它放到 plt.imshow(H)
中得到相应的图像.(请参见
I have the results of a (H,ranges) = numpy.histogram2d()
computation and I'm trying to plot it.
Given H
I can easily put it into plt.imshow(H)
to get the corresponding image. (see http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow )
My problem is that the axis of the produced image are the "cell counting" of H
and are completely unrelated to the values of ranges.
I know I can use the keyword extent
(as pointed in: Change values on matplotlib imshow() graph axis ). But this solution does not work for me: my values on range
are not growing linearly (actually they are going exponentially)
My question is: How can I put the value of range
in plt.imshow()
? Or at least, or can I manually set the label values of the plt.imshow
resulting object?
Editing the extent
is not a good solution.
You can just change the tick labels to something more appropriate for your data.
For example, here we'll set every 5th pixel to an exponential function:
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()
这篇关于Matplotlib:个性化 imshow 轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!