服务器之家

服务器之家 > 正文

Python使用add_subplot与subplot画子图操作示例

时间:2021-02-28 00:12     来源/作者:有一种宿命叫无能为力

本文实例讲述了Python使用add_subplot与subplot画子图操作。分享给大家供大家参考,具体如下:

子图:就是在一张figure里面生成多张子图。

Matplotlib对象简介

   FigureCanvas  画布
   Figure        图
   Axes          坐标轴(实际画图的地方)

Python使用add_subplot与subplot画子图操作示例

注意,pyplot的方式中plt.subplot()参数和面向对象中的add_subplot()参数和含义都相同。

使用面向对象的方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/python
#coding: utf-8
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 100)
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax1.plot(x, x)
ax2 = fig.add_subplot(222)
ax2.plot(x, -x)
ax3 = fig.add_subplot(223)
ax3.plot(x, x ** 2)
ax4 = fig.add_subplot(224)
ax4.plot(x, np.log(x))
plt.show()

pyplot的方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/python
#coding: utf-8
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 100)
plt.subplot(221)
plt.plot(x, x)
plt.subplot(222)
plt.plot(x, -x)
plt.subplot(223)
plt.plot(x, x ** 2)
plt.subplot(224)
plt.plot(x, np.log(x))
plt.show()

运行结果:

Python使用add_subplot与subplot画子图操作示例

希望本文所述对大家Python程序设计有所帮助。

原文链接:https://blog.csdn.net/you_are_my_dream/article/details/53439518

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部