[PYTHON] Match the colorbar to the figure with matplotlib

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()

normal.png

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)

fit.png

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

Match the colorbar to the figure with matplotlib
Add information to the bottom of the figure with Matplotlib
Align the size of the colorbar with matplotlib
How to assign multiple values to the Matplotlib colorbar
The road to download Matplotlib
[Python] Read the csv file and display the figure with matplotlib
Paste the matplotlib figure on excel
Easy to draw graphs with matplotlib
How to title multiple figures with matplotlib
[Python] Set the graph range with matplotlib
Make a partially zoomed figure with matplotlib
Set the xticklabels color individually with matplotlib
Display markers above the border with matplotlib
The road to compiling to Python 3 with Thrift
Add 95% confidence intervals on both sides to the diagram with Python / Matplotlib
Mouse over Matplotlib to display the corresponding image
A memo for making a figure that can be posted to a journal with matplotlib
Try to solve the fizzbuzz problem with Keras
Log in to the remote server with SSH
The easiest way to get started with Django
How to specify the NIC to scan with amazon-dash
Drawing tips with matplotlib on the server side
I tried to save the data with discord
The easiest way to synthesize speech with python
Try to solve the man-machine chart with Python
Increase the font size of the graph with matplotlib
I want to display multiple images with matplotlib.
[Python] How to draw multiple graphs with Matplotlib
Specify the Python executable to use with virtualenv
I wanted to play with the Bezier curve
How to try the friends-of-friends algorithm with pyfof
Say hello to the world with Python with IntelliJ
I wanted to visualize 3D particle simulation with the Python visualization library Matplotlib.
The easiest way to use OpenCV with python
Mathematics memorandum to keep up with the field # 1
Animation with matplotlib
Introduction to Python with Atom (on the way)
Save the object to a file with pickle
Japanese with matplotlib
How to display images continuously with matplotlib Note
The basis of graph theory with matplotlib animation
Mathematics memorandum to keep up with the field # 2
Animation with matplotlib
Visualize the behavior of the sorting algorithm with matplotlib
Histogram with matplotlib
Animate with matplotlib
How to Learn Kaldi with the JUST Corpus
matplotlib: Replace the axis itself with another one.
Mathematics memorandum to keep up with the field # 3
Try to create a battle record table with matplotlib from the data of "Schedule-kun"
When the variable you want to superscript with matplotlib is two or more characters
I tried to learn the sin function with chainer
Images created with matplotlib shift from dvi to pdf
Try to solve the programming challenge book with python3
Let's apply the brand image color to the matplotlib colormap!
How to delete the specified string with the sed command! !! !!
[Python] How to draw a line graph with Matplotlib
Just add the driver to the shape key with blender
[Introduction to Python] How to iterate with the range function?
How to create a submenu with the [Blender] plugin
Try to visualize the room with Raspberry Pi, part 1