当前位置: 移动技术网 > IT编程>脚本编程>Python > Python使用add_subplot与subplot画子图操作示例

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

2018年08月20日  | 移动技术网IT编程  | 我要评论

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

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

matplotlib对象简介

   figurecanvas  画布
   figure        图
   axes          坐标轴(实际画图的地方)

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

使用面向对象的方式

#!/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的方式

#!/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相关内容感兴趣的读者可查看本站专题:《python数学运算技巧总结》、《python字符串操作技巧汇总》、《python编码操作技巧总结》、《python数据结构与算法教程》、《python函数使用技巧总结》、《python入门与进阶经典教程》及《python文件与目录操作技巧汇总

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网