matplotlib

时间:2017-05-17 16:47:25

标签: python matplotlib plot

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

我没有在可用资源中找到任何可以帮助我实现这种效果的方法。如果你能帮助我,我将不胜感激。

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

1 个答案:

答案 0 :(得分: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