Matplotlib imshow fixed aspect and vertical color bar corresponding to the height of the main axis

I need to build a grid with the values ​​of the "temperature map", which I currently use imshow, with the color map. This is described in a Matplotlib review , so I modified the example to customize the custom aspect of the picture:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

plt.figure()
ax = plt.gca()
im = ax.imshow(np.arange(100).reshape((10,10)), aspect=0.5)

# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)

plt.colorbar(im, cax=cax)

plt.savefig("test.png")

But the result is not the one I want, the color bar is above the main axis: test

Interestingly, when the color map is horizontal, it scales correctly:

cax = divider.append_axes("bottom", size="5%", pad=0.05)
plt.colorbar(im, cax=cax, orientation="horizontal")

horizontal

+4
source share
1 answer

, , 0,5 imshow. 2, . 2

, :

cax = fig.add_axes([0.85, 0.3, 0.04, 0.4])

... cax, y-, . , 5%, aspect = 1 1/20 . 1/2, 20 * 0,5 = 10. , , .

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

fig = plt.figure()
ax = plt.gca()
im = ax.imshow(np.arange(100).reshape((10,10)), aspect=0.5)

# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05, aspect=10)
#cax = fig.add_axes([0.85, 0.3, 0.04, 0.4])
plt.colorbar(im, cax=cax)

plt.show()
+2

All Articles