你如何用箭头*和*文本注释matplotlib imshow地图?

时间:2013-10-08 01:43:56

标签: 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()

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

1 个答案:

答案 0 :(得分:9)

我通常会查看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