下面的代码就是这个问题的一个例子。
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
setData = [0]*15
for x in range(15):
exData = np.random.random((256,1024))
index = list(range(0,256))
cols = list(range(0,1024))
df = pd.DataFrame(exData,index=index,columns=cols)
df = df.iloc[8:df.shape[0]-8,10:df.shape[1]-10]
setData[x] = df
bProjections = True
if bProjections:
for frame in range(len(setData)):
img = setData[frame]
print(img.shape)
#Projections
xProj = img.sum(axis=1)
yProj = img.sum(axis=0)
fig,ax = plt.subplots(2,2, figsize=(12,6))
ax[0][0].imshow(img,aspect='auto',cmap='plasma',vmin=img.stack().mean()-2*img.stack().std(),vmax=img.stack().mean()+2*img.stack().std())
ax[0][1].plot(xProj,img.index)
ax[1][0].plot(yProj)
ax[1][0].sharex(ax[0][0])
ax[0][1].sharey(ax[0][0])
ax[1][1].axis('off')
plt.show()结果是如下所示的图:

为什么轴没有更新以反映我已经对数据帧进行了切片?如果我打印数据帧的形状,它将其识别为240x1004,而轴仍然显示为256x1024……
我发现有趣的是,注释掉共享轴确实允许imshow图生成适当的轴限制,但为什么投影不更新?

发布于 2021-10-22 19:24:55
似乎有一些关于pandas系列中的索引值的剩余信息,这些索引值是从数据帧post切片的sum函数和或索引生成的。
通过使用pandas序列中的原始np数组,并为水平投影提供新的增量索引,获得了所需的输出。修改后的代码和示例输出附在附件中。
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
setData = [0]*15
for x in range(15):
exData = np.random.random((256,1024))
index = list(range(0,256))
cols = list(range(0,1024))
df = pd.DataFrame(exData,index=index,columns=cols)
ndf = df.iloc[8:df.shape[0]-8,10:df.shape[1]-10]
setData[x] = ndf
bProjections = True
if bProjections:
for frame in range(len(setData)):
img = setData[frame]
print(img.shape)
#Projections
xProj = img.sum(axis=1).values
yProj = img.sum(axis=0).values
fig,ax = plt.subplots(2,2, figsize=(12,6))
ax[0][0].imshow(img,aspect='auto',cmap='plasma',vmin=img.stack().mean()-2*img.stack().std(),vmax=img.stack().mean()+2*img.stack().std())
ax[0][1].plot(xProj,list(range(0,240,1)))
ax[1][0].plot(yProj)
ax[1][0].sharex(ax[0][0])
ax[0][1].sharey(ax[0][0])
ax[1][1].axis('off')
plt.show()

https://stackoverflow.com/questions/69681086
复制相似问题