当前位置:网站首页>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}$')
边栏推荐
- Detailed explanation of ten solutions across domains (summary)
- 指针常量与常量指针 巧记
- transport layer protocol
- This is indeed the best article on microservice architecture I have read!
- Tencent greetings function SCF - entry instructions
- BIO,NIO,AIO实践学习笔记(便于理解理论)
- 跨域的十种解决方案详解(总结)
- The hook of the operation of the selenium module
- The 25 best free games on mobile in 2020
- DevOps-了解学习
猜你喜欢

The highlight moment of operation and maintenance starts with intelligence

Chengyun Technology was invited to attend the 2022 Alibaba Cloud Partner Conference and won the "Gathering Strength and Going Far" Award

input详解之文件上传

多行文本省略

系统基础-学习笔记(一些命令记录)

滚动条问题,未解决

云计算基础-学习笔记

By solving these three problems, the operation and maintenance efficiency will exceed 90% of the hospital

disabledDate 日期选择器 datePicker

传输层协议
随机推荐
[issue resolved] - jenkins pipeline checkout timeout
Unity realizes first-person roaming (nanny-level tutorial)
vs2017关于函数命名方面的注意事项
初识网页与浏览器
Cloud Computing Basics - Study Notes
媒体查询、rem移动端适配
从“双卡双待“到”双通“,vivo率先推动DSDA架构落地
Tencent Cloud Message Queue CMQ
What should I do if the SSL certificate prompts that it is expired or invalid?
Successful indie developers deal with failure & imposters
程序员应该这样理解I/O
网络层协议介绍
多行文本省略
VRRP overview and experiment
传输层协议
网络协议基础-学习笔记
Proprietary host CDH
路由器和静态路由的配置
618, you may be able to wake up a little bit
请问下通过flink sql读取hologres 的两张表的 binlog,然后如何进行join?