包括改变了各个轴及标签的颜色
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 9 20:04:01 2021
@author: 11chen
"""
#该程序为了绘制散点图,即横坐标为时间,纵坐标有四个
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
data=pd.read_csv(r"H:\mission\LNG\AISgfffgfue.csv") #extent_LS.csv
data['datetime'] = pd.to_datetime(data['datetime'])
c = data['piomas']
c[c==-1]=np.nan
data['piomas'].replace(-1,np.nan)
data['piomas']=c
colors = ['black','orange' , 'blue','red','purple']
labels = ['ship speed', 'a','b','c', 'd']
data=data.resample('1T', on='datetime').mean()
data=data.reset_index(drop=False)
data=data.dropna(axis=0,how='any') #drop all rows that have any NaN values
# data.to_csv(r"H:\mission\LNG\AIS\20210104_20210119\csv\209351000_AIS_position_1T.csv")
data=data.resample('12H', on='datetime').mean()
data=data.reset_index(drop=False)
data=data.dropna(axis=0,how='any') #drop all rows that have any NaN values
print(data['speed'][0:17].corr(data['smoscs2'][0:17]))
print(data['speed'][0:17].corr(data['piomas'][0:17]))
def make_patch_spines_invisible(ax):
ax.set_frame_on(True)
ax.patch.set_visible(False)
for sp in ax.spines.values():
sp.set_visible(False)
fig, host = plt.subplots(figsize=(16, 7))#, dpi=300
fig.subplots_adjust(top=0.95,bottom=0.1,left=0.05,right=0.8)#0.74
# par1 = host.twinx()
par2 = host.twinx()
par3 = host.twinx()
par4 = host.twinx()
# Offset the right spine of par2. The ticks and label have already been
# placed on the right by twinx above.
# par2.spines["right"].set_position(("axes", 1.1)) #设置坐标的偏移位置
par3.spines["right"].set_position(("axes", 1.1)) #设置坐标的偏移位置
par4.spines["right"].set_position(("axes", 1.2)) #设置坐标的偏移位置
# Having been created by twinx, par2 has its frame off, so the line of its
# detached spine is invisible. First, activate the frame but make the patch
# and spines invisible.
make_patch_spines_invisible(par2)
make_patch_spines_invisible(par3)
make_patch_spines_invisible(par4)
# Second, show the right spine.
par2.spines["right"].set_visible(True)
par3.spines["right"].set_visible(True)
par4.spines["right"].set_visible(True)
p1, = host.plot(data['datetime'], data['speed'], color=colors[0],linewidth=3,alpha=1, marker='o', label=labels[0])
# p2, = par1.plot(data['datetime'], data['a'], color=colors[1],alpha=0.5, marker='o', label=labels[1])
p3, = par2.plot(data['datetime'], data['b'], color=colors[2],linewidth=3, alpha=1,marker='o', label=labels[2])
p4, = par3.plot(data['datetime'], data['c'], color=colors[3], linewidth=3,alpha=1,marker='o', label=labels[3])
p5, = par4.plot(data['datetime'], data['d'], color=colors[4],linewidth=3, alpha=1,marker='o', label=labels[4])
host.set_xlim(min(data['datetime']), max(data['datetime']))
host.set_ylim(0, 18)
# par1.set_ylim(0, 2.5)
par2.set_ylim(0, 2.5)
par3.set_ylim(0, 2.5)
par4.set_ylim(0, 100)
host.set_xlabel("date and time",fontdict={'weight': 'normal', 'size': 19})
host.set_ylabel(labels[0]+' (kn)',fontdict={'weight': 'normal', 'size': 19})
# par1.set_ylabel(labels[1]+'(m)',fontdict={'weight': 'normal', 'size': 16})
par2.set_ylabel(labels[2]+ ' (m)',fontdict={'weight': 'normal', 'size': 19})
par3.set_ylabel(labels[3]+ ' (m)',fontdict={'weight': 'normal', 'size': 19})
par4.set_ylabel(labels[4]+ ' (%)',fontdict={'weight': 'normal', 'size': 19})
host.yaxis.label.set_color(p1.get_color())
# par1.yaxis.label.set_color(p2.get_color())
par2.yaxis.label.set_color(p3.get_color())
par3.yaxis.label.set_color(p4.get_color())
par4.yaxis.label.set_color(p5.get_color())
host.spines["right"].set_color(p1.get_color())
par2.spines["right"].set_color(p3.get_color())
par3.spines["right"].set_color(p4.get_color())
par4.spines["right"].set_color(p5.get_color())
tkw = dict(size=4, width=1.5,labelsize=16)
host.tick_params(axis='y', colors=p1.get_color(), **tkw)
# par1.tick_params(axis='y', colors=p2.get_color(), **tkw)
par2.tick_params(axis='y', colors=p3.get_color(), **tkw)
par3.tick_params(axis='y', colors=p4.get_color(), **tkw)
par4.tick_params(axis='y', colors=p5.get_color(), **tkw)
host.tick_params(axis='x',**tkw)
lines = [p1, p3,p4,p5]#p2,
# host.legend(lines, [l.get_label() for l in lines],loc="upper center",bbox_to_anchor=(0.5,1.125),prop={ #0.65 1.1
# 'weight' : 'normal',
# 'size' : 20,
# }, ncol=5)
plt.savefig(r'H:\mission\speed_scatter_12H.png', dpi=300)
plt.show()