首页 技术 正文
技术 2022年11月17日
0 收藏 656 点赞 4,526 浏览 3072 个字

目录

前言

陆陆续续接触了些,关于Matplotlib的教材,总是感觉学不到本质的东西。今天就来讲一下 关于

plt.plot()函数的本质。

(一)plt.plot()函数的本质

1.说明

plt.plot()函数的本质就是根据点连接线。根据x(数组或者列表) 和 y(数组或者列表)组成点,然后连接成线。

2.源代码

import matplotlib.pyplot as pltx = [1, 2, 3, 4]
y = [1, 2, 20, 50]
# 创建一个画布
plt.figure()
# 创建一条线
plt.plot(x, y)# 展现画布
plt.show()

3.展示效果

(二)plt.plot()函数缺省x时

1.说明

缺省x的情况下,x的默认值是:range(len(y))

2.源代码

import matplotlib.pyplot as plt# 缺省x参数时,默认的x是range(len(y))
y = [1, 2, 3, 4]
# 创建一个画布
plt.figure()
# 创建一条线
plt.plot(y)
# 展现画布
plt.show()

3.展示效果

(三)颜色控制符

要想使用丰富,炫酷的图标,我们可以使用更复杂的格式设置,主要颜色,线的样式,点的样式。

默认的情况下,只有一条线,是蓝色实线。多条线的情况下,生成不同颜色的实线。

字符 颜色
‘b’ blue
‘g’ green
‘r’ red
‘c’ cyan 青色
‘m’ magenta平红
‘y’ yellow
‘k’ black
‘w’ white

(四)线形控制符

1.说明

字符 类型
‘-‘ 实线
‘–‘ 虚线
‘-.’ 虚点线
‘:’ 点线
‘ ‘ 空类型,不显示线

2.源代码

import matplotlib.pyplot as pltx = [1, 2, 3, 4]
y1 = [1, 2, 3, 4]
y2 = [1, 4, 9, 16]
y3 = [1, 8, 27, 64]
y4 = [1, 16, 81, 124]
# 创建一个画布
plt.figure()
# 在figure下线
plt.plot(x, y1, "-o") #实线
plt.plot(x, y2, "--o") #虚线
plt.plot(x, y3, "-.o") #虚点线
plt.plot(x, y4, ":o") # 点线
# 展现画布
plt.show()

3.输出效果

(五)点的类型控制符

1.普通点类型

(1)说明:

‘.’
‘,’ 像素点
‘o’ 原点

(2)源代码

import matplotlib.pyplot as pltx = [1, 2, 3, 4]
y1 = [1, 2, 3, 4]
y2 = [1, 4, 9, 16]
y3 = [1, 8, 27, 64]
y4 = [1, 16, 81, 124]
# 创建一个画布
plt.figure()
# 在figure下的线
plt.plot(x, y1, "-.") # 点
plt.plot(x, y2, "-,") # 像素点
plt.plot(x, y3, "-o") # 圆点# 展现画布
plt.show()

(3)输出效果:

2.三角点

(1)说明:

‘^’ 上三角点
‘v’ 下三角点
‘<‘ 左三角点
‘>’ 右三角点

(2)源代码:

import matplotlib.pyplot as pltx = [1, 2, 3, 4]
y1 = [1, 2, 3, 4]
y2 = [1, 4, 9, 16]
y3 = [1, 8, 27, 64]
y4 = [1, 16, 81, 124]
# 创建一个画布
plt.figure()
# 在figure下的线
plt.plot(x, y1, "-^")
plt.plot(x, y2, "-v")
plt.plot(x, y3, "-<")
plt.plot(x, y4, "->")# 展现画布
plt.show()

(3)输出效果:

3.三叉点

(1)说明:

‘1’ 下三叉点
‘2’ 上三叉点
‘3’ 左三叉点
‘4’ 右三叉点

(2)源代码:

import matplotlib.pyplot as pltx = [1, 2, 3, 4]
y1 = [1, 2, 3, 4]
y2 = [1, 4, 9, 16]
y3 = [1, 8, 27, 64]
y4 = [1, 16, 81, 124]
# 创建一个画布
plt.figure()
# 在figure下的线
plt.plot(x, y1, "-1")
plt.plot(x, y2, "-2")
plt.plot(x, y3, "-3")
plt.plot(x, y4, "-4")# 展现画布
plt.show()

(3)输出效果:

4.多边形点

(1)说明:

‘s’ 正方点
‘p’ 五角点
‘*’ 星形点
‘h’ 六边形1
‘H’ 六边形2

(2)源代码:

import matplotlib.pyplot as pltx = [1, 2, 3, 4]
y1 = [1, 2, 3, 4]
y2 = [1, 4, 9, 16]
y3 = [1, 8, 27, 64]
y4 = [1, 16, 81, 124]
y5 = [1, 64, 100, 180]
# 创建一个画布
plt.figure()
# 在figure下的线
plt.plot(x, y1, "-s")
plt.plot(x, y2, "-p")
plt.plot(x, y3, "-*")
plt.plot(x, y4, "-h")
plt.plot(x, y5, "-H")# 展现画布
plt.show()

(3)输出效果:

5.其他形状点

(1)说明:

‘+’ 加号点
‘x’ 乘号点
‘D’ 实心菱形点
‘d’ 细菱形点
‘_’ 横线点
‘|’ 竖线点

(2)源代码:

import matplotlib.pyplot as pltx = [1, 2, 3, 4]
y1 = [1, 2, 3, 4]
y2 = [1, 4, 9, 16]
y3 = [1, 8, 27, 64]
y4 = [1, 16, 81, 124]
y5 = [1, 64, 100, 180]
# 创建一个画布
plt.figure()
# 在figure下的线
plt.plot(x, y1, "-+")
plt.plot(x, y2, "-x")
plt.plot(x, y3, "-D")
plt.plot(x, y4, "-d")
plt.plot(x, y5, "-_")# 展现画布
plt.show()

(3)输出效果:

注:三种控制符可以单独使用,也可以组合使用

(六)风格使用的另一种方法

1.说明

color="green" 指定颜色为绿色

linestyle="dashed" 指定线形为dashed类型

marker="o" 指定标记类型为o点

markerfacecolor="blue"指定标记的颜色为蓝色

markersize=20 指定标记的大小为20

2.原代码

import matplotlib.pyplot as plt
import numpy as npx = np.arange(10)
y1 = x * 1.5
y2 = x * 2.5
y3 = x * 3.5
y4 = x * 4.5
y5 = x * 5.5plt.plot(x, y1, "-P")
plt.plot(x, y2, "-|")
plt.plot(x, y3, color="#000000")
plt.plot(x, y4, "-o", markersize=20)
plt.plot(x, y5, "-^", markerfacecolor="blue")plt.show()

3.输出效果

作者:Mark

日期:2019/01/30 周三

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918