python - matplotlib总结 - 基础篇

官方文档:
https://matplotlib.org/stable/index.html
参考资料:
https://www.jianshu.com/p/da385a35f68d
https://blog.csdn.net/q936330007/article/details/91674108

Matplotlib简介:

matplotlib是受MATLAB的启发构建的。MATLAB是数据绘图领域广泛使用的语言和工具。MATLAB语言是面向过程的。利用函数的调用,MATLAB中可以轻松的利用一行命令来绘制直线,然后再用一系列的函数调整结果。

matplotlib有一套完全仿照MATLAB的函数形式的绘图接口,在matplotlib.pyplot模块中。这套函数接口方便MATLAB用户过度到matplotlib包

Matplotlib有两种画图接口:一个是类似MATLAB风格接口,另一个是功能更强大的面向对象接口。(这里我们主要学习和总结matplotlib.pyplot模块)

基础操作:

1.引入matplotlib.pyplot模块:

import matplotlib.pyplot as plt
import numpy as np

2.定义Figure与Axes:

fig, ax = plt.subplots()  # Create a figure containing a single axes.
ax.plot([1, 2, 3, 4], [1, 4, 2, 3])  # Plot some data on the axes.

当然更加简便的写法如下:

plt.plot([1, 2, 3, 4], [1, 4, 2, 3])  # Matplotlib plot.

但是作为matplotlib的学习者,你需要了解matplotlib的基本建模思路:

Matplotlib graphs your data on Figures (i.e., windows, Jupyter widgets, etc.), each of which can contain one or more Axes (i.e., an area where points can be specified in terms of x-y coordinates, or theta-r in a polar plot, or x-y-z in a 3D plot, etc.). The simplest way of creating a figure with an axes is using pyplot.subplots. We can then use Axes.plot to draw some data on the axes.
(引用来源于官方文档)

也就是说对于matplotlib来说我们是在Axes上面作图,而Axes是Figures的子对象,并且每个Figures下都包含好一个或者好几个Axes。

而对于每一个Axes来说,它都包含以下若干属性:
图片来源于官方文档
Object-interface:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2, 100)

# Note that even in the OO-style, we use `.pyplot.figure` to create the figure.
fig, ax = plt.subplots()  # Create a figure and an axes.
ax.plot(x, x, label='linear')  # Plot some data on the axes.
ax.plot(x, x**2, label='quadratic')  # Plot more data on the axes...
ax.plot(x, x**3, label='cubic')  # ... and some more.
ax.set_xlabel('x label')  # Add an x-label to the axes.
ax.set_ylabel('y label')  # Add a y-label to the axes.
ax.set_title("Simple Plot")  # Add a title to the axes.
ax.legend()  # Add a legend.

Matlab-interface:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2, 100)

plt.plot(x, x, label='linear')  # Plot some data on the (implicit) axes.
plt.plot(x, x**2, label='quadratic')  # etc.
plt.plot(x, x**3, label='cubic')
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend()

效果图:

3.设置关于Axes相关的属性:

属性相关描述
axex设置坐标轴边界和表面的颜色、坐标刻度值大小和网格的显示
figure控制dpi、边界颜色、图形大小、和子区( subplot)设置
font字体集(font family)、字体大小和样式设置
grid设置网格颜色和线性
legend设置图例和其中的文本的显示
line设置线条(颜色、线型、宽度等)和标记
patch是填充2D空间的图形对象,如多边形和圆,控制线宽、颜色和抗锯齿设置等
savefig可以对保存的图形进行单独设置,例如,设置渲染的文件的背景为白色
verbose设置matplotlib在执行期间信息输出,如silent、helpful、debug和debug-annoying
xticks/yticks为x,y轴的主刻度和次刻度设置颜色、大小、方向,以及标签大小

修改相关的属性:
ax.set_properties()函数:

fig, ax = plt.subplots()  # Create a figure and an axes.
ax.set_xlabel('x label')  # Add an x-label to the axes.
ax.set_ylabel('y label')  # Add a y-label to the axes.
ax.set_title("Simple Plot")  # Add a title to the axes.

如何创建多个Fig的子对象Axes:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
# 准备开始画图
fig, ax = plt.subplots(2)

# 在每个子图上分别绘制
ax[0].plot(x, np.sin(x))
ax[1].plot(x, np.cos(x))
这里我们注意到ax是一个list类型的变量,而这会导致Axes可以被索引进行不同图像的访问。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值