Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

imshow colormap figure and the suptitle don't align in the center

It's a very small issue but still I can't figure it out. I'm using imshow with matplotlib to plot a colormap - but the result is that the figure and the title are not aligned together:

enter image description here

The code i'm using for the plot is:

fig, ax = plt.subplots(figsize=(27, 10))

cax1 = ax.imshow(reversed_df, origin='lower', cmap='viridis', interpolation = 'nearest', aspect=0.55)

ylabels = ['0:00', '03:00', '06:00', '09:00', '12:00', '15:00', '18:00', '21:00']
major_ticks = np.arange(0, 24, 3)
ax.set_yticks(major_ticks)
ax.set_yticklabels(ylabels, fontsize = 15)

xlabels = ['Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Jan17']
xmajor_ticks = np.arange(0,12,1)
ax.set_xticks(xmajor_ticks)
ax.set_xticklabels(xlabels, fontsize = 15)
fig.autofmt_xdate()

fmt = '%1.2f'
cb = plt.colorbar(cax1,fraction=0.046, pad=0.04, format=fmt)
cb.update_ticks

fig.suptitle('2016 Monthly Pressure Data (no Normalization) $[mbar]$',fontsize=20, horizontalalignment='center')

fig.savefig('Pressure 2016 Full.jpeg', dpi=300)
plt.show()

Any ideas?

Thank you !

like image 990
ValientProcess Avatar asked Mar 17 '17 07:03

ValientProcess


1 Answers

This issue occurs when the axes aspect is set to "equal", which is the case for an imshow plot, and a colorbar is added.

import matplotlib.pyplot as plt
import numpy as np

x = np.random.rand(5,5)
fig, ax = plt.subplots(figsize=(12, 3))
fig.patch.set_facecolor('#dbf4d9')

im = ax.imshow(x)

fig.colorbar(im)
plt.show()

enter image description here

A workaround would be to set the subplot parameters left and right such that the image is in the center. This may require some trial and error, but works as follows:

import matplotlib.pyplot as plt
import numpy as np

x = np.random.rand(5,5)
fig, ax = plt.subplots(figsize=(12, 3))
plt.subplots_adjust(left=0.2, right=0.68)
fig.patch.set_facecolor('#dbf4d9')

im = ax.imshow(x)

fig.colorbar(im)
plt.show()

enter image description here

like image 166
ImportanceOfBeingErnest Avatar answered Nov 01 '22 17:11

ImportanceOfBeingErnest