matplotlib学习笔记

用matplotlib画图

1.绘制折线图

import random
from matplotlib import pyplot as plt
x = range(120)
y = [random.randint(20,35) for i in range(120)
plt.plot(x,y)
plt.show()

更换图片质量

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

更换横纵坐标

plt.xticks(range(0,121,10))

保存

plt.savefig("fig1.png")

在这里插入图片描述

横坐标字符串形式

由于matpotlib不支持中文格式,会导致乱码问题,采用系统自带的字体。

my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/STSONG.TTF")
#更换横纵坐标
_x = list(x)[::10]
_xtick_lables = ["10点{}分".format(i) for i in range(60)]
_xtick_lables += ["11点{}分".format(i) for i in range(60)]
plt.xticks(_x,_xtick_lables[::10],rotation=45,fontproperties=my_font) #斜着字体,步长为10

在这里插入图片描述

添加描述信息

#添加描述信息
plt.xlabel("时间",fontproperties=my_font)
plt.ylabel("温度 单位('C)",fontproperties=my_font)

绘制网格

#绘制网格
plt.grid()

一个小例子

from matplotlib import pyplot as plt, font_manager
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/STSONG.TTF")
x = range(11,31)
y = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
plt.figure(figsize=(20,8),dpi=80)
_xlables = ["{}岁".format(i) for i in range(11,31)]
plt.xticks(list(x),_xlables,fontproperties=my_font)
plt.xlabel("time")
plt.ylabel("count")
plt.plot(x,y)
plt.show()

在这里插入图片描述

添加图例

plt.legend(prop=my_font)
from matplotlib import pyplot as plt, font_manager

my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/STSONG.TTF")
x = range(11,31)
y1 = [1,0,1,1,2,5,3,2,3,8,4,1,4,5,4,3,3,2,1,1]
y2 = [0,0,2,1,2,4,3,8,3,4,2,5,6,6,4,3,3,1,1,5]
plt.figure(figsize=(20,8),dpi=80)
#绘制网格
plt.grid(alpha=0.1)
plt.plot(x,y1,label="自己",color="orange",linestyle=":")
plt.plot(x,y2,label="同桌")
#添加图例
plt.legend(prop=my_font)
_xlables = ["{}岁".format(i) for i in range(11,31)]
plt.xticks(list(x),_xlables,fontproperties=my_font)
plt.xlabel("time")
plt.ylabel("count")
plt.show()

在这里插入图片描述

2.绘制散点图

主要变化为scatter()这个函数

from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/STSONG.TTF")
y_3 = [11,17,16,11,12,11,12,6,6,7,5,8,9,12,15,14,17,18,21,16,17,18,21]
y_10 = [10,12,14,15,19,14,15,1,12,14,4,23,24,15,16,48,15,11,17,22]
x = range(0,23)
x2 = range(50,70)
plt.figure(figsize=(20,8),dpi=80)
plt.scatter(x,y_3)
plt.scatter(x2,y_10)
#调整x轴的刻度
_x = list(x)+list(x2)
_xlabels = ["3月{}日".format(i) for i in x]
_xlabels += ["10月{}日".format(j) for j in y_10]
plt.xticks(_x[::3],_xlabels[::3],fontproperties=my_font,rotation=45)
#添加描述信息
plt.ylabel("温度",fontproperties=my_font)
plt.xlabel("时间",fontproperties=my_font)
plt.show()

在这里插入图片描述

3.绘制条形图

a = [1, 2, 3, 4, 5]
b = [56.2,50.8,20.5,52.6,33.4]
plt.bar(range(len(a)),b)
plt.show()

在这里插入图片描述

from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/STSONG.TTF")

a = [1, 2, 3, 4, 5]
b = [56.2,50.8,20.5,52.6,33.4]
plt.barh(range(len(a)),b,height=0.3)
plt.show()

在这里插入图片描述

绘制条形图的小例子

from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/STSONG.TTF")

a = ["猩球崛起3:终极之战","敦刻尔克","蜘蛛侠:英雄归来","战狼2"]
b_16 = [15746,312,4497,319]
b_15 = [12357,156,2045,168]
b_14 = [2358,399,2358,362]
y = list(range(len(a)))
#设置图形大小
plt.figure(figsize=(20,8),dpi = 80)
plt.bar(range(len(a)),b_14,width=0.2,label="9月14日")
plt.bar([i + 0.2 for i in y],b_15,width=0.2,label="9月15日")
plt.bar([i+0.4 for i in y],b_16,width=0.2,label="9月16日")

plt.xticks([i + 0.2 for i in y],a,fontproperties=my_font)
#添加图例
plt.legend(prop=my_font)
plt.show()

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值