I´ve got the following problem, that my colorbar overlays my papersized subplots that are "printed" as an pdf.
Code example:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
if __name__ == '__main__':
titles = np.random.uniform(low=1, high=11, size=(1,10))
temp = np.random.uniform(low=23, high=200, size=(10,10))
ctf0102 = np.random.uniform(low=23, high=200, size=(10,10))
ctf03 = np.random.uniform(low=23, high=200, size=(10,10))
fig = plt.figure(figsize=(11.69,8.27), dpi=100)
for num in range(len(titles)):
ax = fig.add_subplot(3,4,num)
im = ax.scatter(ctf03[1:,num], ctf0102[:,num], 12, temp[:,num], cmap='magma')
ax.set_title(titles[num])
fig.text(0.5, -0.00, '...', ha='center', fontsize=16)
fig.text(-0.00, 0.5, '...', va='center', rotation='vertical', fontsize=16)
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(im, cax=cbar_ax)
plt.tight_layout(pad=2)
fig.savefig("example.pdf", dpi=fig.dpi, bbox_inches='tight', pad_inches=0.3)
Any idea how i can move the colorbar to the right, by resizing the space of all my subplots? Besides that i would like to enlarge the colorbar, to become a smooth closing with the top and bottom of all subplots.
Thanks for help and ideas.
Use fig.subplots_adjust
to set the bottom, top, left and right position of the subplots. Then use the same bottom
and top
to define where the colorbar is placed vertically. And make sure the left edge of the colorbar is to the right of the subplots.
Also, you can't use tight_layout
here, otherwise it changes the positions.
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
fig = plt.figure(figsize=(11.69,8.27), dpi=100)
bottom, top = 0.1, 0.9
left, right = 0.1, 0.8
fig.subplots_adjust(top=top, bottom=bottom, left=left, right=right, hspace=0.15, wspace=0.25)
for num in range(12):
ax = fig.add_subplot(3,4,num+1)
im = ax.scatter(np.random.rand(10), np.random.rand(10), 12, np.random.rand(10), cmap='magma')
fig.text(0.5, 0.02, 'x label', ha='center', fontsize=16)
fig.text(0.02, 0.5, 'ylabel ', va='center', rotation='vertical', fontsize=16)
cbar_ax = fig.add_axes([0.85, bottom, 0.05, top-bottom])
fig.colorbar(im, cax=cbar_ax)
fig.savefig("example.pdf", dpi=fig.dpi, pad_inches=0.3)