nalfahel
nalfahel

Reputation: 145

Grid lines not aligned with cells when defining minor ticks in Matplotlib Heatmap

I am using the code below to generate this heatmap: heatmap

dim = np.arange(1, 32, 1)

fig, ax = plt.subplots(figsize=(7,9))
heatmap = ax.imshow(h.T, cmap=plt.cm.get_cmap('Blues', 4), clim=[1,144])
cbaxes = fig.add_axes([.8, .35, .04, .3]) 
cbar = fig.colorbar(heatmap, ticks = [1, 36, 72, 108, 144], label =  'Number of valid records per day', cax = cbaxes)
ax.set_ylabel("Days", fontsize=15)
ax.set_xlabel("Months", fontsize=15)
ax.set_title("Number of valid records per day", fontsize=20)
ax.set_yticks(range(0,31))
ax.set_yticklabels(dim, ha='center', minor=False, fontsize=12)
ax.set_xticks(range(0,13,1))
ax.set_xticklabels(ylabel, rotation = 45, ha = 'right')

ax.set_facecolor('gray')
cbar.set_label('Number of valid records')
ax.xaxis.set_minor_locator(MultipleLocator(0.5))
ax.yaxis.set_minor_locator(MultipleLocator(0.5))

ax.tick_params(axis='y', which='major', pad=10)
ax.grid(which = 'minor', color = 'w')
fig.show()

As you can see there is a slight offset of the gridlines with respect to the heat map cells. Why is that? How can I fix it?

Upvotes: 1

Views: 534

Answers (1)

nalfahel
nalfahel

Reputation: 145

Thanks to the comment left by Jody Klymak, I added the following line of code at the beginning of my notebook and it solved the problem:

matplotlib.rcParams['figure.dpi'] = 300

Upvotes: 1

Related Questions