【Python】Matplotlib绘制折线图

article/2025/11/7 13:09:44

在这里插入图片描述

文章目录

      • 1.Matplotlib画图简单实现
      • 2.折线图
        • 2.1绘制折线图
        • 2.2设置线的格式
        • 2.3设置折点
        • 2.4.图片的保存和导出
        • 2.5设置刻度
        • 2.6显示中文
        • 2.7实例:每分钟心脏跳动图
      • 3.拓展
        • 3..1一图多线
        • 3.2一图多个坐标子图
        • 3.3坐标轴设置

在这里插入图片描述

1.Matplotlib画图简单实现

Matplotlib在一个绘制2D图片的库

import matplotlib.pyplot as plt
#第一个表示x轴,第二个列表表示y轴
plt.plot([1,0,9],[4,5,6])
plt.show()

image-20220521185732775
在这里插入图片描述

2.折线图

2.1绘制折线图

from matplotlib import pyplot as plt
#设置x
x=range(0,8)
#设置y
y=[14,17,19,11,14,13,15,16]
#plot函数需要两个参数,一个是x一个是y
plt.plot(x,y)
plt.show()

image-20220521191326035

接下来逐步对折线图进行修改

2.2设置线的格式

# plt.plot(x,y)
#对折线进行修饰
#color设置为红色,alpha设置为透明度,linestyle表示线的样式,linewidth表示线的宽度
#color还可以设置为16进制的色值,可在网上查看各种颜色对应的色值
plt.plot(x,y,color='red',alpha=0.5,linestyle='--',linewidth=1)
plt.show()
'''线的样式
-	实线(solid)
--  短线(dashed)
-.	短点相间图
:	虚电线(dotted)
'''

image-20220521192149557

2.3设置折点

#maker是设置折点的样式	markersize是设置结点大小,后面两个参数分别设置折点内部
plt.plot(x,y,marker='o',markersize=10,markerfacecolor='red',markeredgecolor='red',markedgewidth=5)

image-20220521193337167

2.4.图片的保存和导出

import random
#生成的x列表一共有12个数
x=range(2,26,2)
#按照x的长度进行随机生成数字,randint是随机生成整数
y=[random.randint(10, 16) for i in x]
#保存图片
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y,color='yellow',alpha=0.8,marker='o',linestyle='--',linewidth=1)
#plot.show()会释放figure资源,如果在显示图像之后保存图片,则会清空为空图片
#plt.savfig('图片名')
plt.savefig('python初阶\案例图片.png')

image-20220521195826232

2.5设置刻度

import random
from matplotlib import font_manager
#生成的x列表一共有12个数
x=range(2,26,2)
#按照x的长度进行随机生成数字,randint是随机生成整数
y=[random.randint(15,30) for i in x]
#保存图片
plt.figure(figsize=(20,8),dpi=80)
#设置x列表标签
x_ticks_label=["{}:00".format(i) for i in x]
#rotation表示旋转角度
#设置x坐标点
plt.xticks(x,x_ticks_label,rotation=45)
y_ticks_label=['{}摄氏度'.format(i) for i in range(min(y),max(y)+1)]
#设置x坐标轴的名称
plt.xlabel("时间")
#设置y坐标z
plt.ylabel("温度")
#设置y坐标点
plt.yticks(range(min(y),max(y)+1),y_ticks_label)
plt.plot(x,y,color='red',alpha=0.8,marker='o',linestyle='--',linewidth=1)
plt.show()

image-20220521203814690

2.6显示中文

matplotlib只显示应为,无法显示中文,需要修改matplotlib的默认字体

通过matplotlib下的font_manger可以解决

import random
from matplotlib import font_manager
#设置文字路径,在:\windows\Fonts路径下有文字
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\C:\Windows\Fonts\SIMLI.TTF",size=20)
#生成的x列表一共有12个数
x=range(2,26,2)
#按照x的长度进行随机生成数字,randint是随机生成整数
y=[random.randint(15,30) for i in x]
#保存图片
plt.figure(figsize=(20,8),dpi=80)
#设置x列表标签
x_ticks_label=["{}:00".format(i) for i in x]
#rotation表示旋转角度
#设置x坐标
plt.xticks(x,x_ticks_label,rotation=45)
y_ticks_label=['{}摄氏度'.format(i) for i in range(min(y),max(y)+1)]
plt.xlabel("时间",fontproperties=my_font)
plt.ylabel("温度",fontproperties=my_font)
#设置y坐标
plt.yticks(range(min(y),max(y)+1),y_ticks_label,fontproperties=my_font)
plt.plot(x,y,color='red',alpha=0.8,marker='o',linestyle='--',linewidth=1)
plt.title("温度表",fontproperties=my_font,color='red')
plt.show()

