索引超出边界的多个子图,除法剩余 python
index out of bound multiple subplots with division rest python
我正在从包含反应结果的数据框中创建多个子图(每个数据框 70 个),我想绘制 12 x 12 的反应图,以便在进行更彻底的分析之前快速查看。由于 70/12 留下余数,简单的实现将 运行 超出范围。我可以通过使用 "if,else" 语句来解决这个问题,但它既不优雅也不高效。我想知道是否有更好的选择。
warDf 的大小为 70,meanDf 的大小为 130x70。时间,pcmean 和 ncmean 的大小为 130。我正在使用库 pandas (pd)、numpy (np) 和 matplotlib.pyplot (plt)、
it=int(np.ceil(np.size(warDf)/12))# defining what to loop over
kk=0
for kk in np.arange(0,it):
#declaring the subplots
fig,axes=plt.subplots(nrows=3,ncols=4,sharex='col',sharey='row')
#transforming axes in a usable list
axe_list=[item for sublist in axes for item in sublist]
# checking that I don't run out of bond
if (12*kk+12<np.size(warDf)):
k=0
# plotting each graph in its corresponding subplot
for k in np.arange(0,12):
ax=axe_list.pop(0)
ax.plot(time,meanDf.iloc[:,12*kk+k],label=(meanDf.columns[12*kk+k]),color='blue')
ax.plot(time,pcmean,color='green')
ax.plot(time,ncmean,color='red')
ax.set_ylabel('fluorescence')
ax.set_xlabel('time/ (s)')
ax.legend()
else: # better option??
k=0
s2=np.size(warDf)-12*kk
for k in np.arange(0,s2):
ax=axe_list.pop(0)
ax.plot(time,meanDf.iloc[:,12*kk+k],label=(meanDf.columns[12*kk+k]),color='blue')
ax.plot(time,pcmean,color='green')
ax.plot(time,ncmean,color='red')
ax.set_ylabel('fluorescence')
ax.set_xlabel('time/ (s)')
ax.legend()
plt.show()
您可以使用 min()
函数。将整个 if/else
替换为:
k=0 # note: you don't have to pre-define k here
s2 = min(np.size(warDf) - 12 * kk, 12) # new part
for k in np.arange(0,s2): # the rest is the same as in the body of the else
ax=axe_list.pop(0)
ax.plot(time,meanDf.iloc[:,12*kk+k],label=(meanDf.columns[12*kk+k]),color='blue')
ax.plot(time,pcmean,color='green')
ax.plot(time,ncmean,color='red')
ax.set_ylabel('fluorescence')
ax.set_xlabel('time/ (s)')
ax.legend()
说明
您目前有
if (12 * kk + 12 < np.size(warDf)):
s2 = 12 # define s2 as a variable here as well
for k in np.arange(0, s2):
# ...
else:
s2 = np.size(warDf) - 12 * kk
for k in np.arrange(0, s2):
# ...
重新排列第一个 if
,我们可以得到:
if (12 < np.size(warDf) - 12 * kk):
s2 = 12
# ...
else:
s2 = np.size(warDf) - 12 * kk
# ...
现在可以看到if右边和s2的赋值是一样的。如果 12 小于,则使用 12。否则,使用 np.size(warDf) - 12 * kk
。这是min()
.
的定义
我正在从包含反应结果的数据框中创建多个子图(每个数据框 70 个),我想绘制 12 x 12 的反应图,以便在进行更彻底的分析之前快速查看。由于 70/12 留下余数,简单的实现将 运行 超出范围。我可以通过使用 "if,else" 语句来解决这个问题,但它既不优雅也不高效。我想知道是否有更好的选择。 warDf 的大小为 70,meanDf 的大小为 130x70。时间,pcmean 和 ncmean 的大小为 130。我正在使用库 pandas (pd)、numpy (np) 和 matplotlib.pyplot (plt)、
it=int(np.ceil(np.size(warDf)/12))# defining what to loop over
kk=0
for kk in np.arange(0,it):
#declaring the subplots
fig,axes=plt.subplots(nrows=3,ncols=4,sharex='col',sharey='row')
#transforming axes in a usable list
axe_list=[item for sublist in axes for item in sublist]
# checking that I don't run out of bond
if (12*kk+12<np.size(warDf)):
k=0
# plotting each graph in its corresponding subplot
for k in np.arange(0,12):
ax=axe_list.pop(0)
ax.plot(time,meanDf.iloc[:,12*kk+k],label=(meanDf.columns[12*kk+k]),color='blue')
ax.plot(time,pcmean,color='green')
ax.plot(time,ncmean,color='red')
ax.set_ylabel('fluorescence')
ax.set_xlabel('time/ (s)')
ax.legend()
else: # better option??
k=0
s2=np.size(warDf)-12*kk
for k in np.arange(0,s2):
ax=axe_list.pop(0)
ax.plot(time,meanDf.iloc[:,12*kk+k],label=(meanDf.columns[12*kk+k]),color='blue')
ax.plot(time,pcmean,color='green')
ax.plot(time,ncmean,color='red')
ax.set_ylabel('fluorescence')
ax.set_xlabel('time/ (s)')
ax.legend()
plt.show()
您可以使用 min()
函数。将整个 if/else
替换为:
k=0 # note: you don't have to pre-define k here
s2 = min(np.size(warDf) - 12 * kk, 12) # new part
for k in np.arange(0,s2): # the rest is the same as in the body of the else
ax=axe_list.pop(0)
ax.plot(time,meanDf.iloc[:,12*kk+k],label=(meanDf.columns[12*kk+k]),color='blue')
ax.plot(time,pcmean,color='green')
ax.plot(time,ncmean,color='red')
ax.set_ylabel('fluorescence')
ax.set_xlabel('time/ (s)')
ax.legend()
说明
您目前有
if (12 * kk + 12 < np.size(warDf)):
s2 = 12 # define s2 as a variable here as well
for k in np.arange(0, s2):
# ...
else:
s2 = np.size(warDf) - 12 * kk
for k in np.arrange(0, s2):
# ...
重新排列第一个 if
,我们可以得到:
if (12 < np.size(warDf) - 12 * kk):
s2 = 12
# ...
else:
s2 = np.size(warDf) - 12 * kk
# ...
现在可以看到if右边和s2的赋值是一样的。如果 12 小于,则使用 12。否则,使用 np.size(warDf) - 12 * kk
。这是min()
.