Matplotlib 绘制折线图
Matplotlib
matplotlib: 最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建
绘制折线图
绘制两小时的温度变化
from matplotlib import pyplot as plt
x = range(2, 26, 2)
y = [15, 13, 14, 5, 17, 20, 25, 26, 24, 22, 19, 15]
# 全局变量fig figure方法(figsize画板尺寸=(长float,宽float), dpi=整数int)
fig = plt.figure(figsize=(20, 8), dpi=80)
# 传入X坐标和Y坐标,并绘图
plt.plot(x, y)
# 调整X轴刻度,传入的列表密集与否,决定X轴的密集程度
plt.xticks(x)
# 调整X轴刻度同理
plt.yticks(range(min(y), max(y)+1))
# savefig方法,传入一个路径
plt.savefig("./t1.png")
# 展示
plt.show()

from matplotlib import pyplot as plt
import random
from matplotlib import font_manager
x = range(0, 120)
y = [random.randint(20, 35) for i in range(120)]
# C:\Windows\Fonts\simhei.ttf 字体路径
# 实例化font_manager
my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\simhei.ttf")
plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y)
# 调整x轴的刻度
_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i) for i in range(60)]
# 取步长,数字和字符串一一对应,数据的长度与数据的刻度一笔一致
# param: rotation=旋转角度(int)
# param: fontproperties=实例化的字体(object)
plt.xticks(list(x)[::3], _xtick_labels[::3],
rotation=45, fontproperties=my_font)
# 添加描述信息
plt.xlabel("时间", fontproperties=my_font) # X轴标签
plt.ylabel("温度 ℃", fontproperties=my_font) # Y轴标签
plt.title("10点到12点每分钟的气温变化情况", fontproperties=my_font) # 标题
plt.show()

模拟11岁到30岁自己和同桌交女朋友的走势
# coding=utf-8
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\simhei.ttf")
y_1 = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
y_2 = [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, y_1, label="自己", color="red")
plt.plot(x, y_2, label="同桌", color="#DB7093", linestyle="--")
# 设置x轴刻度
_xtick_labels = ["{}岁".format(i) for i in x]
plt.xticks(x, _xtick_labels, fontproperties=my_font)
# plt.yticks(range(0,9))
# 绘制网格
plt.grid(alpha=0.4, linestyle=':')
# 添加图例
plt.legend(prop=my_font, loc="upper left")
# 展示
plt.show()

绘制折线图总结

Matplotlib 绘制折线图的更多相关文章
- python使用matplotlib绘制折线图教程
Matplotlib是一个Python工具箱,用于科学计算的数据可视化.借助它,Python可以绘制如Matlab和Octave多种多样的数据图形.下面这篇文章主要介绍了python使用matplot ...
- Python开发【模块】:matplotlib 绘制折线图
matplotlib 1.安装matplotlib ① linux系统安装 # 安装matplotlib模块 $ sudo apt-get install python3-matplotlib # 如 ...
- 【Python】matplotlib绘制折线图
一.绘制简单的折线图 import matplotlib.pyplot as plt squares=[1,4,9,16,25] plt.plot(squares) plt.show() 我们首先导入 ...
- 1 matplotlib绘制折线图
from matplotlib import pyplot as plt #设置图形大小 plt.figure(figsize=(20,8),dpi=80) plt.plot(x,y,color=&q ...
- matplotlib之折线图
1.案例一 # coding=utf-8 from matplotlib import pyplot as plt import random # 设置字体相关 from matplotlib imp ...
- Python绘制折线图
一.Python绘制折线图 1.1.Python绘制折线图对应代码如下图所示 import matplotlib.pyplot as pltimport numpy as np from pylab ...
- 用canvas绘制折线图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- php中用GD绘制折线图
php中用GD绘制折线图,代码如下: Class Chart{ private $image; // 定义图像 private $title; // 定义标题 private $ydata; // 定 ...
- python中matplotlib画折线图实例(坐标轴数字、字符串混搭及标题中文显示)
最近在用python中的matplotlib画折线图,遇到了坐标轴 "数字+刻度" 混合显示.标题中文显示.批量处理等诸多问题.通过学习解决了,来记录下.如有错误或不足之处,望请指 ...
- UUChart的使用--iOS绘制折线图
UUChart是一个用于绘制图表的第三方,尤其适合去绘制折线图. 二.下载地址: https://github.com/ZhipingYang/UUChartView 三.使用 第一步.首先我们将下载 ...
随机推荐
- vue中将验证表单输入框的方法写在一个js文件中(表达式验证邮箱、身份证、号码、两次输入的密码是否一致)
文章目录 1.实现的效果 2.编写的js文件(这里写在了api文件下) 3.在vue页面中引入(script) 4.页面代码 1.实现的效果 20220606_154646 2.编写的js文件(这里写 ...
- 2、yaml配置文件当中的坑(数字的定义和支持进制书写格式)
6.进制数的转换 6.1.问题回顾 我记得我刚刚开始使用SpringBoot的时候,有一天在做到SpringBoot整合第三方技术的时候 我刚好在那天学习到整合Mybatis,做Web项目嘛,不连数据 ...
- C语言整人关机程序
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char input ...
- 云原生之旅 - 4)基础设施即代码 使用 Terraform 创建 Kubernetes
前言 上一篇文章我们已经简单的入门Terraform, 本篇介绍如何使用Terraform在GCP和AWS 创建Kubernetes 资源. Kubernetes 在云原生时代的重要性不言而喻,等于这 ...
- 介绍一个jmeter录制脚本谷歌插件 —— metersphere-chrome-plugin
该插件可将用户在浏览器操作时的 HTTP 请求记录下来并生成 JMX 文件(JMeter 脚本文件). 1. 插件解压 插件下载链接: https://pan.baidu.com/s/14nGb_s9 ...
- promise 的串行执行
function pri (num) { return new Promise((resolve,reject) => { console.log('开始'+num) res ...
- Java开发学习(四十一)----MyBatisPlus标准数据层(增删查改分页)开发
一.标准CRUD使用 对于标准的CRUD功能都有哪些以及MyBatisPlus都提供了哪些方法可以使用呢? 我们先来看张图: 1.1 环境准备 这里用的环境就是Java开发学习(四十)----MyBa ...
- vue3的学习笔记:MVC、Vue3概要、模板、数据绑定、用Vue3 + element ui、react框架实现购物车案例
一.前端MVC概要 1.1.库与框架的区别 框架是一个软件的半成品,在全局范围内给了大的约束.库是工具,在单点上给我们提供功能.框架是依赖库的.Vue是框架而jQuery则是库. 1.2.MVC(Mo ...
- nginx日志切割并备份
[root@lecode-pre55 bin]# cat nginx-log.sh #!bin/bash #auther:ansheng #desc: nginx日志备份,注意脚本中文件的路径. #备 ...
- 大前端html基础学习02
CSS核心属性 一.css属性和属性值的定义 属性:属性是指定选择符所具有的属性,它是css的核心. 属性值:属性值包括法定属性值及常见的数值加单位,如25px,或颜色值等. 二.CSS文本属性 1. ...