image-20220521223958566

2.7实例:每分钟心脏跳动图

from matplotlib import pyplot as plt
import random
from matplotlib import font_manager
#设置字体路径
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\C:\Windows\Fonts\SIMLI.TTF",size=20)
#画图
x=range(0,121)
y=[random.randint(10,30) for i in x]
#设置坐标轴名称
plt.xlabel("时间",fontproperties=my_font,rotation=45)
plt.ylabel("次数",fontproperties=my_font)
#设置坐标轴结点
plt.plot(x,y,color='blue',linewidth=2,alpha=0.7)
plt.title("每分钟心脏跳动数",color='red',fontproperties=my_font)
plt.show()

image-20220521231423510
在这里插入图片描述

3.拓展

3…1一图多线

from matplotlib import pyplot as plt
import random
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname='C:\Windows\Fonts\C:\Windows\Fonts\SIMLI.TTF',size=20)
x=range(11,31)
y1=[1,0,1,1,2,4,3,4,4,5,6,5,4,3,3,1,1,1,1,1]
y2=[1,0,3,1,2,2,2,4,6,4,8,4,3,5,6,4,1,4,2,5]
y3=[2,1,4,0,4,3,7,3,1,0,5,1,3,4,1,4,5,3,2,3]
#设置x轴刻度
x_ticks_label=['{}岁'.format(i) for i in x]
#设置图形
plt.figure(figsize=(20,8),dpi=80)
#画图,zoder是控制画图流程的属性,其值越大则表示画图的时间越晚
plt.plot(x,y1,color='red',label='自己',zorder=5)
plt.plot(x,y2,color='blue',label='同事李',zorder=10)
plt.plot(x,y3,color='green',label='同事张',zorder=15)
#编辑x轴标签
y_ticks_label=['{}个'.format(i) for i in range(0,9)]
plt.yticks(range(0,9),y_ticks_label,fontproperties=my_font)
plt.xticks(x,x_ticks_label,fontproperties=my_font,rotation=45)
#绘制网格->grid
plt.grid(alpha=0.5)
plt.legend(prop=my_font,loc='upper right')
#展示
plt.show()

image-20220522005205441

3.2一图多个坐标子图

import numpy as np
x=np.arange(1,100)
#设置画布
fig=plt.figure(figsize=(20,8),dpi=80)
#使用add_subplot方法向 fig新增子图
#           #解释参数#
# (2,2,1)表示将画布分为2行2列,1表示占用序号为1的画布位置
ax1=fig.add_subplot(2,2,1)
plt.plot(x,x)
ax2=fig.add_subplot(2,2,2)
plt.plot(x,x**2)
ax3=fig.add_subplot(2,2,3)
plt.plot(x,np.log(x))
plt.show()

在这里插入图片描述

对纵坐标进行改进

y=np.arange(-10,10)
z=y**2
plt.plot(y,z)
#对输出的范围进行限制,限制y的取值为-5到5
#限制z的取值为0到80
#可以理解为x.limit;y.limit;
plt.xlim([-5,5])
plt.ylim([0,80])
plt.show()

image-20220522011722436

y=np.arange(-10,10)
z=y**2
plt.plot(y,z)
#设置x的最大值为4
plt.xlim(xmax=4)
#设置x的最小值为4
#plt.xlim(xmin=4)
plt.show()

image-20220522011815803

3.3坐标轴设置

x = [-3,-2,-1,0,1,2,3] 
y = range(0,14,2) 
#取坐标轴的四条边
ax = plt.gca()
plt.plot(x,y) 
plt.show()

image-20220522012505778

