2017-05-17 51 views
0

我试图实现由子图形成的生成图:plt.plot()plt.matshow(),其中两个图具有完全相同的大小。我的意思是一个地块的下边界和第二个地块的下边界位于相同的“高度”。与顶部边界线类似。当前效果显示在下图中。matplotlib中子图的相对大小

我还没有找到任何方式可用的资源,这将有助于我实现这一效果。如果你能帮助我,我将不胜感激。你有

shape=(2500, 2500) 
matrix=np.zeros(shape) 
print "Start of computing" 
for x in range(shape[0]) : 
    for y in range(shape[1]) : 
    matrix[x, y]=shapeFuction((x-shape[0]/2)/13.0, (y-shape[1]/2)/13.0, 2.0e-4, 9e-5, 1.0) 

print "Start of plotting" 
fig=plt.figure() 
ax = fig.add_subplot(1,2,2, aspect=1) 
ax.matshow(matrix, cmap="autumn") #data[250:501,150:351]) 
ax.set(adjustable='datalim', aspect=1) 
ax.set_xlabel("x") 
ax.set_ylabel("y") 
ax.xaxis.set_ticks_position('bottom') 
ax.set(adjustable='box-forced') #adjustable='datalim' 
ax.grid(b=False) 

print "Start of plotting part 2" 
ax = fig.add_subplot(1,2,1) 
phase=(9.0e-5*np.power(np.arange(0, shape[1])-shape[1]/2,3))/7 
g=ax.get_ylim() 
asp=shape[1]/float(abs(g[0]-g[1])) 
ax.plot(phase) #data[250:501,150:351]) 
ax.set(adjustable='box-forced')#, aspect=1.06/6.0) #adjustable='datalim''box-forced' 
ax.set_xlabel("x") 
ax.set_ylabel("Phase") 
plt.savefig('testData-x3.png') 
# plt.show() 

Current output plot from the code above

回答

0

一种选择是设置imshow情节方面(这通常是1,使得像素平方),以"auto"ax2.imshow(z, cmap="autumn", aspect="auto")

import matplotlib.pyplot as plt 
import numpy as np 

x = np.linspace(-3,3) 
y = np.tan(x) 
z = np.random.rand(30,30) 

fig, (ax, ax2) = plt.subplots(ncols=2) 

ax.plot(x,y) 
ax2.imshow(z, cmap="autumn", aspect="auto") 

plt.show() 

enter image description here

相反,如果您希望保留图像图的纵横比,则可以通过比较不同轴界限改变线图的该方面,

import matplotlib.pyplot as plt 
import numpy as np 

x = np.linspace(-3,3) 
y = np.tan(x) 
z = np.random.rand(30,30) 

fig, (ax, ax2) = plt.subplots(ncols=2) 

ax.plot(x,y) 
ax2.imshow(z, cmap="autumn") 

ratio = np.diff(ax.get_ylim())[0]/np.diff(ax.get_xlim())[0] 
ratio2 = np.diff(ax2.get_ylim())[0]/np.diff(ax2.get_xlim())[0] 
aspect = ratio2/ratio 
ax.set_aspect(float(np.abs(aspect))) 

plt.show() 

enter image description here

+0

第二个选项是我正在寻找的。但是我得到以下错误,我不明白哪个起源。你能看看这个吗? 'UnicodeWarning:统一等于比较未能两个参数为Unicode转换 - 将它们解释为是不平等的 如果方面==“正常”: UnicodeWarning:统一等于比较未能两个参数为Unicode转换 - 将它们解释为是不平等的 ('等于','自动')中的elif方面:' –

+0

好的。我只需要将其转换为Python float。感谢您的有效帮助! –

+0

我编辑了导致警告的那一行,谢谢指出。 – ImportanceOfBeingErnest

相关问题