The axis function of matplotlib is convenient and makes it easy to adjust the coordinates.
ʻAxis ("tight") makes the margins as small as possible, ʻAxis ("image")
keeps the same thing with the aspect ratio.
But adding colorbar
makes it confusing.
Consider the following simple case:
import numpy as np
import matplotlib.pyplot as plt
Nx = 100
Ny = 20
a = np.random.rand(Ny, Nx)
x = range(Nx)
y = range(Ny)
X, Y = np.meshgrid(x, y)
plt.pcolormesh(X, Y, a)
plt.axis("image")
plt.colorbar()
I think it's a personal hobby how you want the colorbar to appear at this time. This is not my preference.
That's where make_axes_locatable
comes in.
from mpl_toolkits.axes_grid1 import make_axes_locatable
fig, ax = plt.subplots()
image = ax.pcolormesh(X, Y, a)
ax.axis("image")
divider = make_axes_locatable(ax)
ax_cb = divider.new_horizontal(size="2%", pad=0.05)
fig.add_axes(ax_cb)
plt.colorbar(image, cax=ax_cb)
If you want to generate multiple diagrams, I think this compact is better. When generating multiple figures
fig = plt.figure()
ax = plt.subplot(311)
If you get it like this, the rest is the same.
Recommended Posts