Matplotlib实践之折线图练习

需求1:

绘制10点到12点每分钟的气温, 如何绘制折线图观察每分钟气温的变化情况?
temps = [random.randint(20, 35) for i in range(120)]

from matplotlib import font_manager
from matplotlib import pyplot as plt
myfont = font_manager.FontProperties(fname="/usr/share/fonts/cjkuni-uming/uming.ttc", size=18)
titlefont = font_manager.FontProperties(fname="/usr/share/fonts/cjkuni-uming/uming.ttc", size=14)
#图表的x轴数据,是一个可迭代的数据类型
x_times = range(0, 24, 2)
#图表的y轴数据是一个可迭代数据类型
y_temp = [13, 11, 12, 22, 32, 33, 31, 41,54, 35, 56, 14]

plt.figure(figsize=(10, 10))
#传入x和y轴的数据,绘制图形
plt.plot(x_times, y_temp)
plt.title("每天的气温变化(每隔两个小时)", fontproperties=titlefont)
plt.xlabel("时间", fontproperties=myfont )
plt.ylabel("温度", fontproperties=myfont)
plt.xticks(x_times,labels=["%s时"%(i) for i in x_times], fontproperties=myfont)
plt.savefig('doc/temp.png')
#在执行程序时显示图像
plt.show()

在这里插入图片描述

需求2:假设大家在30岁的时候,根据自己的实际情况统计出来从11岁到30岁每年交的女
(男)朋友的数量如a,请绘制出该数据的折线图,以便分析每年交女(男)朋友的数量走势;

import random
from matplotlib import pyplot as plt
from matplotlib import font_manager

x_age = range(11, 31)
y_count = [random.randint(0, 5) for i in range(20)]
myfont = font_manager.FontProperties(fname="/usr/share/fonts/cjkuni-uming/uming.ttc", size=14)
titlefont = font_manager.FontProperties(fname="/usr/share/fonts/cjkuni-uming/uming.ttc", size=20)

plt.figure(figsize=(10, 10))
plt.plot(x_age, y_count)
plt.title("11岁至30岁所交男(女)友个数", fontproperties=titlefont)
plt.xlabel("年龄", fontproperties=myfont)
plt.ylabel("女(男)友数量", fontproperties=myfont)


plt.xticks(x_age, labels=["%s岁" %(item) for item in x_age], fontproperties=myfont, rotation
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值