matplotlib在数据坐标中注释底图

2024-04-26 06:24:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个锤子投影图,我试图在图的中心添加文本(纬度=0,经度=0)。出于某种原因,字符串“0”绘制在图的左下角。在

我有以下代码。在

from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm


#data points
ra = [25,20,21]
dec = [25,20,21]

fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])

# Get the hammer projection map
m = Basemap(projection='hammer',lon_0 = 0, rsphere = 1.0)
m.drawparallels(np.arange(-90.,90.,30.),labels=[1,0,0,0]) # draw parallels
m.drawmeridians(np.arange(-180.,180.,60.)) # draw meridians

m.plot(ra,dec,marker='o',linestyle='None',markersize=1,latlon=True)
ax.annotate('0', xy=(0, 0), xycoords='data',xytext = (0,0),textcoords='data')

plt.show()

我还附上了图,很明显,字符“0”的位置不正确。有什么想法吗?在

enter image description here


Tags: importdatamatplotlibasnpfigcmplt
1条回答
网友
1楼 · 发布于 2024-04-26 06:24:46

您必须首先使用Basemap实例将纬度/经度坐标转换为其等效的x,y坐标。matplotlib不会自动为您执行此操作,因为它不知道纬度和经度。在

# Convert from latitude/longitude to x,y
x, y = m(0,0)
ax.annotate('0', xy=(x, y), xycoords='data', xytext=(x, y), textcoords='data')

enter image description here

在它们的own documentation中,它们演示了text的用法,而不是注释,因为没有箭头。在

^{pr2}$

相关问题 更多 >

    热门问题