Python 不同大小子地块的共享比例';轴(不共享轴)
使用matplotlib,我想用相同的x轴比例绘制两个图形,但我想显示不同大小的部分。我怎样才能做到这一点 到目前为止,我可以使用GridSpec或共享x轴的相同大小的子地块绘制不同大小的子地块。当我同时尝试两者时,较小的子地块具有相同的轴但缩放较小,而我想要相同的缩放和不同的轴,因此共享轴可能是错误的想法Python 不同大小子地块的共享比例';轴(不共享轴),python,matplotlib,scaling,subplot,Python,Matplotlib,Scaling,Subplot,使用matplotlib,我想用相同的x轴比例绘制两个图形,但我想显示不同大小的部分。我怎样才能做到这一点 到目前为止,我可以使用GridSpec或共享x轴的相同大小的子地块绘制不同大小的子地块。当我同时尝试两者时,较小的子地块具有相同的轴但缩放较小,而我想要相同的缩放和不同的轴,因此共享轴可能是错误的想法 import numpy as np import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec x=
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
x=np.linspace(0,10,100)
y=np.sin(x)
x2=np.linspace(0,5,60)
y2=np.cos(x2)
fig=plt.figure()
gs=GridSpec(2,3)
ax1 = fig.add_subplot(gs[0, :])
ax1.plot(x,y)
ax2 = fig.add_subplot(gs[1,:-1])
#using sharex=ax1 here decreases the scaling of ax2 too much
ax2.plot(x2,y2)
plt.show()
我希望x轴具有相同的缩放比例,也就是说,相同的x值总是正好位于彼此的顶部,这应该会让您有所了解。较小的绘图的框架可以扩展或适合绘图,这无关紧要,天平不匹配
提前谢谢。这还是有点粗糙。我相信有一种更优雅的方法可以做到这一点,但是您可以在
ax2
的轴坐标和ax1
的数据坐标之间创建自定义的转换(请参见)。换言之,您可以(根据ax1
)计算ax2
左右边缘对应位置的数据值,然后相应地调整ax2
的xlim
这里的演示表明,即使第二个子批次没有以任何特定方式与第一个子批次对齐,它也可以工作
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
x=np.linspace(0,25,100)
y=np.sin(x)
x2=np.linspace(10,30,60)
y2=np.cos(x2)
fig=plt.figure()
gs=GridSpec(2,6)
ax1 = fig.add_subplot(gs[0, :])
ax1.plot(x,y)
ax2 = fig.add_subplot(gs[1,3:-1])
ax2.plot(x2,y2)
# here is where the magic happens
trans = ax2.transAxes + ax1.transData.inverted()
((xmin,_),(xmax,_)) = trans.transform([[0,1],[1,1]])
ax2.set_xlim(xmin,xmax)
# for demonstration, show that the vertical lines end up aligned
for ax in [ax1,ax2]:
for pos in [15,20]:
ax.axvline(pos)
plt.show()
编辑:一种可能的优化方法是在中进行转换。这样,即使在第一个轴中缩放/平移时,轴也保持同步
正如您所指出的,tight_layout()
也有一个小问题,但是直接调用回调函数可以很容易地解决
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
def on_xlim_changed(event):
# here is where the magic happens
trans = ax2.transAxes + ax1.transData.inverted()
((xmin, _), (xmax, _)) = trans.transform([[0, 1], [1, 1]])
ax2.set_xlim(xmin, xmax)
x = np.linspace(0, 25, 100)
y = np.sin(x)
x2 = np.linspace(10, 30, 60)
y2 = np.cos(x2)
fig = plt.figure()
gs = GridSpec(2, 6)
ax1 = fig.add_subplot(gs[0, :])
ax1.plot(x, y)
ax2 = fig.add_subplot(gs[1, 3:-1])
ax2.plot(x2, y2)
# for demonstration, show that the vertical lines end up aligned
for ax in [ax1, ax2]:
for pos in [15, 20]:
ax.axvline(pos)
# tight_layout() messes up the axes xlim
# but can be fixed by calling on_xlim_changed()
fig.tight_layout()
on_xlim_changed(None)
ax1.callbacks.connect('xlim_changed', on_xlim_changed)
plt.show()
我建议根据ax1的限制设置第二个轴的限制
试试这个
ax2 = fig.add_subplot(gs[1,:-1])
ax2.plot(x2,y2)
lb, ub = ax1.get_xlim()
# Default margin is 0.05, which would be used for auto-scaling, hence reduce that here
# Set lower bound and upper bound based on the grid size, which you choose for second plot
ax2.set_xlim(lb, ub *(2/3) -0.5)
plt.show()
对于绘图问题,如果您可以提供所需内容的可视化表示,这将非常有用。我想您可能想使用get\u
函数从ax1中提取轴信息,然后使用ax2。在纠正大小差异后,根据需要设置\u
。我添加了一个“如果”的图形,希望能让您了解我的意思。谢谢,这正是我所需要的,无论子地块位于何处,它都能完美工作。我注意到有两件事可以帮助防止任何错误(但可能非常明显),那就是在转换之前设置网格、刻度等。也不适用于紧密布局我添加了一些改进,以在平移/缩放时保持轴同步,并修复了tight\u layout()