【问题标题】:matplotlib hammer projection draw outside mapmatplotlib 锤子投影在地图外绘制
【发布时间】:2016-03-08 02:02:10
【问题描述】:

我找到了一个带有锤子投影的人物,我一直在尝试复制它。 Hammer 投影本身没有问题,但我不知道如何生成地图下方的轴。事实上,我不知道如何在图形之外绘制(箭头和文本)一些东西。任何人都可以提供一个想法如何复制带有底部轴的图形吗?

【问题讨论】:

    标签: matplotlib axes


    【解决方案1】:

    你不能在图形坐标(像素或分数)中使用注释(参见http://matplotlib.org/users/annotations_intro.html)吗?

    【讨论】:

      【解决方案2】:

      您可以使用transform 关键字在轴坐标而不是数据坐标中绘制事物。如果您构建了一个轴对象ax (see here) 来绘制例如您可以使用左下角的文本字符串“180°”

      ax.text(0, 0, '180$^\circ$', ha='left', transform=ax.transAxes)
      

      transform=ax.transAxes 设置坐标系,使点(0, 0) 在左下角,(1, 0) 在右下角,(1, 1) 在右上角,(0, 1) 在左上角,无论坐标轴在数据坐标中的值。

      要绘制箭头,您可以等效地使用ax.arrow,也可以使用transform 关键字。

      【讨论】:

      • 我尝试了您建议的解决方案,但地图下方似乎没有足够的空间来绘制箭头和文本。箭头看起来像截断,如果将其延伸到地图的中间底部,它实际上会与地图本身重叠。这是代码:fig = plt.figure() rect = 0.05,0.05,0.9,0.9 ax = fig.add_axes(rect) m = Basemap(projection='hammer',lon_0=0,resolution='c') m.drawparallels(np.arange(-90.,120.,30.)) ax.text(0, 0, '180$^\circ$', ha='left', transform=ax.transAxes) ax.arrow(0.05, 0.0, 0.1, 0, head_width=0.05, head_length=0.05, fc='k', ec='k',transform=ax.transAxes)
      • 尝试使轴相对于图形更小,例如ax=fig.add_axes([0.2, 0.2, 0.6, 0.6]) 在轴的每一侧为您提供 1/5 (=0.2) 的图形大小的空白。这应该为您的旁注提供足够的空间。
      • 感谢您尝试帮助我,但它仍然不起作用。箭头仍然被切断。我的代码,根据你的建议是:from mpl_toolkits.basemap import Basemap import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_axes([0.2,0.2,0.6,0.6]) m = Basemap(projection='hammer',lon_0=0,resolution='c') # draw parallels and meridians. m.drawparallels(np.arange(-90.,120.,30.)) m.drawmeridians(np.arange(0.,420.,60.)) ax1.arrow(0, 0, 0.5, 0, head_width=0.05, head_length=0.05, fc='k', ec='k',transform=ax1.transAxes) plt.show()你能试试看吗?
      • 对不起,我没有安装mpl_toolkits.basemap,也不会。我不明白,你如何将Basemapax1 联系起来。也许在Basemap 文档中寻找一些关键字参数,以在ax 上显式绘制它。
      【解决方案3】:

      我的解决方案,以上述为基础:

      我用弧度来标记我的坐标轴,并且使用的是极坐标系,但原理是一样的:

      # custom x-axis
      ax.text(.49, -.15, '0', ha='left', transform=ax.transAxes)
      ax.text(.24, -.15, r'$\frac{\pi}{2}$', ha='left', transform=ax.transAxes)
      ax.text(0, -.15, r"$\pi$", ha='left', transform=ax.transAxes)
      ax.text(.74, -.15, r"$\frac{3\pi}{2}$", ha='left', transform=ax.transAxes)
      ax.text(.98, -.15, r"$\pi$", ha='left', transform=ax.transAxes)
      
      ax.annotate('', xy=(.96, -.1275), xycoords='axes fraction', xytext=(.78, -.1275), 
              arrowprops=dict(arrowstyle="<-", alpha=.35))
      ax.annotate('', xy=(.72, -.1275), xycoords='axes fraction', xytext=(.52, -.1275), 
              arrowprops=dict(arrowstyle="<-", alpha=.35))
      ax.annotate('', xy=(.48, -.1275), xycoords='axes fraction', xytext=(.27, -.1275), 
              arrowprops=dict(arrowstyle="<-", alpha=.35))
      ax.annotate('', xy=(.23, -.1275), xycoords='axes fraction', xytext=(.03, -.1275), 
              arrowprops=dict(arrowstyle="<-", alpha=.35))
      
      
      # custom y-axis
      ax.text(.84, .98, '0', ha='left', transform=ax.transAxes)
      ax.text(.84, -.03, r"$\pi$", ha='left', transform=ax.transAxes)
      
      ax.annotate('', xy=(.85, .12), xycoords='axes fraction', xytext=(.85, .01), 
              arrowprops=dict(arrowstyle="<-", alpha=.35))
      ax.annotate('', xy=(.851, .97), xycoords='axes fraction', xytext=(.851, .87), 
              arrowprops=dict(arrowstyle="-", alpha=.35))
      

      Output plot

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-16
        相关资源
        最近更新 更多