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()
Use the matpltolib. pyplot. clim() Function to Set the Range of Colorbar in Matplotlib. The clim() function can be used to control the range of the colorbar by setting the color limits of the plot, which are used for scaling.
The colorbar() function in pyplot module of matplotlib adds a colorbar to a plot indicating the color scale.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With