2017-04-26 76 views
0

我有多个具有相同x轴的绘图。我想将它们堆叠在一个报告中,并让所有内容排成一列。但是,matplotlib似乎会根据y tick标签长度稍微调整它们。强制matplotlib修复绘图区域

相对于我保存的pdf画布,是否可以强制绘图区域和位置在绘图中保持相同?

import numpy as np 
import matplotlib.pyplot as plt 
xs=np.arange(0.,2.,0.00001) 
ys1=np.sin(xs*10.) #makes the long yticklabels 
ys2=10.*np.sin(xs*10.)+10. #makes the short yticklabels 

fig=plt.figure() #this plot ends up shifted right on the canvas 
plt.plot(xs,ys1,linewidth=2.0) 
plt.xlabel('x') 
plt.ylabel('y') 

fig=plt.figure() #this plot ends up further left on the canvas 
plt.plot(xs,ys2,linewidth=2.0) 
plt.xlabel('x') 
plt.ylabel('y') 
+0

您可以将它们作为子图在同一图中绘制? – DavidG

+0

在这两种情况下,情节都具有相同的尺寸。此外,轴的尺寸也相同。目前尚不清楚你要求什么。 – ImportanceOfBeingErnest

回答

2

你的问题是有点不清楚,但是绘制它们作为次要情节在同一个数字应该gaurantee两个次要情节的轴和数字大小将彼此

import numpy as np 
import matplotlib.pyplot as plt 

xs=np.arange(0.,2.,0.00001) 
ys1=np.sin(xs*10.) #makes the long yticklabels 
ys2=10.*np.sin(xs*10.)+10. #makes the short yticklabels 

fig, (ax1, ax2) = plt.subplots(2, 1) 
ax1.plot(xs,ys1,linewidth=2.0) 
ax1.set_xlabel('x') 
ax1.set_ylabel('y') 

ax2.plot(xs,ys2,linewidth=2.0) 
ax2.set_xlabel('x') 
ax2.set_ylabel('y') 

plt.subplots_adjust(hspace=0.3) # adjust spacing between plots  
plt.show() 

这将产生被神韵:如下图所示:

enter image description here

0

使用次要情节具有相同x轴应该做的伎俩。

当您创建子图时使用sharex=Truesharex的好处是在1个子图上缩放或平移也可以在共享轴的所有子图上自动更新。

import numpy as np 
import matplotlib.pyplot as plt 
xs = np.arange(0., 2., 0.00001) 
ys1 = np.sin(xs * 10.) # makes the long yticklabels 
ys2 = 10. * np.sin(xs * 10.) + 10. # makes the short yticklabels 

fig, (ax1, ax2) = plt.subplots(2, sharex=True) 
ax1.plot(xs, ys1, linewidth=2.0) 
ax1.xlabel('x') 
ax1.ylabel('y') 

ax2.plot(xs, ys2, linewidth=2.0) 
ax2.xlabel('x') 
ax2.ylabel('y') 
plt.show()