我想要一个分为4个扇区的窗口:在(0,0)一个imshow图像(ax1);(1,0)一个使用twinx()图像分割窗口的子图(ax2&ax3);(1,1)一个常规的绘图图像(ax4);以及一个迭代的绘图部分(0,1),它应该给出一个比另一个(ax5)一个的“数量_个子图”。希望没有xticklabels,只有最后一个。
This is how the frame should look like before the iterative subplot creation.
我的问题:当迭代在窗口的右上角空间创建子窗口时,子批距该空间越远,并消除了ax4
This is how the window looks after the "for" cyle for the subplot creation
下面您将看到我使用的代码的简化,这样您可以更好地看到它。我已经用随机数代替了我的实验数据,这样你就可以很容易地复制了。
你能告诉我我做错了什么吗?我仍然不能控制python中的所有处理程序。几年前我曾经在matlab中做过类似的事情。
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import numpy as np
import pdb
pos = [1,2,3,4,5]
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
xx = np.linspace(0, 20, 1000)
fig1 = plt.figure()
number_of_subplots = len(pos) #number between 1-7
ax1 = plt.subplot2grid((number_of_subplots+1,2),(0,0),rowspan = number_of_subplots-1) # Here the idea is to "dinamically" create the division of the grid, making space at the bottom of it for the image in the bottom left.
ax1.scatter(x,y)
ax2 = plt.subplot2grid((number_of_subplots+1,2),(number_of_subplots-1,0), rowspan = 2)
ax2.plot(xx,np.sin(xx),label = 'sin(x)',color = 'b')
ax3 = ax2.twinx()
ax3.plot(xx,np.cos(xx), label = 'cos(x)', color = 'r')
ax4 = plt.subplot2grid((number_of_subplots+1,2),(number_of_subplots-1,1), rowspan = 2)
ax4.plot(xx,np.tan(xx), label = 'tan(x)', color = 'g')
for i,v in enumerate(xrange(number_of_subplots)):
v = v+1
ax5 = plt.subplot2grid((number_of_subplots+1,2),(v-1,1))
ax5.plot(np.sin(xx+3.1416*v/2)) # Grafica los perfiles, asociandoles el mismo color que para los cortes en la imagen 2D
if (i % 2 == 0): #Even
ax5.yaxis.tick_left()
else:
ax5.yaxis.tick_right()
plt.draw()
plt.show()
通过使用GridSpec解决了这个问题。下面是给出以下解决方案的代码实现。在
This is the correct way the image should look like and the implementation is below on the code.
相关问题 更多 >
编程相关推荐