python 散点图 多个Y轴

 包括改变了各个轴及标签的颜色

# -*- 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()

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

就是一只白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值