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 三.使用 第一步.首先我们将下载 ...
随机推荐
- 5.-GET请求和POST请求
一.定义 无论是GET请求还是POST,统一由视图函数接收请求,通过判定request.method区分具体的请求动作 二.GET处理 GET请求方式中,如果有数据需要传递给服务器,通常会用查 ...
- Codeforces Round #830 (Div. 2) A-D
比赛链接 A 题解 知识点:贪心,数论. 先求出序列最大公约数 \(d\) ,如果为 \(1\) 直接输出 \(0\) . 否则,尝试用最后一个数操作, \(gcd(d,n) = 1\) 则可以,花费 ...
- VUE2 学习(推荐直接学习VUE3)
概念区分: 前端框架:Vue.AngularJS.React 界面模板:Bootstrap.easyUI.adminlte 学习地址: b站:https://space.bilibili.com/39 ...
- VM虚拟机搭建Linux CentOS7(手把手教程)
VM虚拟机搭建Linux CentOS7(手把手教程) 目录 VM虚拟机搭建Linux CentOS7(手把手教程) 一.VM虚拟机和Linux镜像文件下载 1. 登录VM虚拟机官方地址: 2. 安装 ...
- 一个超经典 WinForm 卡死问题的再反思
一:背景 1.讲故事 这篇文章起源于昨天的一位朋友发给我的dump文件,说它的程序出现了卡死,看了下程序的主线程栈,居然又碰到了 OnUserPreferenceChanged 导致的挂死问题,真的是 ...
- LoadRunner11脚本关联+运行负载+分析结果
一.脚本创建关联和插入检查点 脚本录制完成后,首先需运行脚本回放,验证是否可回放成功,然后找出各事务请求中的关联点! 如本例子中,录制的场景为:打开综合窗口收件-->查询事项-->窗口登记 ...
- KeeWiDB的高性能修炼之路:架构篇
数据也有冷热之分,你知道吗? 根据访问的频率的高低可将数据分为热数据和冷数据,访问频率高的则为热数据,低为冷数据.如果热.冷数据不区分,一并存储,显然不科学.将冷数据也存储在昂贵的内存中,那么你想,成 ...
- 本地文件上传Gitee
0.对于小白来说,我再细讲一下 一.下载git 下载细节参考博客 二.Git配置 点击桌面的图标,进入Git Bash Here 1.配置自己的用户名和邮箱 git config --global u ...
- go GMP
动态栈 操作系统的线程一般都有固定的栈内存(通常为2MB),而 Go 语言中的 goroutine 非常轻量级,一个 goroutine 的初始栈空间很小(一般为2KB),所以在 Go 语言中一次创建 ...
- 2022春每日一题:Day 9
题目:IncDec Sequence 思维题,差分好题,每次区间操作,对应差分a[l]+=v,a[r+1]-=v,在差分数组中一定有一个正负号抵消,那么我们求出差分数组中正数(负数)和,记做s1,s2 ...