当前位置:网站首页>(subplots用法)matplotlib如何绘制多个子图(轴域)
(subplots用法)matplotlib如何绘制多个子图(轴域)
2022-07-03 05:07:00 【音程】
import matplotlib.pyplot as plt
#准备数据。
x=np.arange(3)
y1=np.random.rand((3))
y2=np.random.rand((3))
粗略
#得到绘图区以及子图。
fig,(ax1,ax2)=plt.subplots(1,2,figsize=(10,4))#指定绘图区为(10,4),并且一行两列,默认两列各占一半,即(5,4)(5,4)
ax1.plot(x,y1)
ax2.plot(x,y2)

精细
1.刻度
子图的刻度和plt的那个刻度用法不一样。
后者是:
plt.xticks(x,label)#就可以在x的位置处填写label值。
但是子图的用法是:
ax1.set_xticks(x)#在x的位置处标记数字。
ax1.set_xticklabels(label)#在x处标记标签。
效果如下:
#得到绘图区以及子图。
fig,(ax1,ax2)=plt.subplots(1,2,figsize=(10,4))#指定绘图区为(10,4),并且一行两列,默认两列各占一半,即(5,4)(5,4)
ax1.plot(x,y1)
ax2.plot(x,y2)
ax1.set_xticks(x)#在x的位置处标记数字。
# ax1.set_xticklabels(label)#在x处标记标签。

#得到绘图区以及子图。
fig,(ax1,ax2)=plt.subplots(1,2,figsize=(10,4))#指定绘图区为(10,4),并且一行两列,默认两列各占一半,即(5,4)(5,4)
ax1.plot(x,y1)
ax2.plot(x,y2)
ax1.set_xticks(x)#在x的位置处标记数字。
label=["a","b","c"]
ax1.set_xticklabels(label)#在x处标记标签。

2.大小
现在我们可以看到,我们上面设置整个绘图区是(10,4),然后默认就是各自一半,那么如何精细控制每一个子图的大小呢?
#得到绘图区以及子图。
fig,(ax1,ax2)=plt.subplots(1,2,figsize=(10,4))#指定绘图区为(10,4),并且一行两列,默认两列各占一半,即(5,4)(5,4)
ax1.plot(x,y1)
ax2.plot(x,y2)
ax1.set_position((0.1,0.1,0.8,0.8))#将ax1的左下角放在绘图区的0.1,0.1位置处(0.1指代的是绘图区的10%的意思)。ax1的左下角放置好了之后,宽为80%,高为80%。注意,此时ax1的右上角的位置是0.9,0.9。
ax2.set_position((0.9,0.9,0.1,0.1))#所以这里从0.9开始,否则会重叠。然后只占比0.1,刚好用完绘图区。

可以看到,一个大,一个小。
绝招
其实如果你有时间的话,子图(轴域)这个类官方文档中有写,axes其他属性可以见https://matplotlib.org/stable/api/axes_api.html?highlight=axes#module-matplotlib.axes。
总之,要有类的思想,axes(子图)是在figure(画布)中的。那为什么一到画图就很难学了呢?我的感觉是官方为了方便大家,构建子图,我们根本不是去构造axes类,而是调用五花八门的方法,例如上面的
subplots()
这样子,很容易上手,方便新手,但是却大大损害了进阶人士的理解,哎。
按照类的思想,我们应该如下:
先创建绘图区对象figure,然后将这个对象以及子图位置传入axes类的构造函数中,得到子图。然后绘制。
边栏推荐
- leetcode452. Detonate the balloon with the minimum number of arrows
- Notes | numpy-09 Broadcast
- Notes | numpy-07 Slice and index
- ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation
- Self introduction and objectives
- JDBC database operation
- Cross platform plug-in flutter for displaying local notifications_ local_ notifications
- 1118 birds in forest (25 points)
- Maximum continuous sub segment sum (dynamic programming, recursive, recursive)
- [set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
猜你喜欢
![[set theory] relational power operation (relational power operation | examples of relational power operation | properties of relational power operation)](/img/8b/c10423ee95200a0d94f9fb9dde76eb.jpg)
[set theory] relational power operation (relational power operation | examples of relational power operation | properties of relational power operation)

Shallow and first code
![[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree](/img/0f/bc8c44aee7a2c9dccac050b1060017.jpg)
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree

Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
![[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)](/img/2a/362f3b0491f721d89336d4f468c9dd.jpg)
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)

appium1.22.x 版本后的 appium inspector 需单独安装

cookie session jwt

Mobile terminal - uniapp development record (public request encapsulation)

JQ style, element operation, effect, filtering method and transformation, event object

leetcode406. Rebuild the queue based on height
随机推荐
Keepalived热备与HAProxy
1106 lowest price in supply chain (25 points)
微服务常见面试题
1118 birds in forest (25 points)
Gbase8s composite index (I)
Market status and development prospect prediction of the global forward fluorescent microscope industry in 2022
1107 social clusters (30 points)
[backtrader source code analysis 4] use Python to rewrite the first function of backtrader: time2num, which improves the efficiency by 2.2 times
Realize file download through the tag of < a > and customize the file name
Market status and development prospect prediction of the global autonomous hybrid underwater glider industry in 2022
Shuttle + alluxio accelerated memory shuffle take-off
How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in
1111 online map (30 points)
Current market situation and development prospect forecast of global UV sensitive resin 3D printer industry in 2022
Web APIs exclusivity
Market status and development prospects of the global IOT active infrared sensor industry in 2022
Common methods of JS array
The process of browser accessing the website
编译GCC遇到的“pthread.h” not found问题
The programmer resigned and was sentenced to 10 months for deleting the code. JD came home and said that it took 30000 to restore the database. Netizen: This is really a revenge