0

everyone,

I have problem when I tried to adjust colorbar to the same height with figure. I know little about the intrinsic mechanism of data visualization, or axis, fig or something like that. my code is following, sorry for unloading images,

For Figure(1), notice that input data is square, i.e., 51 by 51. The figure is satisfying. import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.axes_grid1 import make_axes_locatable

plt.cla()
plt.clf()

fig1 = plt.figure(1)
ax0 = plt.subplot()
im0 = ax0.imshow(np.arange(51*51).reshape((51,51)), cmap="hsv")

divider0 = make_axes_locatable(ax0)
ax_cb0 = divider0.append_axes("right", size="2%", pad=0.05)
fig1.add_axes(ax_cb0)
plt.colorbar(im0, cax=ax_cb0)
plt.savefig("tmp0.png", bbox_inches="tight")

For figure (2), the code is following, notice that now the input data is 51 by 501, the output is not satisfying,

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable
plt.cla()
plt.clf()

fig2 = plt.figure(2)
ax1 = plt.subplot()
im1 = ax1.imshow(np.arange(51*501).reshape((51,501)), cmap="hsv")

divider1 = make_axes_locatable(ax1)
ax_cb1 = divider1.append_axes("right", size="2%", pad=-4)
fig2.add_axes(ax_cb1)
plt.colorbar(im1, cax=ax_cb1)
ax1.set_aspect(4)

plt.savefig("tmp1.png", bbox_inches="tight")

but still, we can make it better by manually adjusting pad parameter in this line

ax_cb0 = divider0.append_axes("right", size="2%", pad=0.05)

but which is absolutely not the recommended way, COULD anyone know the smart way of doing it or smart way of estimating the value of pad parameter? Thanks in advance.

Daniel
  • 1
  • 2
  • I don't understand your problem. Please post an image (or a link to image if not enough rep) showing your result and a mockup of what you are trying to achieve – Diziet Asahi Aug 17 '16 at 10:33
  • Possible duplicate of [Set Matplotlib colorbar size to match graph](http://stackoverflow.com/questions/18195758/set-matplotlib-colorbar-size-to-match-graph) – evtoh Aug 17 '16 at 11:10
  • sorry that I failed to upload a image, also I search some solutions from Internet, but as I stated, their solution seems to work well only for square matrix. – Daniel Aug 17 '16 at 11:35

0 Answers0