当前位置:网站首页>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}$')
边栏推荐
- H5 的浏览器存储
- Programmers should understand I/O this way
- Transformer详细解读与预测实例记录
- Seven Ways to Center a Box Horizontally and Vertically
- time complexity and space complexity
- 前置++和后置++的区别
- Passing parameters in multiple threads
- D39_Vector
- What should I do if the SSL certificate prompts that it is expired or invalid?
- js判断文字是否超过区域
猜你喜欢

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

前置++和后置++的区别

LeetCode刷题记录(2)

Introduction to Network Layer Protocols

el-autocomplete使用

LeetCode practice and self-comprehension record (1)

Into the pre-service, thought they play so flowers

markdown editor template

NAT experiment

The hook of the operation of the selenium module
随机推荐
初识网页与浏览器
From "dual card dual standby" to "dual communication", vivo took the lead in promoting the implementation of the DSDA architecture
浏览器兼容汇总
原生JS带你了解数组方法实现及使用
[ingress]-ingress exposes services using tcp port
Growth: IT Operations Trends Report
单片机原理与应用复习
Will intelligent operation and maintenance replace manual operation and maintenance?
product learning materials
程序员应该这样理解I/O
What is the website ICP record?
Configuration of routers and static routes
BIO,NIO,AIO实践学习笔记(便于理解理论)
系统基础-学习笔记(一些命令记录)
Billions of IT operations in the market, the product by strength to speak
Take you in-depth understanding of cookies
sql server duplicate values are counted after
VRRP概述及实验
One-arm routing experiment and three-layer switch experiment
[ingress]-ingress使用tcp端口暴露服务