Reputation: 1801
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:
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 !
Upvotes: 5
Views: 1778
Reputation: 327
I wasn't able to get the above method to work; it may be because I am additionally plotting in PyQt5.
If anyone else gets stumped here, I ultimately used the accepted solution at a different link to solve this problem (Matplotlib 2 Subplots, 1 Colorbar). Just set it up for one plot, then add the cbar_ax
for that plot. You'll still have to adjust the numbers a bit.
Upvotes: 0
Reputation: 339350
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()
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()
Upvotes: 5