Home > Back-end >  matplotlib imshow on sliced dataframe axis alignment
matplotlib imshow on sliced dataframe axis alignment

Time:10-25

The code below is an example of the problem.

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()

The result is a plot like this :WHY THE WHITE SPACE Why doesn't the axis update to reflect that I've sliced the dataframe? If I print the shape of the dataframe, it recognizes it as 240x1004 yet the axis still shows out to 256x1024...

I found it interesting to note that commenting out the shared axis does allow for the imshow plot to generate proper axis limits, but why don't the projections update? no axis share

CodePudding user response:

There appears to be some left over information regarding the index values in the pandas series generated from the sum functions and or index of the dataframe post slicing.

By using the raw np array from the pandas series and providing a fresh incremented index for the horizontal projection, the desired output was obtained. The modified code and example output is attached.

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()

enter image description here

  • Related