matplotlib 折线图
1、基本要点
# 导入模块
from matplotlib import pyplot as plt # x轴数据
x = range(2, 26, 2)
# y轴数据
y = [15, 13, 14.5, 17, 20, 25, 26, 26, 27, 22, 18, 15] # 绘图
plt.plot(x, y) # 展示图片
plt.show()
注意: x轴和y轴是可迭代对象,x轴和y轴的数值必须一一对应, x和y中的值必须是数子
2、设置图行大小
plt.figure(figsize=(宽, 高), dpi=80)
# 注意:设置图形大小,要在绘图的前面
3、保存图片
plt.savefig('./名称.png')
# 注意:
# 保存图片要在,绘图之后
# 可以保存为矢量图(svg)
4、调整x轴或y轴上的刻度
plt.xticks(可迭代对象)
plt.yticks(可迭代对象)
# 注意:
# 调整x轴或y轴的刻度,要放在保存或展示图片的前面
例子
# 导入模块
from matplotlib import pyplot as plt # x轴数据
x = range(2, 26, 2)
# y轴数据
y = [15, 13, 14.5, 17, 20, 25, 26, 26, 27, 22, 18, 15] # 设置图形大小
plt.figure(figsize=(20, 8), dpi=80) # 绘图
plt.plot(x, y) # 调整x轴的刻度
plt.xticks(x)
# 调整y轴的刻度
plt.yticks(range(min(y), max(y) + 1)) # 保存图片
# plt.savefig('./test.svg')
# 展示图片
plt.show()
5、调整x轴或y轴的刻度并显示标签
plt.xticks(ticks, labels, rotation=45)
# 注意
# ticks和labels的数据类型最好是列表,方便调整刻度
# ticks和labels即数字和字符串要一一对应,步长也要一致
# rotation调整标签的旋转
例子
import random
from matplotlib import pyplot as plt y = [random.randint(20, 35) for i in range(120)] x = range(120) # 设置图形大小
plt.figure(figsize=(20, 8), dpi=80) # 绘图
plt.plot(x, y) # 设置x轴刻度
x = list(x)
x_labels = ['10H:{}M'.format(i) for i in range(60)]
x_labels += ['11H:{}M'.format(i) for i in range(60)]
plt.xticks(x[::3], x_labels[::3], rotation=45)
# 设置y轴刻度
plt.yticks(range(min(y), max(y) + 1)) # 展示图片
plt.show()
6、显示中文
a、查看字体
linux/mac查看支持的字体
fc-list # 查看支持的字体
fc-list :lang=Zhang # 查看支持的中文(冒号前面加空格) windows查看支持的字体
Win+R, 输入fonts
获取字体路径:
推荐使用git bash
查看字体
cd 'C:\Windows\Fonts'
ls -l
一般使用 msyh.ttc
b、设置
修改matplotlib的默认字体
1.matplotlib.rc,具体看源码(windows/linux),不是百分百成功
2.matplotlib下的font_manager(windwos/linux/mac),百分百成功
# 导入模块
from matplotlib import font_manager
# 设置显示中文
my_font = font_manager.FontProperties(fname='C:\Windows\Fonts\msyh.ttc')
# 添加fontproperties=my_font的属性
plt.xticks(x[::3], x_labels[::3], rotation=45, fontproperties=my_font) # 注意: 设置中文显示时
# 只有plt.legend, 使用prop
# 其它使用fontproperties
例子
import random
from matplotlib import pyplot as plt
from matplotlib import font_manager # 设置显示中文
my_font = font_manager.FontProperties(fname='C:\Windows\Fonts\msyh.ttc') y = [random.randint(20, 35) for i in range(120)] x = range(120) # 设置图形大小
plt.figure(figsize=(20, 8), dpi=80) # 绘图
plt.plot(x, y) # 设置x轴刻度,显示标签(中文)
x = list(x)
x_labels = ['10时:{}分'.format(i) for i in range(60)]
x_labels += ['11时:{}分'.format(i) for i in range(60)]
plt.xticks(x[::3], x_labels[::3], rotation=45, fontproperties=my_font)
# 设置y轴刻度
plt.yticks(range(min(y), max(y) + 1)) # 展示图片
plt.show()
7、设置描述信息
# 1. 设置x轴的label
plt.xlabel()
# 2. 设置y轴的label
plt.ylabel()
# 3. 设置title
plt.title() # 注意
# 设置描述信息要在保存、展示的前面
# 显示中文用fontproperties
例子
# 设置中文字体
my_font = font_manager.FontProperties(fname='C:\Windows\Fonts\msyh.ttc')
# 描述信息
plt.xlabel('时间', fontproperties=my_font)
plt.ylabel('温度 单位(℃)', fontproperties=my_font)
plt.title('10点到12点每分钟的温度变化', fontproperties=my_font)
8、添加网格
plt.grid(alpha=0.5)
# alpha网格的alpha值(0~1)
# 在展示图片和保存图片前
9、添加图例
# 中文显示
my_font = font_manager.FontProperties(fname='C:\Windows\Fonts\msyh.ttc')
# 绘图
plt.plot(x, y, label='该折线的描述名称')
# 添加图例
plt.legend(prop=my_font, loc='') # 注意: loc参数设置图例的位置,具体看源码,一般默认即可
# best
# upper right
# upper left
# lower left
# lower right
# right
# center left
# center right
# lower center
# upper center
# center
10、自定义绘图的风格
plt.plot(color='', linestyle='', linewidth= , alpha= )
# color 线条颜色
# 16进制的颜色代码
# linestyle 线条风格
# - 实线
# -- 虚线
# -. 点划线
# : 点虚线
# linewidth 线条粗细
# alpha 透明度
# 注意:这些风格对网格(grid())同样有效
例子
# -*- encoding: utf-8 -*- from matplotlib import pyplot as plt
from matplotlib import font_manager #
# 设置中文字体
my_font = font_manager.FontProperties(fname='C:\Windows\Fonts\msyh.ttc') y1 = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
y2 = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
x = range(11, 31)
# 设置图形大小
plt.figure(figsize=(20, 8), dpi=80)
# 绘图
plt.plot(x, y1, label='自己', linestyle=':')
plt.plot(x, y2, label='敌人', color='#00FF00') # 设置x轴y刻度
x = list(x)
x_labels = ['{}岁'.format(i) for i in x]
plt.xticks(x, x_labels, fontproperties=my_font)
plt.yticks(range(0, 7))
# 设置描述信息
plt.xlabel('年龄', fontproperties=my_font)
plt.ylabel('人数', fontproperties=my_font)
plt.title('11岁到30岁每年交的女朋友', fontproperties=my_font) # 绘制网格
plt.grid(alpha=0.8, linestyle=':', color='#DC143C') # 添加图例
plt.legend(prop=my_font) plt.show()
matplotlib 折线图的更多相关文章
- 练习: bs4 简单爬取 + matplotlib 折线图显示 (关键词,职位数量、起薪)
要看一种技术在本地的流行程度,最简单的就是找招聘网站按关键词搜索. 比如今天查到的职位数量是vue 1296个,react 1204个,angular 721个.国际上比较流行的是react,本地市场 ...
- python matplotlib 折线图
1.绘制折线图,去上和右边框,显示中文 import numpy as np import matplotlib.pyplot as plt #plt.style.use('default') #pl ...
- matplotlib折线图
绘制折线图:参考https://baijiahao.baidu.com/s?id=1608586625622704613 (3)近10年GDP变化的曲线图,及三次产业GDP变化的曲 ...
- python中matplotlib画折线图实例(坐标轴数字、字符串混搭及标题中文显示)
最近在用python中的matplotlib画折线图,遇到了坐标轴 "数字+刻度" 混合显示.标题中文显示.批量处理等诸多问题.通过学习解决了,来记录下.如有错误或不足之处,望请指 ...
- 用matplotlib.pyplot画简单的折线图,直方图,散点图
#coding=utf-8 """ 用matplotlib.pyplot画简单的折线图,直方图,散点图 """ import matplot ...
- Matplotlib学习---用matplotlib画折线图(line chart)
这里利用Jake Vanderplas所著的<Python数据科学手册>一书中的数据,学习画图. 数据地址:https://raw.githubusercontent.com/jakevd ...
- 06. Matplotlib 2 |折线图| 柱状图| 堆叠图| 面积图| 填图| 饼图| 直方图| 散点图| 极坐标| 图箱型图
1.基本图表绘制 plt.plot() 图表类别:线形图.柱状图.密度图,以横纵坐标两个维度为主同时可延展出多种其他图表样式 plt.plot(kind='line', ax=None, figsiz ...
- python用matplotlib画折线图
折线图: import matplotlib.pyplot as plt y1=[10,13,5,40,30,60,70,12,55,25] x1=range(0,10) x2=range(0,10) ...
- python使用matplotlib绘制折线图教程
Matplotlib是一个Python工具箱,用于科学计算的数据可视化.借助它,Python可以绘制如Matlab和Octave多种多样的数据图形.下面这篇文章主要介绍了python使用matplot ...
随机推荐
- angular 全局常用指令
1.全局支持 enter快捷键触发事件 // 全局指令 app.directive('ngEnter', ['$window',"$timeout", ($window,$time ...
- BAT 脚本判断当前系统是 x86 还是 x64 系统
本文告诉大家在写 BAT 脚本的时候,如何判断当前的系统是 32 位系统的还是 64 位系统 通过注册表进行判断方法 @echo OFF reg Query "HKLM\Hardware\D ...
- excel转换成实体
package com.cinc.ecmp.utils; import java.io.IOException; import java.io.InputStream; import java.lan ...
- Spring Security学习笔记-自定义Spring Security过滤链
Spring Security使用一系列过滤器处理用户请求,下面是spring-security.xml配置文件. <?xml version="1.0" encoding= ...
- Oracle生成批量清空表数据脚本
select 'DELETE FROM ' || a.table_name || '; --' || a.comments from user_tab_comments a where a.table ...
- mac系统上访问docker容器中的ip配置
使用 mac系统,发现docker没有 docker0网桥,无法直接在宿主机上 访问 容器的ip, 在测试的时候有这种需求,而不是通过-p的方式,可以参考下面的连接,主要就是 修改 setting.j ...
- kafka性能测试代码
bin/kafka-producer-perf-test.sh --num-records 5000000 --record-size 5000 \ --topic kafkatopic2 \ --b ...
- HDU1166 敌兵布阵 BZOJ1012 最大数[树状数组]
一.前置知识-树状数组 树状数组(binary indexed tree)是一种简洁的代码量很小的数据结构,能够高效的处理前缀区间上的问题.在很多情况下能写树状数组解决的就不用码半天线段树了. 树状数 ...
- java面试-java动态代理和cglib代理
代理模式就是为了提供额外或不同的操作,而插入的用来替代实际对象的对象,这些操作涉及到与实际对象的通信,因此代理通常充当中间人角色 一.java动态代理 java动态代理可以动态地创建代理并动态 ...
- Linux忘记root密码后如何在grub界面中以单用户模式进入系统并重置密码的方法
本文将介绍在Linux系统中忘记root用户密码的情况下,如何在gurb界面进入单用户模式并重置root用户密码.在单用户模式下,用户不需要输入任何密码即可进入系统并可以修改密码.实验步骤如下: 1. ...