How to reduce WIDTH colorbar in matplotlib?

I have a plot that I made in ipython's notebook using two imported datasets and an array that I made for the x axis, but the color bar is a bit thick to my liking. Is there any way to make it more subtle?

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

enter image description here

+4
source share
2 answers

Looking for the same answer, I think I found something easier to work with compatible with the current version of Matplotlib to reduce the width of the color bar. You 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)

enter image description here

+3
source

add_axes. ( !)

cbar_ax = fig.add_axes([0.09, 0.06, 0.84, 0.02])
fig.colorbar(thplot, cax=cbar_ax, orientation="horizontal")

enter image description here

+2

Source: https://habr.com/ru/post/1613887/


All Articles