【Python数据分析 - 2】:多个坐标系的绘制

多个坐标系绘制的代码

本文无过多讲解,大部分为代码,提供了解。

import matplotlib.pyplot as plt
import random

# 设置字体
plt.rcParams['font.sans-serif'] = ['SimHei']

# 创建fig和axes对象,nrow是行,ncol是列,这里表示设置一行二列的坐标系
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 8))
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
y_beijing = [random.uniform(1, 3) for i in x]

# 构造中文
x_ch = ['11点{}分'.format(i) for i in x]
y_ticks = range(40)

# plt是对整体的画图,axes是对每个坐标系进行处理
# axes[0]表示为第一个坐标系,0为索引值
axes[0].plot(x, y_shanghai, label='上海')
axes[1].plot(x, y_beijing, label='北京', linestyle='--', color='r')

# 修改x,y轴坐标的刻度显示,注意使用subplot方法创建的图表不能直接使用set_sticks方法传入自定义的刻度,必须使用set_sticklabels()方法
axes[0].set_xticklabels(x_ch[::5])
axes[1].set_xticklabels(x_ch[::5])
axes[0].set_yticks(y_ticks[::5])
axes[1].set_yticks(y_ticks[::5])

# 设置坐标轴名称、标头
axes[0].set_xlabel('时间')
axes[1].set_xlabel('时间')
axes[0].set_ylabel('温度')
axes[1].set_ylabel('温度')

axes[0].set_title('上海温度变化情况')
axes[1].set_title('北京温度变化情况')

axes[0].legend(loc='best')
axes[1].legend(loc='best')

# 展示绘图结果
plt.show()

绘图结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

街 三 仔

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

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

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

打赏作者

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

抵扣说明:

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

余额充值