最近在做一个散点图,横轴为经度,纵轴为速度,即不是纬度,因此不是那种展现空间关系的。
但是数据的经度为70到180,之后-180到-170这样,因此用正常的方法出图会出现,-180和180的散点无法进行连接。
于是使用python修改标签的办法。首先将横轴的数据即经度的-180~180变为0-360
利用以下函数实现
def changto360(data):
c = data['lon']
c[c<0]=c+360
data['lon']=c
return data
之后采用以下办法自定义横轴标签
plt.xticks([60,90,120,150,180],#,210
['60°E','90°E','120°E','150°E','180°'],#,'150°W'
fontsize=12)
以下是所有代码,包含字体的大小的定义和副的刻度值
def changto360(data):
c = data['lon']
c[c<0]=c+360
data['lon']=c
c = data['piomas']
c[c==-1]=np.nan
data['piomas']=c
return data
#该程序为了绘制散点图,随着经度变化而变化
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
fig, host = plt.subplots(figsize=(15, 7), dpi=80)#
fig.subplots_adjust(top=0.8,bottom=0.095,left=0.06,right=0.95)
ship4=pd.read_csv(r"H:\mission\AIS\speed\20_AIS_position_value.csv") #extent_LS.csv
ship4['datetime'] = pd.to_datetime(ship4['datetime'])
ship1=pd.read_csv(r"H:\mission\AIS\speed\2_AIS_position_value.csv") #extent_LS.csv
ship1['datetime'] = pd.to_datetime(ship1['datetime'])
ship2=pd.read_csv(r"H:\mission\AIS\speed\2_back_AIS_position_value.csv") #extent_LS.csv
ship2['datetime'] = pd.to_datetime(ship2['datetime'])
ship3=pd.read_csv(r"H:\mission\AIS\speed\3_AIS_position_value.csv") #extent_LS.csv
ship3['datetime'] = pd.to_datetime(ship3['datetime'])
ship1=changto360(ship1)
ship2=changto360(ship2)
ship3=changto360(ship3)
ship4=changto360(ship4)
plt.scatter(ship1['lon'], ship1['piomas'],label="20210105-20210126",alpha=0.5)
plt.scatter(ship3['lon'], ship3['piomas'],label="20210106-20210128",alpha=0.5)
plt.scatter(ship4['lon'], ship4['piomas'],label="20211224-20210119",alpha=0.5)
plt.scatter(ship2['lon'], ship2['piomas'],label="20210127-20210219",alpha=0.5)
#系统默认为蓝色和橙色
plt.xticks([60,90,120,150,180],#,210
['60°E','90°E','120°E','150°E','180°'],#,'150°W'
fontsize=12)
xminorLocator = MultipleLocator(10) #将x轴次刻度标签设置为10的倍数
host.xaxis.set_minor_locator(xminorLocator)
plt.yticks(fontsize=12)
plt.legend(loc="upper center",bbox_to_anchor=(0.7,1.29),prop={
'weight' : 'normal',
'size' : 15,
})#在散点图右上角显示名字
#plt.xticks(ticks, [labels], **kwargs)
plt.xlabel('longitude(°)',fontdict={'weight': 'normal', 'size': 16})
#plt.ylabel('ship speed(kn)',fontdict={'weight': 'normal', 'size': 16})
plt.ylabel('PIOMAS sea ice thickness(m)',fontdict={'weight': 'normal', 'size': 16})
plt.show()
plt.savefig(r'H:\mission\plot\scatter_piomas_longitude.png', dpi=300)