matplotlib绘制折线图

 在同一figure同一图表绘制三个城市三条温度变化折线图

import matplotlib.pyplot as plt
import random
plt.rcParams['font.sans-serif'] = ['SimHei'] #显示中文
#创建figure
plt.figure(figsize = (20, 8), dpi = 80)
# 北京,上海,广州冬天11点到12的温度变化折线图
# 准备数据
x  = range(60)
y_beijing = [random.uniform(1, 3) for i in x]
y_shanghai = [random.uniform(15, 20) for i in x]
y_guangzhou  = [random.uniform(20, 25) for i in x]

# 绘制折线图
plt.plot(x, y_shanghai, color = 'b', linestyle = '-', label = '上海')
plt.plot(x, y_beijing, color = 'r', linestyle = '--', label = '北京')
plt.plot(x, y_guangzhou, color = 'g', label = '广州')

#修改刻度
y_ticks = range(40)
x_ch = ["11点{}分".format(i) for i in x]

plt.xticks(x[::5], x_ch[::5])
plt.yticks(y_ticks)


plt.xlabel("时间")
plt.ylabel("温度")
plt.title("北京,上海,广州冬天11点到12的温度变化折线图")

plt.legend(loc='best')

plt.show()

同一figure不同图表绘制不同城市的一条温度变化折线图

import matplotlib.pyplot as plt
import random
plt.rcParams['font.sans-serif'] = ['SimHei']

#创建两行两列的figure
fig, ax = plt.subplots(nrows = 2, ncols = 2, figsize = (20, 8))
# 准备数据
x = range(60)
y_guangzhou = [random.uniform(20, 25) for i in x]
y_shanghai = [random.uniform(10, 15) for i in x]
y_beijing = [random.uniform(1, 3) for i in x]

#绘制折线图
ax[0][0].plot(x, y_guangzhou, color = 'r', label='广州')
ax[0][1].plot(x, y_shanghai, color = 'b', label = "上海")
ax[1][0].plot(x, y_beijing, color = 'g', label = '北京')


#修改刻度
x_ch = ["11点{}分".format(i) for i in x]
y_ticks = range(35)

ax[0][0].set_xticks(x[::5], x_ch[::5])
ax[0][1].set_xticks(x[::5], x_ch[::5])
ax[1][0].set_xticks(x[::5], x_ch[::5])

ax[0][0].set_yticks(y_ticks[::5])
ax[0][1].set_yticks(y_ticks[::5])
ax[1][0].set_yticks(y_ticks[::5])


ax[0][0].set_xlabel("时间")
ax[0][0].set_ylabel("温度")

ax[0][1].set_xlabel("时间")
ax[0][1].set_ylabel("温度")

ax[1][0].set_xlabel("时间")
ax[1][0].set_ylabel("温度")

ax[0][0].set_title("广州11点到12点的温度变化")
ax[0][1].set_title("上海11点到12点的温度变化")
ax[1][0].set_title("北京11点到12点的温度变化")


ax[0][0].legend(loc = 'upper left')
ax[0][1].legend(loc = 'upper left')
ax[1][0].legend(loc = 'upper left')

plt.show()


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

易守年嘉爱

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

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

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

打赏作者

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

抵扣说明:

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

余额充值