栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

22.matplotlib共享个别子区绘图区域的坐标轴

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

22.matplotlib共享个别子区绘图区域的坐标轴

文章目录
  • 1.设置方法
  • 2.用函数autoscale()调整坐标轴范围

1.设置方法
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

x1 = np.linspace(0,2*np.pi,400)
y1 = np.cos(x1**2)

x2 = np.linspace(0.01,10,100)
y2 = np.sin(x2)

x3 = np.random.rand(100)
y3 = np.linspace(0,3,100)

x4 = np.arange(0,6,0.5)
y4 = np.power(x4,3)

# set (2,2) subplots
fig,ax = plt.subplots(2,2)

# set chart of each subplot
ax1 = plt.subplot(221)
ax1.plot(x1,y1)

ax2 = plt.subplot(222)
ax2.plot(x2,y2)

ax3 = plt.subplot(223)
ax3.scatter(x3,y3)

ax4 = plt.subplot(224,sharex=ax1)
ax4.scatter(x4,y4)

plt.show()

2.用函数autoscale()调整坐标轴范围

autoscale()进行坐标轴的自适应调整,以使图形可以非常紧凑的填充绘图区域。调用签名是
autoscale(enable=True,axis=“both”,tight=True)
enable:进行坐标轴范围的自适应调整
axis:使x,y轴都进行自适应调整
tight:让坐标轴的范围调整到数据的范围上
例如将子区3的x轴范围调整到数据范围上,只需在子区3的代码部分增加下面的代码

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

x1 = np.linspace(0,2*np.pi,400)
y1 = np.cos(x1**2)

x2 = np.linspace(0.01,10,100)
y2 = np.sin(x2)

x3 = np.random.rand(100)
y3 = np.linspace(0,3,100)

x4 = np.arange(0,6,0.5)
y4 = np.power(x4,3)

# set (2,2) subplots
fig,ax = plt.subplots(2,2)

# set chart of each subplot
ax1 = plt.subplot(221)
ax1.plot(x1,y1)

ax2 = plt.subplot(222)
ax2.plot(x2,y2)

ax3 = plt.subplot(223)
plt.autoscale(enable=True,axis="both",tight=True)
ax3.scatter(x3,y3)

ax4 = plt.subplot(224,sharex=ax1)
ax4.scatter(x4,y4)

plt.show()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/932343.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号