x = [-3,-2,-1,0,1,2,3] 
y = range(0,14,2) 
#取坐标轴的四条边
ax = plt.gca()
#分别对四条轴进行设置
#取消右轴
ax.spines['right'].set_color('none')
#取消上轴
ax.spines['top'].set_color('none')
#设置下轴为红色
ax.spines['bottom'].set_color('red')
#设置左轴是绿色
ax.spines['left'].set_color('green')
plt.plot(x,y) 
plt.show()

image-20220522012733323

设置为标准坐标轴

x = [-3,-2,-1,0,1,2,3] 
y = range(0,14,2) 
#取坐标轴的四条边
ax = plt.gca()
#分别对四条轴进行设置
#取消右轴
ax.spines['right'].set_color('none')
#取消上轴
ax.spines['top'].set_color('none')
#设置下轴为红色
ax.spines['bottom'].set_color('red')
#设置左轴是绿色
ax.spines['left'].set_color('green')
#将下轴的‘0’移动到中间
ax.spines['bottom'].set_position(('data',0)) 
#将左轴的‘0’移动到中间
ax.spines['left'].set_position(('data',0))
plt.plot(x,y) 
plt.show()

image-20220522012952218
在这里插入图片描述


http://chatgpt.dhexx.cn/article/Ea3NOsbv.shtml

相关文章

【python】画折线图

文章目录 一个简单的折线图同一张图上显示多条数据给这个折线图中的点加数据标签 一个简单的折线图 画折线图至少需要2个列表:横坐标列表和纵坐标列表,两个坐标的位置一一对应。 from pylab import * mpl.rcParams[font.sans-serif] [SimHei] # 添加…

Python绘制折线图

一、安装matplotlib pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple二、matplotlib图像简介 matplotlib的图像分为三层,容器层、辅助显示层和图像层。 容器层主要由Canvas、Figure、Axes组成。 Canvas位于图像的最底层,充当画…

使用python代码画折线图【matplotlib】

1、画折线图【一条示例】 import matplotlib.pyplot as plt import numpy as npx_axis_data [1,2,3,4,5,6,7] #x y_axis_data [68,69,79,71,80,70,66] #yplt.plot(x_axis_data, y_axis_data, b*--, alpha0.5, linewidth1, labelacc)#bo-表示蓝色实线,数据点实心原…

使用python绘制折线图

前言 最近在完成一篇气象预报的论文,涉及到深度学习与气象绘图。我觉得还是有必要写一下我在这个过程中的一些经验总结,借此机会与各位同道交流。 一、基础命令 在我们使用深度学习时,肯定会用到绘图命令,绘制loss与val_loss等…

Python数据分析--Matplotlib绘图--折线图

Matplotlib中的折线图 折线图简介 折线图,是一种将点按照顺序连接起来的图形,可以看做是将散点图,按照x轴坐标顺序连接起来的图像. 折线图的主要功能是查看因变量y随着自变量x改变的趋势,最适合用于显示随时间而变化的连续数据. 可以看出数量的差异,增长趋势的变化.折线图的代…

Python matplotlib绘制折线图

Python matplotlib绘制折线图 matplotlib是Python中的一个第三方库。主要用于开发2D图表,以渐进式、交互式的方式实现数据可视化,可以更直观的呈现数据,使数据更具说服力。 一、安装matplotlib pip install matplotlib -i https://pypi.tu…

用python画折线图

import matplotlib.pyplot as plt #设置默认字体,选择支持中文的字体以避免出现中文乱码情况 plt.rcParams[font.sans-serif] [Arial Unicode MS] from matplotlib.ticker import MultipleLocator, FormatStrFormatter lst_temp3[10, 16, 17, 14, 12, 10, 12, 6, 6…

一文看懂用python绘制折线图(吐血整理版)极详细!

折线图的绘制主要分为四步: 1)导入相关的库 2)做图片输出的通用设置 3)设置数据及折现的属性 4)设置图的标题 案例: import matplotlib import numpy as np from matplotlib import pyplot as plt %…

代码解读——Retinex低光照图像增强(Deep Retinex Decomposition for Low-Light Enhancement)

