DonkeyKong
DonkeyKong

Reputation: 476

why are my colorbars out of the image's range?

I use the following code to make a scatter plot with an additional contour-plot in its foreground. I am trying to create colorbars for both plots (one at the bottom and one at the right side), but they are out of the picture's range (the right one also, as one can see by its cut off lables). Does anyone have an idea what the problem could be?

Thanks in advance

import matplotlib as mpl

params = {
    'figure.figsize'    : [5.0, 4.0],
    'legend.fontsize'   : 12,
    'text.usetex'       : True,
    'xtick.major.size'  : 6,
    'xtick.minor.size'  : 4,
    'ytick.major.size'  : 6,
    'ytick.minor.size'  : 4
}

mpl.rcParams.update(params)
mpl.rcParams.update({'figure.autolayout': True})

import matplotlib.pyplot as plt
import numpy as np
import pylab as pl
import math
import scipy.interpolate
import os          
from mpl_toolkits.axes_grid1 import make_axes_locatable
from glob import glob 

xax = r'$\mu $'
yax = r'$\nu $'

a  = np.genfromtxt(r'data.dat', usecols = (0), unpack=True)
b   = np.genfromtxt(r'data.dat', usecols = (1), unpack=True)
r = np.genfromtxt(r'data.dat', usecols = (4), unpack=True)
p    = np.genfromtxt(r'data.dat', usecols = (5), unpack=True)

N = 100 #number of points for plotting/interpolation

a_new  = np.linspace(-22.0, 22.0, N)
b_new   = np.linspace(-22.0, 22.0, N)
r_new = scipy.interpolate.griddata( (a, b), r,\
                                         (a_new[None,:], b_new[:,None]), method='cubic')
p_new = scipy.interpolate.griddata( (a, b), p,\
                                         (a_new[None,:], b_new[:,None]), method='cubic')
fig = plt.figure()
ax = plt.gca()
CS = plt.contour(a_new, b_new, r_new, zorder=+1)

p_scat = ax.scatter(a, b, marker='.', s=7, linewidths=0, c=p, cmap= \
          plt.get_cmap('jet'), zorder=-1)

pl.xlim([-22.0, 22.0])
pl.ylim([-22.0, 22.0])
plt.xlabel(xax)
plt.ylabel(yax)


cax_h = fig.add_axes([0.05, 0.0, 0.85, 0.05]) # [left, bottom, width, height]
cax_v = fig.add_axes([0.92, 0.0, 0.05, 0.80])
colorbar_contour = plt.colorbar(CS,ticks=contour_values_r, cax=cax_h, orientation='horizontal')
colorbar_scatter = plt.colorbar(p_scat, ticks=ticks_phi, cax=cax_v, orientation='vertical')
colorbar_scatter.set_ticklabels(labels_ticks_p, update_ticks=True)
plt.show()

unfortunately doing this the colorbars are are leaving the picture's range (see picture).

enter image description here

Upvotes: 0

Views: 63

Answers (1)

Bart
Bart

Reputation: 10298

I don't know where you got the coordinates for cax_h and cax_v, but for these kind of figures I often find it useful to specify the colorbar location relative to the axes of the figure. You can get the coordinates using ax.get_position(), and use that to position the colorbar. For example:

import numpy as np
import matplotlib.pylab as pl

a = np.random.random((10,10))
b = np.random.random((10,10))
c = np.random.random((10,10))
x = np.linspace(0,1,10)
y = np.linspace(0,1,10)

fig = pl.figure()

# Adjust the axis position, to create sufficient room for two colorbars:
#                   L    B    R    T    ws  hs
fig.subplots_adjust(0.09,0.19,0.87,0.94,0.04,0.3)

# Dummy contour and scatter plots
ct  = pl.contourf(x, y, b)
sc  = pl.scatter(a, b, c=c)

# Get current ax and its position:
ax = pl.gca()
axp = ax.get_position()

# Set the colorbar axes relative to figure ax
cax_h = fig.add_axes([axp.x0, axp.y0-0.1, axp.x1-axp.x0, 0.02])
cax_v = fig.add_axes([axp.x1+0.02, axp.y0, 0.02, axp.y1-axp.y0])

pl.colorbar(ct, cax=cax_v, orientation='vertical')
pl.colorbar(sc, cax=cax_h, orientation='horizontal')

enter image description here

Upvotes: 2

Related Questions