为什么我的色条超出了图像的范围?

时间:2015-12-14 16:34:07

标签: python matplotlib plot contour colorbar

我使用以下代码制作散点图,其前景中有一个额外的轮廓图。我正在尝试为两个图形创建颜色条(一个在底部,一个在右侧),但它们不在图片的范围内(正确的一个,正如人们可以看到它的剪切标签) 。有谁知道问题可能是什么?

提前致谢

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

不幸的是,这样做的颜色栏正在离开图片的范围(见图片)。

enter image description here

1 个答案:

答案 0 :(得分:2)

我不知道你在哪里得到了cax_hcax_v的坐标,但是对于这些图形,我经常发现指定相对于图形轴的颜色条位置很有用。您可以使用ax.get_position()获取坐标,并使用它来定位颜色条。例如:

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