Matplotlib

matplotlib: 最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建

绘制折线图

绘制两小时的温度变化

  1. from matplotlib import pyplot as plt
  2. x = range(2, 26, 2)
  3. y = [15, 13, 14, 5, 17, 20, 25, 26, 24, 22, 19, 15]
  4. # 全局变量fig figure方法(figsize画板尺寸=(长float,宽float), dpi=整数int)
  5. fig = plt.figure(figsize=(20, 8), dpi=80)
  6. # 传入X坐标和Y坐标,并绘图
  7. plt.plot(x, y)
  8. # 调整X轴刻度,传入的列表密集与否,决定X轴的密集程度
  9. plt.xticks(x)
  10. # 调整X轴刻度同理
  11. plt.yticks(range(min(y), max(y)+1))
  12. # savefig方法,传入一个路径
  13. plt.savefig("./t1.png")
  14. # 展示
  15. plt.show()

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

模拟11岁到30岁自己和同桌交女朋友的走势

  1. # coding=utf-8
  2. from matplotlib import pyplot as plt
  3. from matplotlib import font_manager
  4. my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\simhei.ttf")
  5. y_1 = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
  6. y_2 = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  7. x = range(11, 31)
  8. # 设置图形大小
  9. plt.figure(figsize=(20, 8), dpi=80)
  10. plt.plot(x, y_1, label="自己", color="red")
  11. plt.plot(x, y_2, label="同桌", color="#DB7093", linestyle="--")
  12. # 设置x轴刻度
  13. _xtick_labels = ["{}岁".format(i) for i in x]
  14. plt.xticks(x, _xtick_labels, fontproperties=my_font)
  15. # plt.yticks(range(0,9))
  16. # 绘制网格
  17. plt.grid(alpha=0.4, linestyle=':')
  18. # 添加图例
  19. plt.legend(prop=my_font, loc="upper left")
  20. # 展示
  21. plt.show()

绘制折线图总结

Matplotlib 绘制折线图的更多相关文章

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

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

  2. Python开发【模块】:matplotlib 绘制折线图

    matplotlib 1.安装matplotlib ① linux系统安装 # 安装matplotlib模块 $ sudo apt-get install python3-matplotlib # 如 ...

  3. 【Python】matplotlib绘制折线图

    一.绘制简单的折线图 import matplotlib.pyplot as plt squares=[1,4,9,16,25] plt.plot(squares) plt.show() 我们首先导入 ...

  4. 1 matplotlib绘制折线图

    from matplotlib import pyplot as plt #设置图形大小 plt.figure(figsize=(20,8),dpi=80) plt.plot(x,y,color=&q ...

  5. matplotlib之折线图

    1.案例一 # coding=utf-8 from matplotlib import pyplot as plt import random # 设置字体相关 from matplotlib imp ...

  6. Python绘制折线图

    一.Python绘制折线图 1.1.Python绘制折线图对应代码如下图所示 import matplotlib.pyplot as pltimport numpy as np from pylab ...

  7. 用canvas绘制折线图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. php中用GD绘制折线图

    php中用GD绘制折线图,代码如下: Class Chart{ private $image; // 定义图像 private $title; // 定义标题 private $ydata; // 定 ...

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

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

  10. UUChart的使用--iOS绘制折线图

    UUChart是一个用于绘制图表的第三方,尤其适合去绘制折线图. 二.下载地址: https://github.com/ZhipingYang/UUChartView 三.使用 第一步.首先我们将下载 ...

随机推荐

  1. JavaScript基础&实战(5)js中的数组、forEach遍历、Date对象、Math、String对象

    文章目录 1.工厂方法创建对象 1.1 代码块 1.2.测试结果 2.原型对象 2.1 代码 2.2 测试结果 3.toString 3.1 代码 3.2 测试结果 4.数组 4.1 代码 5.字面量 ...

  2. 记录一次sshd服务启动失败

    记录一次sshd服务启动失败 问题描述: 服务器开机之后发现无法通过远程连接服务器终端,但是服务器并未宕机,于是考虑到sshd服务出现异常 解决思路: 查看服务器sshd服务运行情况 [root@ha ...

  3. 解决ffmpeg的播放摄像头的延时优化问题(项目案例使用有效)

    在目前的项目中使用了flv的播放摄像头的方案,但是延时达到了7-8秒,所以客户颇有微词,没有办法,只能开始优化播放延时的问题,至于对接摄像头的方案有好几种,这种咱们以后在聊,今天只要聊聊聊优化参数的问 ...

  4. 转载:Python 实现百度翻译

    来源: https://blog.csdn.net/qq_44814439/article/details/105642066 作者: Chloemxc 功能: Python 实现百度翻译 from ...

  5. 谣言检测(RDCL)——《Towards Robust False Information Detection on Social Networks with Contrastive Learning》

    论文信息 论文标题:Towards Robust False Information Detection on Social Networks with Contrastive Learning论文作 ...

  6. 这篇关于Oracle内存管理方式的介绍太棒了!我必须要转发,很全面。哈哈~

    "Oracle内存管理可分为两大类,自动内存管理和手动内存管理.其中手动内存管理又可分为自动共享内存管理,手动共享内存管理,自动PGA内存管理以及手动PGA内存管理.本文会简单的介绍不同的内 ...

  7. MySQL数据库的性能分析 ---图书《软件性能测试分析与调优实践之路》-手稿节选

    1  .MySQL数据库的性能监控 1.1.如何查看MySQL数据库的连接数 连接数是指用户已经创建多少个连接,也就是MySQL中通过执行 SHOW  PROCESSLIST命令输出结果中运行着的线程 ...

  8. Perl printf 函数

    转载 Perl printf 函数

  9. 2022春每日一题:Day 10

    题目:CF1110E Magic Stones 每次操作 c[i]变成c[i-1]+c[i+1]-c[i],那么显然,c[1]和c[n]是不会改变的,因此只要c[1]和t[1],c[n]和t[n]不相 ...

  10. 12、求Sn = a + aa + aaa + aaaa + ....其中a为一个数字,一共有n项。a和n由用户键盘输入。

    /* 求Sn = a + aa + aaa + aaaa + ....其中a为一个数字,一共有n项.a和n由用户键盘输入. */ #include <stdio.h> #include & ...