当前位置:网站首页>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}$')
边栏推荐
- Network Troubleshooting Basics - Study Notes
- What's the point of monitoring the involution of the system?
- Media query, rem mobile terminal adaptation
- Take you in-depth understanding of cookies
- System basics - study notes (some command records)
- By solving these three problems, the operation and maintenance efficiency will exceed 90% of the hospital
- VRRP overview and experiment
- Successful indie developers deal with failure & imposters
- 教您简单几步实现工业树莓派正确安装RS232转USB驱动
- Advantages of overseas servers
猜你喜欢

Vim tutorial: vimtutor

Cloud Computing Basics - Study Notes

LeetCode中常用语言的一些基本方法记录

Autoware--Beike Tianhui rfans lidar uses the camera & lidar joint calibration file to verify the fusion effect of point cloud images

input detailed file upload

从“双卡双待“到”双通“,vivo率先推动DSDA架构落地

摆脱极域软件的限制

人人AI(吴恩达系列)

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

滚动条问题,未解决
随机推荐
User and user group management, file permission management
Seven Ways to Center a Box Horizontally and Vertically
初识网页与浏览器
What?CDN cache acceleration only works for accelerating static content?
5分钟完成mysql离线安装
flink cdc 目前支持Gauss数据库源吗
vs2017关于函数命名方面的注意事项
What should I do if the SSL certificate prompts that it is expired or invalid?
reduce()方法的学习和整理
单片机期末复习大题
CIPU, what impact does it have on the cloud computing industry?
ALC experiment
LeetCode中常用语言的一些基本方法记录
Media query, rem mobile terminal adaptation
LeetCode刷题记录(2)
In-depth Zabbix user guide - from the green boy
Unity realizes first-person roaming (nanny-level tutorial)
Cocos Creator Mini Game Case "Stick Soldier"
H5 的浏览器存储
Tencent greetings function SCF - entry instructions