matplotlib 基础

数据可视化
一,简单折线图

from matplotlib import pyplot as plt

fig=plt.figure(figsize=(20,8),dpi=80)

x = range(2, 26, 2)
# 数据在x轴的位置,是一个可迭代对象
y = [15, 13, 14.5, 17, 20, 25, 26, 26, 24, 22, 18, 14]
# 数据在y轴的位置,是一个可迭代对象。x轴和y轴的数据一起组成了所有要绘制出的坐标,
plt.savefig('./sig_size1.png')#保存图片
plt.plot(x, y) # 传入x和y,通过plot绘制出折线图
# plt.xticks(x[::2])#设置刻度
plt.xticks(x)
plt.show()

在这里插入图片描述
二,绘制10点到12点每分钟的温度变化情况

import random
import matplotlib
from matplotlib import pyplot as plt
from matplotlib import font_manager
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['font.family'] = 'sans-serif'
#楷体KaiTi,微软雅黑Microsoft YaHei
plt.figure(figsize=(20, 8))
x = range(120)

random.seed(10)#设置随机种子,让不同时候的随机结果都一样
y = [random.randint(20, 35) for i in range(120)]
plt.plot(x, y)

_x_tickets = ['10点{}分'.format(i) for i in x if i < 60]
_x_tickets += ['11点{}分'.format(i - 60) for i in x if i > 60]#设置x轴上的字符串的刻度

my_font=font_manager.FontProperties(fname='C:\Windows\Fonts\BRUSHSCI.TTF')#中文
plt.xticks(x[::5], _x_tickets[::5], rotation=0)
# plt.rcParams['font.sans-serif']=['SimHei']
plt.xlabel('时间',fontproperties=my_font)
plt.ylabel('温度',fontproperties=my_font)
plt.title('10点到12点每分钟的温度变化情况',fontproperties=my_font)
plt.show()

结果如图:
在这里插入图片描述
三,11-30岁交男女朋友数量变化趋势对比:

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

my_font = font_manager.FontProperties(fname='C:\Windows\Fonts\simsun.ttc')

x = range(11, 31, 1)
y1 = [1, 2, 5, 3, 4, 2, 1, 0, 0, 3, 9, 1, 2, 0, 3, 1, 2, 1, 2,1]
y2 = [3, 1, 1, 0, 0, 0, 1, 1, 2, 3, 1, 5, 4, 3, 1, 2, 0, 1, 0,2]

_x_tickets = ['{}岁'.format(i) for i in x]
fig = plt.figure(figsize=(20, 8), dpi=80)
plt.xticks(x, _x_tickets, rotation=45, fontproperties=my_font)
plt.plot(x, y1, color='blue', linewidth='3.0', label='自己')
plt.plot(x, y2, color='red', linewidth=1.0, linestyle='--', label='同桌')
# plt.xticks(x[::1])
plt.xlabel('年龄', fontproperties=my_font)
plt.ylabel('个数', fontproperties=my_font)
plt.grid(alpha=0.9)
plt.legend(prop=my_font, loc='best')
plt.title('30岁之前交友数量对比', fontproperties=my_font)
plt.show()

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值