下面的代碼是問題的一個例子。
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 圖生成適當的軸限制,但為什么投影不更新? 
uj5u.com熱心網友回復:
似乎有一些關于從資料幀后切片的求和函式和/或索引生成的熊貓系列中的索引值的剩余資訊。
通過使用來自熊貓系列的原始 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://www.uj5u.com/houduan/335142.html
標籤:Python 数据框 matplotlib 显示
