python - 你如何用箭头 * 和 * 文本注释 matplotlib imshow map ?

标签 python matplotlib imshow

我正在尝试制作一个看起来像这样的插图:

better image

但我得到的是:

basic image

这是我的程序:

from pylab import *
from matplotlib import colors
# A = [[1,2,3,4,5]]
A = [[0],[1]]
Amap = colors.ListedColormap(['blue','green'])

figure(1)
imshow(A, cmap=Amap, interpolation='nearest')
annotate('AA BB',xy=(0,0), xytext=(.8,0), fontsize=20)
axis('off')
savefig('graph-py.pdf')
show()

我已经尝试了所有方法来获得箭头,但似乎无法实现。有什么想法吗?

最佳答案

我通常会浏览 gallery找到我想做的事的例子。 annotation_demo2看起来与您想要的相似……我想出了这个。看起来你错过了 arrowprops kwarg .

from pylab import *
from matplotlib import colors
# A = [[1,2,3,4,5]]
A = [[0],[1]]
Amap = colors.ListedColormap(['blue','green'])

fig = figure(1)
ax = fig.add_subplot(111, autoscale_on=False)
imshow(A, cmap=Amap, interpolation='nearest')
ax.annotate('AA BB', fontsize=20, xy=(.25, .75),
            xycoords='data', xytext=(150, -6),
            textcoords='offset points',
            arrowprops=dict(arrowstyle="->",
                            linewidth = 5.,
                            color = 'red')
            )
ax.annotate('CC DD', fontsize=20, xy=(.25, .25),
            xycoords='data', xytext=(150, -6),
            textcoords='offset points',
            arrowprops=dict(width = 5.,
                            headwidth = 15.,
                            frac = 0.2,
                            shrink = 0.05,
                            linewidth = 2,
                            color = 'red')
            )
axis('off')
savefig('graph-py.pdf')
show()
close()

看起来像这样:

annotate example

关于python - 你如何用箭头 * 和 * 文本注释 matplotlib imshow map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19237703/

相关文章:

python - Matplotlib 3D : Remove axis ticks & draw upper edge border?

python - 如何在 y 轴上手动或自动添加缺失的 y 标签,该标签与其他规则间隔不一致

python - 如何获得 matplotlib 图中第二个颜色条的正确大小?

python - 在 django 框架上将 pyplot 图嵌入到 html 中

python - 从 raw_input 中提取 IP 地址

python - Pandas : OR If value contains "-" then do

python - 为什么当我执行 'log' 时,我的 matplotlib 条形图会压缩 x 轴

python - 将 MultiDiGraph 结果绘制为有向图

python - Python 子图中的作用域类似于 MATLAB 的 stackedplot()

python - 在 python 中的 imshow 中获取正确的轴标签