Search code examples
pythonmatplotlibplotdata-visualizationsubplot

Create a subplot without axis but with title and y label


I am trying to create a subplot with the axis kept off but I want to include the labels. Here is my code. I read somewhere that if I do plt.axis('off') it would even hide the labels. Is there any workaround this? Here is my code.

fig, ax = plt.subplots(3, 10, figsize=(20,7))
i = 0
for row in range(3):
    for col in range(10):
        if row == 0:
            ax[row][col].imshow(final[i])
            ax[row][col].set_title(temp_df.columns[col])
        
        ax[row][col].tick_params(top='off', bottom='off', left='off', right='off', labelleft='on', labelbottom='off')
        ax[row][col].imshow(final[i])
        # ax[row][col].axis('off')
        # ax[row][col].tick_params(axis='both', which='both',length=0)
        if col == 0:
            ax[row][col].set_ylabel(str(cols[row]))
        
        i += 1

Snapshot of the subplot generated:

How can I generate a plot with axis kept off and have the labels in the plot?


Solution

  • If I understand correctly what you want, you can remove axis ticks by passing an empty list to ax.set_xticks and ax.set_yticks:

    ax[row][col].set_xticks([])
    ax[row][col].set_yticks([])
    

    Complete Code

    import matplotlib.pyplot as plt
    import numpy as np
    
    fig, ax = plt.subplots(3, 10, figsize = (20, 7))
    for row in range(3):
        for col in range(10):
            if row == 0:
                ax[row][col].set_title('your title')
    
            ax[row][col].tick_params(top = 'off', bottom = 'off', left = 'off', right = 'off', labelleft = 'on', labelbottom = 'on')
            ax[row][col].imshow(np.random.randint(0, 10, (10, 10)))
            ax[row][col].set_xticks([])
            ax[row][col].set_yticks([])
            ax[row][col].set_xlabel('x label')
            if col == 0:
                ax[row][col].set_ylabel(f'row = {row}')
    
    plt.show()
    

    enter image description here