(学习笔记)matplotlib.pyplot模块下基本画图函数的整理

matplotlib版本:3.7.1

python版本:3.10.12

# 模块引入
import matplotlib.pyplot as plt

1. plt.plot()函数

主要用于画图,绘制点和线。
语法:

plt.plot(x,y,format_string,**kwargs)

参数介绍:

x:x轴的数据,可以是标量、元组、列表
y:y轴的数据,可以是标量、元组、列表
format_string:控制曲线格式的字符串,可选
**kwargs:第二组或更多(x,y,format_string),可画多条曲线。

其中format_string:由颜色字符、风格字符、标记字符组成
颜色字符举例:
‘b’ :蓝色
‘c’: 青绿色
‘g’: 绿色
‘k’ :黑色
‘m’:洋红色
‘r’: 红色
‘w’:白色
‘y’: 黄色

风格字符举例:
‘‐’ 实线
‘‐‐’ 破折线
‘‐.’ 点划线
‘:’ 虚线
‘’ ’ ’ 无线条

标记字符举例:
‘.’ 点标记
‘,’ 像素标记(极小点)
‘o’ 实心圈标记
‘v’ 倒三角标记
‘^’ 上三角标记
‘>’ 右三角标记
‘<’ 左三角标记


**kwargs这是一个字典,里面有很多可选参数:
常用的几个为:
color:指定颜色
lable:线条的标签
linestyle:线条的风格
linewidth:线条的宽度

1.1 plt.plot(x, y)

x = [1, 2, 4, 7]
y = [5, 6, 8, 10]

# 传入两个列表,x为横坐标,y为纵坐标
plt.plot(x, y)
plt.show()

在这里插入图片描述

x = [1, 2, 4, 7]
y = [5, 6, 8, 10]
y1 = [11, 15, 16, 19]

# 传入两个列表,x为横坐标,y为纵坐标
plt.plot(x, y) # 用红色的原点,标出点,用实线连接
plt.plot(y1)   #x可以省略,默认从[0, 1, 2, 3, N-1 ]递增
plt.show()

在这里插入图片描述
还可以传入两组或多组参数

x = [1, 2, 4, 7]
y = [5, 6, 8, 10]
x1 = [10, 12, 14, 18]
y1 = [11, 15, 16, 19]

# 传入两个列表,x为横坐标,y为纵坐标
plt.plot(x, y, 'ro-', x1, y1, 'bo--')
plt.show()

在这里插入图片描述

还可以传2维数组

lst1 = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
lst2 = [[2, 3, 2], [3, 4, 3], [4, 5, 4]]
# 传入两个列表,x为横坐标,y为纵坐标
plt.plot(lst1, lst2,  'bo--')
plt.show()

观察发现,二维数组的第一列为一组坐标,第二列为一组坐标,第三列为一组坐标。
在这里插入图片描述

1.2 plt.plot(x, y, **kwargs)

控制线和点的参数。

x = [1, 2, 4, 7]
y = [5, 6, 8, 10]
# 蓝色,线宽20,圆点,点尺寸50,点填充红色,点边缘宽度6,点边缘灰色
plt.plot(x, y, color="blue", linewidth=10, marker="o", markersize=50,
         markerfacecolor="red", markeredgewidth=6, markeredgecolor="grey")
plt.show()

在这里插入图片描述

2. plt.xlable(), plt.ylable()

给x/y轴取名字。

语法:plt.xlable(string)
string:字符串格式名字

x = [1, 2, 4, 7]
y = [5, 6, 8, 10]

plt.xlabel('size')
plt.ylabel('price')
plt.plot(x, y, 'ro')
plt.show()

在这里插入图片描述

3. plt.title()

给表格取一个标题。

语法:plt.title(string)
shring:字符串格式的标题。

x = [1, 2, 4, 7]
y = [5, 6, 8, 10]

plt.xlabel('size')
plt.ylabel('price')
plt.title('house price')
plt.plot(x, y, 'ro')
plt.show()

在这里插入图片描述

4. plt.show()

显示坐标图。
上面几个函数已经包含此例子。

5.plt.subplot()

把一个figure分成n行m列,选择第k个位置作图(从1开始数)。

语法:plt.subplot(nrows,mcols, k)

# 把画布分成两行两列,画在第四个位置
x = [1, 2, 4, 7]
y = [5, 6, 8, 10]
plt.subplot(224)
plt.xlabel('size')
plt.ylabel('price')
plt.title('house price')
plt.plot(x, y, 'ro')
plt.show()

在这里插入图片描述

plt.subplots()

语法:fig, ax = plt.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)

参数设置:

  • nrows/ncols:设置图标的行数、列数,默认为1
  • sharex/sharey:设置x、y是否为共享属性,默认为false,可设置为’none’、‘all’、‘row’或‘col’。False或none每个子图的x轴或y轴都是独立的,True或‘all’:所有子图共享x轴或y轴,‘col’:设置每个子图列共享一个x轴或y轴。
  • squeeze:布尔值,默认为 True,表示额外的维度从返回的 Axes(轴)对象中挤出,对于 N*1 或 1*N 个子图,返回一个 1 维数组,对于 N*M,N>1 和 M>1 返回一个 2 维数组。如果设置为 False,则不进行挤压操作,返回一个元素为 Axes 实例的2维数组,即使它最终是1x1。
  • subplot_kw:可选,字典类型。把字典的关键字传递给 add_subplot() 来创建每个子图。
  • gridspec_kw:可选,字典类型。把字典的关键字传递给 GridSpec 构造函数创建子图放在网格里(grid)
  • **fig_kw:把详细的关键字参数传给 figure() 函数
import matplotlib.pyplot as plt
import numpy as np

# 创建一些测试数据 -- 图1
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)

# 创建一个画像和子图 -- 图2
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')

# 创建两个子图 -- 图3
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)

# 创建四个子图 -- 图4
fig, axs = plt.subplots(2, 2, subplot_kw=dict(projection="polar"))
axs[0, 0].plot(x, y)
axs[1, 1].scatter(x, y)

# 共享 x 轴
plt.subplots(2, 2, sharex='col')

# 共享 y 轴
plt.subplots(2, 2, sharey='row')

# 共享 x 轴和 y 轴
plt.subplots(2, 2, sharex='all', sharey='all')

# 这个也是共享 x 轴和 y 轴
plt.subplots(2, 2, sharex=True, sharey=True)

# 创建标识为 10 的图,已经存在的则删除
fig, ax = plt.subplots(num=10, clear=True)

plt.show()

在这里插入图片描述

在这里插入图片描述


使用该函数画一个散点图的示例:

X_train = np.array([[1.0], [2.0]], dtype=np.float32)           #(size in 1000 square feet)
Y_train = np.array([[300.0], [500.0]], dtype=np.float32)       #(price in 1000s of dollars)

fig, ax = plt.subplots(1,1)
ax.scatter(X_train, Y_train, marker='x', c='r', label="Data Points")
ax.legend( fontsize='xx-large')
ax.set_ylabel('Price (in 1000s of dollars)', fontsize='xx-large')
ax.set_xlabel('Size (1000 sqft)', fontsize='xx-large')
plt.show()

在这里插入图片描述

6. plt.figure()

创建一个matplotlib函数的对象,它是顶层容器。我们可以利用这个对象来进行画图操作。

语法:fig = plt.figure()
创建一个fig的对象

fig.add_subplot()

增加一个子图

语法:ax = fig.add_subplot(111)
增加一个一行一列的子图

参考网站:
菜鸟教程

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值