Home > Software engineering >  thick offset problem with matplotlib imshow
thick offset problem with matplotlib imshow

Time:10-21

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

CodePudding user response:

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 heatmap

  • Related