我同时拥有散点图和前景中的附加等高线图。对于两者,我想绘制主图旁边的相应颜色条。不幸的是,我无法强制执行适当大小的颜色栏。我试过了
divider = make_axes_locatable(ax)
cax_r = divider.append_axes("right", size="5%", pad=0.05)
cax_hor = divider.append_axes("bottom", size="5%", pad=0.15)
但无法正确应用此功能。生成具有不期望大小的颜色栏的代码如下:
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)
#divider = make_axes_locatable(ax)
#cax_r = divider.append_axes("right", size="5%", pad=0.05)
#cax_hor = divider.append_axes("bottom", size="5%", pad=0.15)
colorbar_contour = plt.colorbar(CS, orientation='horizontal')
p_scat = ax.scatter(a, b, marker='.', s=7, linewidths=0, c=p, cmap= \
plt.get_cmap('jet'), zorder=-1)
colorbar_scatter = plt.colorbar(p_scat, orientation='vertical')
pl.xlim([-35.0, 35.0])
pl.ylim([-35.0, 35.0])
plt.xlabel(xax)
plt.ylabel(yax)
plt.show()
任何人都可以告诉我(尊重我糟糕的蟒蛇技能)如何使用' devider.append_axes()'适当地,还是解释另一种方法?
提前致谢
答案 0 :(得分:1)
最简单的方法是使用plt.axes
或fig.add_axes
显式构造轴 - 两者都接受4个参数的列表/元组,[left, bottom, width, height]
的分数数字大小(默认情况下)。
然后,在使用cax
参数构造颜色条时可以使用这些轴:
cbar_ax = fig.add_axes([...])
cbar = plt.colobar(..., cax=cbar_ax)