matplotlibimshowticker

thick offset problem with matplotlib imshow


Im trying to plot a correlation matrix with matplotlib imshow.

It's almost a success but my tick are ofset and the first is missing!?

this is my code:

fig1, ax = plt.subplots(1,1)
heatplot = ax.imshow(X.corr(), cmap='jet')
ax.set_xticklabels(X.columns)
ax.set_yticklabels(X.columns)
tick_spacing = 1
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
ax.yaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))

the results: Tmin should be over Tmax but was now on last row without it tick

enter image description here


Solution

  • Calling ax.xaxis.set_major_locator(ticker.MultipleLocator(1)) doesn't specify the starting tick position. Apparently, here matplotlib also sets a tick at position -1. Also, the tick positions should be fixed before calling set_xticklabels.

    The easiest is to call ax.set_xticks(range(len(X.columns))) to specify the exact ticks needed.

    from matplotlib import pyplot as plt
    import numpy as np
    import pandas as pd
    
    fig1, ax = plt.subplots(1, 1)
    X = pd.DataFrame(np.random.rand(4, 10), columns=[*'abcdefghij'])
    heatplot = ax.imshow(X.corr(), cmap='seismic_r', vmin=-1, vmax=1)
    ax.set_xticks(range(len(X.columns)))
    ax.set_yticks(range(len(X.columns)))
    ax.set_xticklabels(X.columns, rotation=90)
    ax.set_yticklabels(X.columns)
    plt.show()
    

    imshow with ticks

    A simpler interface is provided by seaborn, which does many things automatically:

    from matplotlib import pyplot as plt
    import seaborn as sns
    import numpy as np
    import pandas as pd
    
    X = pd.DataFrame(np.random.rand(4, 12), columns=['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'])
    sns.heatmap(data=X.corr(), cmap='RdYlGn', vmin=-1, vmax=1, annot=True)
    plt.xticks(rotation=30)
    plt.yticks(rotation=0)
    plt.tight_layout()
    plt.show()
    

    seaborn heatmap