一:概述
python种绘图的第三方库有很多,在这里我将介绍matplotlib这个库的快速入门指南,这个指南我是基于官网入门进行说明的。欢迎点赞、评论和关注哦!!!!
二:具体说明
<1>简单的坐标抽绘制
在这里我简单的绘制了一个简单的折线图和两个正比例函数以及二次函数
# 简单坐标抽绘制
# 导入matplotlib模块
import matplotlib as mpl
# 导入matplotlib模块中的pyplot
import matplotlib.pyplot as plt
# 导入numpy模块
import numpy as np
fig,ax = plt.subplots() # 创建一个单一的坐标抽图形
ax.plot([1, 2, 3, 4, 5, 6],[1, 9, 8, 7, 6, 5])
ax.plot([1, 2, 3, 4, 5, 6],[1, 2, 3, 4, 5, 6])
ax.plot([1, 2, 3, 4],[3 ,6, 9, 12])
# 二次函数 y = 2x^2
ax.plot([1, 2, 3, 4, 5, 6], [2, 8, 18, 32, 50, 72])
plt.show()
ax.plot([1, 2, 3, 4],[3 ,6, 9, 12])
这个代码中的前半部分列表相当于坐标轴的x值,后半部分相当于坐标轴的y值。
<2>网格轴线图形绘制
导入matplotlib模块
import matplotlib as mpl
# 导入matplotlib模块中的pyplot
import matplotlib.pyplot as plt
# 导入numpy模块
import numpy as np
fig = plt.figure() # 一个没有轴的空洞数字
fig, ax = plt.subplots() # 单轴线
fig, axs = plt.subplots(3, 4) # 一个有 3x4 网格轴线的图形
fig, axs = plt.subplots(2, 2) # 一个有 3x4 网格轴线的图形
fig, axs = plt.subplots(3, 3) # 一个有 3x3 网格轴线的图形
plt.show()
fig, axs = plt.subplots(2, 2)
括号里面的前一个参数表示是行数,第二个参数表示的是列数
<3>绘图函数的输入类型
# 绘图函数的输入类型
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
a = np.matrix([[2, 3, 4, 5],[6, 7, 8, 9]])
a_asarray = np.asarray(a)
np.random.seed(1314521) # 为随机生成树播种
data = {
'e': np.arange(100),
'f': np.random.randint(0, 100, 100),
'g': np.random.randn(100)
}
data['a'] = data['e'] + 10 * np.random.randn(100)
data['g'] = np.abs(data['g']) * 100
fig, ax = plt.subplots(figsize=(5, 2.7), layout='constrained')
ax.scatter('e', 'a', f='f', g='g', data=data)
ax.set_xlabel('e值')
ax.set_ylabel('a值')
plt.show()
在运行上面的代码,结果报错了:
Traceback(最近调用最后):
文件 “E:\Code\Python\blog\blog1\matplotlib\simple1\Input_Types_for_Plotting _Functions.py”,第 16 行,<模块>
fig, ax = plt.subplots(figsize=(5, 2.7), layout='constrained')
包装器中的文件 “\.conda\envs\keras\lib\site-packages\matplotlib\_api\deprecation.py”,第 471 行
返回 func(*args, **kwargs)
文件 “C:\.conda\envs\keras\lib\site-packages\matplotlib\pyplot.py”,第 1439 行,在子图中
图 = 图(**fig_kw)
文件“.conda\envs\keras\lib\site-packages\matplotlib\pyplot.py”,第 797 行,如图所示
经理 = new_figure_manager(
文件 “C.conda\envs\keras\lib\site-packages\matplotlib\pyplot.py”,第 316 行,new_figure_manager
返回 _backend_mod.new_figure_manager(*args, **kwargs)
文件“D:\JetBrains\PyCharm\PyCharm 2024.1.4\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py”,第 63 行,new_figure_manager
figure = FigureClass(*args, **kwargs)
TypeError:__init__() 获取了意外的关键字参数 'layout'
出现这个错误的原因和解决办法如下:
问题出在 函数的 参数上。该参数在某些版本的 Matplotlib 中并不被支持。你可以尝试删除 参数,然后再次运行代码。这里是修改后的代码:plt.subplots()
layout
layout='constrained'
修改后的代码如下所示:
# 绘图函数的输入类型
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
a = np.matrix([[2, 3, 4, 5], [6, 7, 8, 9]])
a_asarray = np.asarray(a)
np.random.seed(1314521) # 为随机生成树播种
data = {
'e': np.arange(100),
'f': np.random.randint(0, 100, 100),
'g': np.random.randn(100)
}
data['a'] = data['e'] + 10 * np.random.randn(100)
data['g'] = np.abs(data['g']) * 100
# 删除 layout='constrained'
fig, ax = plt.subplots(figsize=(5, 2.7))
ax.scatter('e', 'a', c='f', s='g', data=data)
ax.set_xlabel('e值')
ax.set_ylabel('a值')
plt.show()
运行之后还会出现一个错误如下:
.conda\envs\keras\lib\site-packages\matplotlib\backends\backend_agg.py:240: RuntimeWarning: Glyph 20540 missing from current font.
font.set_text(s, 0.0, flags=flags)
\.conda\envs\keras\lib\site-packages\matplotlib\backends\backend_agg.py:203: RuntimeWarning: Glyph 20540 missing from current font.
font.set_text(s, 0, flags=flags)
这个警告信息是关于字体缺失的问题。它通常出现在使用Matplotlib绘制图形时,指示某个字符在当前字体中找不到。这可能会导致在图形中显示错误的字符或方块。
解决办法如下:(通过Windos系统自带的字体来设置这个里面的字体)
修改后的代码如下所示:
# 绘图函数的输入类型
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 设置中文字体为宋体
font_path = 'C:/Windows/Fonts/simhei.ttf' # Windows 系统的宋体字体路径
font_prop = FontProperties(fname=font_path)
a = np.matrix([[2, 3, 4, 5], [6, 7, 8, 9]])
a_asarray = np.asarray(a)
np.random.seed(1314521) # 为随机生成树播种
data = {
'e': np.arange(100),
'f': np.random.randint(0, 100, 100),
'g': np.random.randn(100)
}
data['a'] = data['e'] + 10 * np.random.randn(100)
data['g'] = np.abs(data['g']) * 100
# 删除 layout='constrained'
fig, ax = plt.subplots(figsize=(5, 2.7))
ax.scatter('e', 'a', c='f', s='g', data=data)
ax.set_xlabel('e值',fontproperties=font_prop)
ax.set_ylabel('a值',fontproperties=font_prop)
plt.legend(prop=font_prop)
plt.show()
注意:这个ax.set_ylabel('a值',fontproperties=font_prop)
后面需要加英文分号,否则运行后图上空白
运行成功后的图形如下:
这一节的入门就到这里了,希望多多点赞评论和关注哦!!!!