Python实现可视化(一)--双Y轴画图及图例控制

Python实现可视化(一)-双Y轴画图及图例控制

python实现可视化

基于python绘制双Y轴图像

1.图例与Y轴一致

// 加载相关库
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(15, 9))

// 左侧坐标
ax1 = fig.add_subplot(111)
ax1.plot(df['ft_5min'], df['speed'], '-', color='b', label='平均速度变化')
ax1.plot(df['ft_5min'], df['density'], '-', color='r',label='平均密度变化')
ax1.set_ylabel('速度(km/h) & 密度(辆/公里车道)')
ax1.legend(loc=2)
// 右侧坐标
ax2 = ax1.twinx()
ax2.plot(df['ft_5min'], df['volume'], '-', color='orange',label='交通量变化')
ax2.set_ylabel('交通量(辆/小时/车道)')
ax2.set_xlabel('Same')
ax2.legend(loc=1)

plt.show()

结果:左侧坐标轴对应左侧图例,右侧坐标轴对应右侧图例在这里插入图片描述

2.图例放在一起

// 加载相关库
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(15, 9))
ax1 = fig.add_subplot(111)
lns1 = ax1.plot(df['ft_5min'], df['speed_repair'], '-*', color='b', label='平均速度变化')
lns2 = ax1.plot(df['ft_5min'], df['density'], '-*', color='r',label='平均密度变化')
ax1.set_ylabel('速度(km/h) & 密度(辆/公里车道)')

ax2 = ax1.twinx()
lns3 = ax2.plot(df['ft_5min'], df['volume'], '-*', color='orange', label='交通量变化')
ax2.set_ylabel('交通量(辆/小时/车道)')
ax2.set_xlabel('Same')

// 图例控制
lns = lns1 + lns2 + lns3
labs = [l.get_label() for l in lns]
ax1.legend(lns, labs, loc=0, fontsize=14)
plt.show()

结果为
在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值