Reputation: 1879
I have a plot I made in ipython notebook using two imported datasets & an array I made for the x-axis, but the colorbar is a bit thick for my liking. Is there any way to make it slimmer?
#import packages
import numpy as np #for importing u array
import matplotlib.pyplot as plt #for plotting
%matplotlib inline
th = np.loadtxt("MT3_th.dat") #imports data file- theta pert.
pi = np.loadtxt("MT3_pi.dat") #imports data file- pressure pert.
k = np.loadtxt("MT3_z.dat") #imports data file- height
i = np.linspace(-16.,16.,81) #x-axis array
x, z = np.meshgrid(i, k)
th[th == 0.0] = np.nan #makes zero values for th white
clevels = [0.5, 1, 1.5, 2, 2.5, 3.]
fig = plt.figure(figsize=(12,12))
thplot = plt.contourf(x, z, th, clevels, cmap=plt.cm.Reds, vmin=0., vmax=3.)
ax2 = fig.add_subplot(111)
piplot = ax2.contour(x, z, pi, 6, colors='k')
plt.axis([-16.,16, 0, 16.])
plt.xticks(np.arange(-16,16.001,4))
plt.yticks(np.arange(0,16.001,2))
plt.colorbar(pad=0.05, orientation="horizontal")
plt.xlabel("x (km)", size=15)
plt.ylabel("z (km)", size=15)
plt.title("Initial temp. & pres. perturbations (K, Pa)", size=20)
plt.grid()
plt.show()
Upvotes: 29
Views: 47030
Reputation: 57
I was able to use a combination of fraction
and aspect
parameters to get the desired width of the colorbar while it still stretched across the plot.
Upvotes: 1
Reputation: 1879
I was able to manually fix it using add_axes. (Thank goodness for colleagues!)
cbar_ax = fig.add_axes([0.09, 0.06, 0.84, 0.02])
fig.colorbar(thplot, cax=cbar_ax, orientation="horizontal")
Upvotes: 11
Reputation: 595
Looking for the same answer I think I found something easier to work with compatible with the current version of Matplotlib to decrease the width of the colorbar. One can use the "ratio" option of the matplolib.figure function (see http://matplotlib.org/api/figure_api.html). And here is an example :
import numpy as np
from matplotlib import pyplot as plt
# generate data
x = np.random.normal(0.5, 0.1, 1000)
y = np.random.normal(0.1, 0.5, 1000)
hist = plt.hist2d(x,y, bins=100)
plt.colorbar(aspect=20)
plt.colorbar(aspect=50)
Upvotes: 42