折线图与散点图的绘制

折线图的绘制

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

import random

from matplotlib import pyplot as plt
from matplotlib import font_manager
# 4). 中文显示乱码问题;
# 可以通过fc-list :lang=sh 来查看中文格式
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=24)
# 图表的x轴的数据, 是一个可迭代的数据类型
x_times = range(0,120)
# 图表的y轴的数据, 是一个可迭代的数据类型
y_temp = [random.randint(20, 35) for i in range(120)]

# min(y_temp), max(y_temp)

#  1). 如何设置图片的大小;
plt.figure(figsize=(20, 20))

# 传入x和y轴的数据, 绘制图形;
plt.plot(x_times, y_temp)

# 3). x轴和y轴的描述信息;
plt.title("10点到11点温度变化)",fontproperties=titlefont )
plt.xlabel("时间", fontproperties=myfont)
plt.ylabel("温度", fontproperties=myfont)

# 5). 调整x轴和y轴的刻度;
# 6). x轴的刻度信息过长, 如何调整?

# 10时10分
# 11时10分
_x_labels = ["10时%s分" %(i) for i in range(0, 60, 10)]
_x_labels += ["11时%s分" %(i) for i in range(0, 60, 10)]
plt.xticks(x_times[::10], labels=_x_labels, fontproperties=myfont, rotation=45)
y_temp_range = range(min(y_temp), max(y_temp)+1, 2)
plt.yticks(y_temp_range, labels=["%s 。C"%(i) for i in y_temp_range], fontproperties=myfont)

# #
# plt.scatter(x_times[2], y_temp[2], color='b')
# plt.scatter(x_times[2], y_temp[2], color='', marker='o', edgecolors='r', s=300)

# 2). 如何保存到本地;
plt.savefig('doc/temp3.png')

# 在执行程序时显示图像
# plt.show()

散点图的绘制

案例: 绘制北京3,10月份每天白天的最高气温随时间(天)变化的散点图,并找出规律
数据来源:天气网 http://lishi.tianqi.com/beijing/index.html
a = [11,17,16,11,12,11,12,6,6,7,8,9,12,15,14,17,18,21,16,17,20,14,15,15,15,19,21,22,22,22,23]
b = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,13,12,13,6]

from matplotlib import pyplot as plt
from matplotlib import font_manager

#  中文显示乱码问题;
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=24)

# 图表的x轴的数据, 是一个可迭代的数据类型
x_march = range(1, 32)
x_oct = range(50, 81)

# 图表的y轴的数据, 是一个可迭代的数据类型
y_temp_march = [11, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21, 16, 17, 20, 14, 15, 15, 15, 19, 21,
                22, 22, 22, 23]
y_temp_oct = [26, 26, 28, 19, 21, 17, 16, 19, 18, 20, 20, 19, 22, 23, 17, 20, 21, 20, 22, 15, 11, 15, 5, 13, 17, 10, 11,
              13, 12, 13, 6]

#   如何设置图片的大小;
plt.figure(figsize=(30, 10))

# 传入x和y轴的数据, 绘制图形;
plt.scatter(x_march, y_temp_march, label="3月的温度变化", color='r', alpha=0.5)
plt.scatter(x_oct, y_temp_oct, label="10月的温度变化", color='g', alpha=0.5)

# 3). x轴和y轴的描述信息;
plt.title("北京3,10月份每天白天的最高气温随时间(天)变化的散点图", fontproperties=titlefont)
plt.xlabel("时间", fontproperties=myfont)
plt.ylabel("温度", fontproperties=myfont)

# 5). 调整x轴和y轴的刻度;
# 6). x轴的刻度信息过长, 如何调整?
_x_info = list(x_march) + list(x_oct)
_x_labels_march = ["3月%s日" % (i) for i in x_march]
_x_labels_oct = ["10月%s日" % (i - 49) for i in x_oct]  # range(50, 81)
plt.xticks(_x_info[::3], labels=(_x_labels_march + _x_labels_oct)[::3], fontproperties=myfont, rotation=45)

plt.legend(prop=myfont, loc="upper left")
plt.grid(alpha=0.5)

# 2). 如何保存到本地;
plt.savefig('doc/scatter.png')

# 在执行程序时显示图像
# plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值