【问题标题】:how to plot a text on basemap, python如何在底图上绘制文本,python
【发布时间】:2013-12-20 06:20:36
【问题描述】:

我想在我的地图上放一个文字,比如the satellite imagery

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

m = Basemap(resolution='l',projection='geos',lon_0=-75.)    
fig = plt.figure(figsize=(10,8))    
m.drawcoastlines(linewidth=1.25)    
x,y = m(-150,80)
plt.text(x,y,'Jul-24-2012')

但是,“2012 年 7 月 24 日”文字没有出现在我的图中。 我猜这是因为地图不在笛卡尔坐标中。

那么,谁能帮我弄清楚如何做到这一点,好吗?

【问题讨论】:

    标签: python matplotlib matplotlib-basemap


    【解决方案1】:

    您的文本未显示的原因是您试图绘制一个对您正在使用的地图投影无效的点。

    如果您只想将文本放置在 坐标中的某个点(例如图的左上角),请使用 annotate,而不是 text

    事实上,您很少真正想要使用textannotate 更加灵活,实际上是为了注释绘图,而不是仅仅将文本放置在数据坐标中的 x,y 位置。 (例如,即使您想在数据坐标中注释 x,y 位置,您通常也希望文本与它的偏移距离以 points 为单位,而不是数据单位。)

    import matplotlib.pyplot as plt
    
    from mpl_toolkits.basemap import Basemap
    
    m = Basemap(resolution='l',projection='geos',lon_0=-75.)
    
    fig = plt.figure(figsize=(10,8))
    
    m.drawcoastlines(linewidth=1.25)
    
    #-- Place the text in the upper left hand corner of the axes
    # The basemap instance doesn't have an annotate method, so we'll use the pyplot
    # interface instead.  (This is one of the many reasons to use cartopy instead.)
    plt.annotate('Jul-24-2012', xy=(0, 1), xycoords='axes fraction')
    
    plt.show()
    

    【讨论】:

    • 应该注意annotate 只是text 的精美包装器,它会在后台为您处理转换。
    • 这真的很有帮助。我已将其更改为该位置的像素。谢谢。
    • 这个答案很完美。你为什么不接受它@Franke?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 2020-11-06
    • 1970-01-01
    • 2011-11-11
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多