When I make a graph with matplotlib, the color bar sticks out of the graph and it's awkward. I will show you how to align the color bars on the graph.
colorbar1.py
import numpy
import matplotlib.pyplot
x = numpy.arange(10)
y = numpy.arange(10)
X, Y = numpy.meshgrid(x, y)
Z = X**2. + Y**2.
fig = matplotlib.pyplot.figure()
ax = fig.add_subplot(111)
im = ax.imshow(Z, interpolation='none')
fig.colorbar(im)
fig.savefig('colorbar1.png')
An example where the height of the main graph and the color bar happened to be the same.
colorbar2.py
import numpy
import matplotlib.pyplot
x = numpy.arange(15)
y = numpy.arange(10)
X, Y = numpy.meshgrid(x, y)
Z = X**2. + Y**2.
fig = matplotlib.pyplot.figure()
ax = fig.add_subplot(111)
im = ax.imshow(Z, interpolation='none')
fig.colorbar(im)
fig.savefig('colorbar2.png')
Once the aspect ratio of the main graph collapses, the color bars stick out up and down. sad. .. ..
colorbar3.py
import numpy
import matplotlib.pyplot
import mpl_toolkits.axes_grid1
x = numpy.arange(15)
y = numpy.arange(10)
X, Y = numpy.meshgrid(x, y)
Z = X**2. + Y**2.
fig = matplotlib.pyplot.figure()
ax = fig.add_subplot(111)
divider = mpl_toolkits.axes_grid1.make_axes_locatable(ax)
cax = divider.append_axes('right', '5%', pad='3%')
im = ax.imshow(Z, interpolation='none')
fig.colorbar(im, cax=cax)
fig.savefig('colorbar3.png')
The most common example on the net. How to use make_axes_locatable and append_axes in mpl_toolkits.axes_grid1. This is the solution. It was good.
colorbar4.py
import numpy
import matplotlib.pyplot
import mpl_toolkits.axes_grid1
x = numpy.arange(15)
y = numpy.arange(10)
X, Y = numpy.meshgrid(x, y)
Z = X**2. + Y**2.
fig = matplotlib.pyplot.figure()
ax = fig.add_subplot(111, projection='polar')
divider = mpl_toolkits.axes_grid1.make_axes_locatable(ax)
cax = divider.append_axes('right', '5%', pad='3%')
im = ax.imshow(Z, interpolation='none')
fig.colorbar(im, cax=cax)
fig.savefig('colorbar4.png')
The color bar is not displayed! ?? Because type (cax) is matplotlib.projections.polar.PolarAxes. append_axes () does not have a projection argument and seems to use the projection of ax as it is. Is it possible to change the projection somehow? .. ..
colorbar4.py
import io
import numpy
import matplotlib.pyplot
x = numpy.arange(15)
y = numpy.arange(10)
X, Y = numpy.meshgrid(x, y)
Z = X**2. + Y**2.
fig = matplotlib.pyplot.figure()
ax = fig.add_subplot(111)
im = ax.imshow(Z, interpolation='none')
cax = fig.colorbar(im)
fig.savefig(io.BytesIO())
ax_pos = ax.get_position()
cax_pos0 = cax.ax.get_position()
cax_pos1 = [cax_pos0.x0, ax_pos.y0, cax_pos0.x1 - cax_pos0.x0, ax_pos.y1 - ax_pos.y0]
cax.ax.set_position(cax_pos1)
fig.savefig('colorbar5.png')
I was able to get the position of ax with get_position () and align it vertically using set_position () of cax. However, since ax.get_position () has a specification (?) That is not updated until fig.savefig () is executed, fig.savefig () will be called twice. This method is used in fig.savefig (bbox_inches ='tight').
Recommended Posts