当前位置:网站首页>Matplotlib绘图笔记
Matplotlib绘图笔记
2022-08-05 05:25:00 【ProfSnail】
使用Python中的Matplotlib绘图,有一些功能记不全,每次都要上网查找相应的代码使用方法,以本文为笔记,记录常用、不常用的使用方法。
官方文档地址:https://matplotlib.org/stable/api/axes_api.html
坐标轴
坐标轴标签密度
MultipleLocator(1)表示间距为1。
from matplotlib import ticker
ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
不显示坐标轴
一次将所有的边框都不显示。
plt.axis('off')
横纵坐标轴的比例
让横轴和纵轴的比例是1:1。
plt.gca().set_aspect(1)
坐标轴范围
常规单图情况使用plt.xlim(xmin,xmax)。多图情况使用ax.set(xlim=(xmin, xmax),ylim=(ymin,ymax))。
# 一般单图使用
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
# 多图情况
ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax))
# 或单图使用
plt.gca().set(xlim=(xmin, xmax), ylim=(ymin, ymax))
子图
生成子图
生成子图,plt.subplots(行数,列数),返回的axes是一个二维索引结构。
fig, axes = plt.subplots(nrows=2, ncols=2)
对二维索引结构扁平化为一维的索引结构,axes.flatten()
for i, ax in enumerate(axes.flatten()):
ax.plot(x, y)
子图标题
使用ax.set_title(‘title’)标记子图的名称,此时,标题一般在图的顶部。
ax.set_title('title')
为了使标题在图的底部,使用y=-0.2,定义标题的位置。
ax.set_title('title', y=-0.2)
fontsize=15,调整字体大小。
ax.set_title('title', y=-0.2, fontsize=15)
子图间距
通过plt.subplots_adjust进行调整,参数里面的top, left, right, right对应的是以图像左下角为原点,上下左右边角的位置比例,取值为0-1。wspace和hspace控制子图间横纵间距。
plt.subplots_adjust(left, bottom, right, top, wspace, hspace)
设置子图坐标轴的显示范围
如果使用plt.xlim(xmin, xmax)的方式,在迭代过程中,这个范围的限制仅仅只作用于最后一张子图上,正确的方法是对每个子图使用ax.set(xlim=(xmin,xmax))的方法。
参见:https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.axis.html
fig, axes = plt.subplots(2, 3)
for i, ax in enumerate(axes.flatten()):
ax.set(xlim=(xmin, xmax), ylim=(ymin,ymax))
绘制
绘制箭头
由xytext的坐标指向xy的坐标。
plt.gca().annotate("", xy=(0, 2.5), xytext=(0, -1.2),
arrowprops=dict(arrowstyle="->"))
图中写文字
使用$$之后,与LaTeX的使用方法一样。
plt.text(x=1, y=2, s=r'$\hat{\beta}$')
边栏推荐
- D39_Eulerian Angles and Quaternions
- DevOps - Understanding Learning
- [ingress]-ingress使用tcp端口暴露服务
- markdown editor template
- Operation and maintenance engineer, come and pick up the wool
- 单片机期末复习大题
- 错误记录集锦(遇到则记下)
- User and user group management, file permission management
- Successful indie developers deal with failure & imposters
- Into the pre-service, thought they play so flowers
猜你喜欢

LeetCode practice and self-comprehension record (1)

Passing parameters in multiple threads

VRRP overview and experiment

Teach you simple steps to achieve industrial raspberries pie properly installed RS232 USB drive

selenium学习

What are some things that you only know when you do operation and maintenance?

错误类型:反射。ReflectionException:无法设置属性“xxx”的“类”xxx”与价值“xxx”

Dry!Teach you to use industrial raspberries pie combining CODESYS configuration EtherCAT master station

Cocos Creator Mini Game Case "Stick Soldier"

Operation and maintenance engineer, come and pick up the wool
随机推荐
VLAN介绍与实验
D39_Eulerian Angles and Quaternions
LeetCode练习及自己理解记录(1)
用户和用户组管理、文件权限管理
Transformer详细解读与预测实例记录
The size of the screen adaptation
input detailed file upload
Seven Ways to Center a Box Horizontally and Vertically
King power volume LinkSLA, realize operations engineer is happy fishing
Met with the browser page
云计算基础-学习笔记
Tencent Cloud Message Queue CMQ
network issue?Service packet loss?This is enough
单片机期末复习大题
路由器和静态路由的配置
What impact does CIPU have on the cloud computing industry?
el-autocomplete use
Four ways to obtain Class objects through reflection
Network Protocol Fundamentals - Study Notes
The 25 best free games on mobile in 2020