Using an image for tick labels in matplotlib
我有一系列固定宽度的小图像,我想用它们替换刻度标签。例如,考虑以下最小的工作示例:
1
2 3 4 5 6 7 |
import numpy as np
import pylab as plt A = np.random.random(size=(5,5)) |
我想用自定义图像替换” 0″。我可以关闭标签,将图像加载到数组中并正常显示。但是,我不确定=”” <=”” p=”” class=”box-hide box-show”>
- 刻度标签的位置在哪里,因为它们位于绘图之外。
- 使用 imshow 显示该图像,如果将其放入轴中,它将被”剪裁”。
我的想法是使用 set_clip_on 或自定义艺术家,但我没有取得太大进展。
有趣的问题,并且可能有许多可能的解决方案。这是我的方法,基本上是先计算标签’0’在哪里,然后用绝对坐标在那里画一个新轴,最后把图像放在那里:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import numpy as np
import matplotlib.pyplot as plt import matplotlib.image as mpimg import pylab as pl A = np.random.random(size=(5,5)) xl, yl, xh, yh=np.array(ax.get_position()).ravel() img=mpimg.imread(‘microblog.png’) plt.savefig(‘temp.png’) |
来源:https://www.codenong.com/24226683/