今天带来一篇代码解读的文章,是2018年BMVC上的一篇暗光增强文章。个人觉得网络比较轻量并且能够取得还不错的效果。废话不多说,直接贴传送门: 文章地址:http://arxiv.org/abs/1808.04560 源码地址:https://github.co…

Retinex低光照图像增强

Retinex低光照图像增强 常见的图像增强算法基于直方图的增强算法基于Retinex理论的图像增强算法基于同态滤波的图像增强算法基于双边滤波改进的Retinex算法分析 常见的图像增强算法 在夜间等弱光环境下拍摄的图像,由于其亮度、对比度较低且含有噪声、色彩不饱和、细…

对Retinex算法的一些理解

最近在跟老师做视网膜病灶检测的一个项目,其中一个环节是将两张不同时刻的视网膜图像(灰度图像)亮度对齐以方便后续的处理,即保持相同的组织变化后的灰度值基本相同而病变区域仍有较大的差异。在这个对齐图像亮度的环节中我主要应用了Retinex算法,也测试了一些图像,现在把…

OpenCV—Python Retinex图像增强算法

Retinex图像增强算法 一、单尺度SSR(Single Scale Retinex)理论二、Retinex理论的理解三、多尺度MSR(Multi-Scale Retinex)四、MSRCR & MSRCPMSRCR 算法步骤MSRCR其他实现方法MSRCP算法实现:(代码有问题,改进待续) 一、单尺度…

Retinex算法在暗光增强应用以及Python实现

图像暗光增强(一) Retinex简介Single Scale Retinex(SSR)多尺度MSR Multi-Scale Retinex带色彩恢复的多尺度MSR,即MSRCR(Multi-Scale Retinex with Color Restoration)MSRCP Retinex简介 Retinex由两个单词合成的一个…

基于retinex理论改进的低照度图像增强算法

写本文的目的是记录自己学习过或做过的一些东西,深化理解,理清思路,便于回忆。本人处于学习阶段,欢迎指出不对的地方。 本文算法参考文献:李勇.基于Retinex理论的低照度图像增强算法研究与实现[D].西安电子科技大学,2…

图像去雾(二)Retinex图像增强算法

前一段时间研究了一下图像增强算法,发现Retinex理论在彩色图像增强、图像去雾、彩色图像恢复方面拥有很好的效果,下面介绍一下我对该算法的理解。 Retinex理论 Retinex理论始于Land和McCann于20世纪60年代作出的一系列贡献,其基本思想是人感知到某点的颜…

基于 Retinex 的几种图像增强算法总结

Retinex 理论 Retinex 这个词由 Retina 和 Cortex 两个单词组成。在 Retinex 理论中,物体的颜色是由物体对长波、中波和短波光线的反射能力决定的,而不是由反射光强度的绝对值决定的,并且物体的色彩不受光照非均性的影响,具有一致…

Retinex图像增强算法(SSR, MSR, MSRCR)详解及其OpenCV源码

Retinex图像增强算法(SSR, MSR, MSRCR)详解及其OpenCV源码 Retinex是一种常用的建立在科学实验和科学分析基础上的图像增强方法,它是Edwin.H.Land于1963年提出的。就跟Matlab是由Matrix和Laboratory合成的一样,Retinex也是由两个单词合成的一个词语&…

Retinex实例

Retinex实例 1、代码&#xff1a; #include <iostream>#include <cstring>#include "opencv2/opencv.hpp"using namespace cv;static void help(std::string errorMessage){std::cout<<"Program init error : "<<errorMessage&l…

matlab Retinex图像增强算法

Retinex理论在彩色图像增强、图像去雾、彩色图像恢复方面拥有很好的效果&#xff0c;下面介绍一下我对该算法的理解。 Retinex理论 Retinex理论始于Land和McCann于20世纪60年代作出的一系列贡献,其基本思想是人感知到某点的颜色和亮度并不仅仅取决于该点进入人眼的绝对光线&a…

深入探究Retinex

深入探究Retinex 导读Retinex动态范围增强Retinex核心理论 带色彩恢复的Retinex->MSRCR 导读 Retinex理论是建立在人对颜色感知的恒常性上&#xff0c;真实物体都是无色的&#xff0c;所有颜色的产生都是光和物体相互作用&#xff0c;再被人眼感知的过程。在这样的理论基础下…