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 折线图的更多相关文章

  1. 练习: bs4 简单爬取 + matplotlib 折线图显示 (关键词,职位数量、起薪)

    要看一种技术在本地的流行程度,最简单的就是找招聘网站按关键词搜索. 比如今天查到的职位数量是vue 1296个,react 1204个,angular 721个.国际上比较流行的是react,本地市场 ...

  2. python matplotlib 折线图

    1.绘制折线图,去上和右边框,显示中文 import numpy as np import matplotlib.pyplot as plt #plt.style.use('default') #pl ...

  3. matplotlib折线图

    绘制折线图:参考https://baijiahao.baidu.com/s?id=1608586625622704613           (3)近10年GDP变化的曲线图,及三次产业GDP变化的曲 ...

  4. python中matplotlib画折线图实例(坐标轴数字、字符串混搭及标题中文显示)

    最近在用python中的matplotlib画折线图,遇到了坐标轴 "数字+刻度" 混合显示.标题中文显示.批量处理等诸多问题.通过学习解决了,来记录下.如有错误或不足之处,望请指 ...

  5. 用matplotlib.pyplot画简单的折线图,直方图,散点图

    #coding=utf-8 """ 用matplotlib.pyplot画简单的折线图,直方图,散点图 """ import matplot ...

  6. Matplotlib学习---用matplotlib画折线图(line chart)

    这里利用Jake Vanderplas所著的<Python数据科学手册>一书中的数据,学习画图. 数据地址:https://raw.githubusercontent.com/jakevd ...

  7. 06. Matplotlib 2 |折线图| 柱状图| 堆叠图| 面积图| 填图| 饼图| 直方图| 散点图| 极坐标| 图箱型图

    1.基本图表绘制 plt.plot() 图表类别:线形图.柱状图.密度图,以横纵坐标两个维度为主同时可延展出多种其他图表样式 plt.plot(kind='line', ax=None, figsiz ...

  8. 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) ...

  9. python使用matplotlib绘制折线图教程

    Matplotlib是一个Python工具箱,用于科学计算的数据可视化.借助它,Python可以绘制如Matlab和Octave多种多样的数据图形.下面这篇文章主要介绍了python使用matplot ...

随机推荐

  1. php连接数据库并创建数据库表

    先开启本地服务器 1.输入localhost/phpmyadmin 查看本地数据库是否安装 2.在本地服务器上建一个文件夹,里面建一个php文件(如test.php) 3.连接数据库 4.在浏览器上输 ...

  2. Linux 内核驱动结构嵌入

    如同大部分驱动核心结构的情形, device_driver 结构常常被发现嵌到一个更高级的, 总 线特定的结构. lddbus 子系统不会和这样的趋势相反, 因此它已定义了它自己的 ldd_drive ...

  3. 小心Powershell的位数

    我们都知道64位的 Windows 中有两个Powershell,32位的 Windows Powershell(x86)和64位的 Windows Powershell.(当然,32位的Window ...

  4. 【Docker】企业级镜像仓库harbor的搭建(http/https)及使用

    一:用途 Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器. 二:安装docker-ce 环境:阿里云轻量应用服务器CentOS 7.3 这里通过yum Docker源仓 ...

  5. slim中的请求URI

    请求 URI 每个 HTTP 请求都有一个识别被请求的应用程序资源的 URI .HTTP 请求 URI 分为这几部分: Scheme (e.g. http or https) Host (e.g. e ...

  6. JAVA8学习——深入浅出Lambda表达式(学习过程)

    JAVA8学习--深入浅出Lambda表达式(学习过程) lambda表达式: 我们为什么要用lambda表达式 在JAVA中,我们无法将函数作为参数传递给一个方法,也无法声明返回一个函数的方法. 在 ...

  7. python内置模块(python标准库)

    1.time模块 掌握下面三种方式的转换 文件名不能跟系统内置模块重名!   #三种方式的转化关系 #时间戳(timestamp): 为了计时 为了减法计算   import time print(t ...

  8. javaweb-选课系统

    选课系统中用到了4个表,分别是classs.yonghu.teacher.student.在用户中存放管理员的信息name和password以及id,在另三个表中存放对应的数据如图: calss: t ...

  9. Kafka学习笔记(四)—— API使用

    Kafka学习笔记(四)-- API使用 1.Producer API 1.1 消息发送流程 Kafka的Producer发送消息采用的是异步发送的方式.在消息发送的过程中,涉及到了两个线程--mai ...

  10. Mysql 字段类型与约束条件

    一.数值类型 二.日期类型 三.枚举与集合 四.约束条件 五.设置严格模式 一.数值类型 1.1 整型 应用场景: id号.年龄... tinyint: 有符号:默认范围 -128, 127 无符号: ...