如何在python中对齐地图图和y图的y轴(纬度) [英] How can I align the y-axis (latitudes) of a map plot and a plot in python

查看:147
本文介绍了如何在python中对齐地图图和y图的y轴(纬度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个图中绘制两个面板. 第一个(左侧)是数据在y轴上具有纬度值的数据.第二个面板是地图. 我想两个面板的纬度值一致,但是我不知道怎么得到.

I'm trying plot two panels in a plot. The first one (left) is a data with latitude values in its y-axis. The second panel is a map. I wanna that the latitude values of both panels coinciding, but I don't know how get it.

我有这样的代码:

fig_mapa= plt.figure()

'''Mapa'''
ax1=fig_mapa.add_subplot(122)
map = Basemap(llcrnrlon=-90,llcrnrlat=-58.1,urcrnrlon=-32,urcrnrlat=12.6,
            resolution='f',projection='merc',lon_0=-58,lat_0=-25, ax=ax1)
map.drawparallels(np.arange(-90,90.,5), labels=[0,1,0,0], linewidth=0.5)
map.drawmeridians(np.arange(-180.,180.,5), labels=[0,0,0,1], linewidth=0.5)
map.readshapefile("./Fases_tectonicas/Shapefiles/Unidades_Fi",  'Unidades_Fi', linewidth=0.1)
#map.warpimage(image='./Geotiffs/NE1_HR_LC_SR_W_DR/NE1_HR_LC_SR_W_DR.tif', zorder=1)
map.drawcoastlines(linewidth=0.5, color='k')
Nombre_Unidad= []
for elemento in map.Unidades_Fi_info:
    Nombre_Unidad.append(elemento['NAME'])
for i in range(len(Nombre_Unidad)):
    draw=map.Unidades_Fi[i]
    poly=Polygon(draw, facecolor=color[Nombre_Unidad[i]],edgecolor='k', alpha=0.5,linewidth=0.1, zorder=2)
    plt.gca().add_patch(poly)

'''Gráfico Eventos Compresivos'''
ax2= fig_mapa.add_subplot(121)
ax2.set_ylim(-58.1,12.6)
ax2.set_xlim(120,0)
ax2.set_xlabel('Tiempo [Ma]')
ax2.set_ylabel('Latitud[°]')
ax2.grid()

推荐答案

对齐两个轴的最简单方法是使用plt.subplotssharexsharey关键字.但是,Basemap显示的坐标及其用于Axes实例的坐标是两件事,因此,如果要在第二个Axes实例中具有易于理解的ytick标签和一些有意义的图形,则必须在这两者之间进行转换. .下面,我展示了如何对齐两个y轴,正确设置yticks并将数据转换为底图的数据坐标.我没有改变底图的创建.

The simplest way to align two axes is with the sharex or sharey keyword for plt.subplots. However, the coordinates that Basemap shows and the coordinates that it uses for the Axes instance are two different things, so you will have to convert between the two if you want to have understandable ytick labels and some meaningful graph in your second Axes instance. Below I show how you can align the two y-axes, set the yticks properly and transform your data to the data coordinates of your Basemap. I left the creation of the Basemap untouched.

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

##figure with two subplots and shared y-axis
fig,(ax2,ax1) = plt.subplots(nrows=1, ncols=2, sharey='row')

m1 = Basemap(llcrnrlon=-90,llcrnrlat=-58.1,urcrnrlon=-32,urcrnrlat=12.6,
            #resolution='f',
              projection='merc',lon_0=-58,lat_0=-25, ax=ax1)
m1.drawparallels(np.arange(-90,90.,5), labels=[0,1,0,0], linewidth=0.5)
m1.drawmeridians(np.arange(-180.,180.,5), labels=[0,0,0,1], linewidth=0.5)
m1.drawcoastlines(linewidth=0.5, color='k')

##turning off yticks at basemap
ax1.yaxis.set_ticks_position('none')

##setting yticks:
yticks = np.arange(-55,12.6,5)
##transform yticks:
_,yticks_data = m1(0*yticks,yticks)
ax2.set_yticks(yticks_data)
ax2.set_yticklabels(['{: >3}$^\circ${}'.format(
    abs(int(y)), 'N' if y>0 else 'S' if y<0 else ' '
) for y in yticks])

ax2.set_xlim(120,0)
ax2.set_xlabel('Tiempo [Ma]')
ax2.set_ylabel('Latitud[$^\circ$]')
ax2.grid()

#some fake data for testing plotting
yrange = np.linspace(-60,20,100)
temp = (np.sin(10*np.deg2rad(yrange))+1)*50
##transform yrange
_,yrange_data = m1(0*yrange, yrange)
ax2.plot(temp,yrange_data)

plt.show()

以上代码的结果如下:

希望这会有所帮助.

这篇关于如何在python中对齐地图图和y图的y轴(纬度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