我正在尝试制作三个联合情节。其中一个图的框架相对于另一个旋转90度并垂直于另一个的轴。所以我可以在此框架中制作直方图,但是当我使用kde
并生成数据并使用fill
覆盖到hist
时,它将不会旋转。
import pylab as plt
import seaborn as sns
from scipy.stats import gaussian_kde
import numpy as np
from astroML.plotting import hist
from mpl_toolkits.axes_grid1 import make_axes_locatable
sns.set_style("ticks")
axScatter = plt.subplot(111)
xmin, xmax = x.min(), x.max()
ymin, ymax = y.min(), y.max()
# Peform the kernel density estimate
xx, yy = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([xx.ravel(), yy.ravel()])
values = np.vstack([x, y])
kernel = gaussian_kde(values)
f = np.reshape(kernel(positions).T, xx.shape)
axScatter.set_xlim(xmin, xmax)
axScatter.set_ylim(ymin, ymax)
# Contourf plot
cfset = axScatter.contourf(xx, yy, f, cmap='Blues')
## Or kernel density estimate plot instead of the contourf plot
#ax.imshow(np.rot90(f), cmap='Blues', extent=[xmin, xmax, ymin, ymax])
# Contour plot
cset = axScatter.contour(xx, yy, f, colors='k')
# Label plot
axScatter.scatter(x, y, marker='o', s=1, alpha=0.2, color='k')
axScatter.set_aspect('auto')
axScatter.set_xlabel(r'$X$')
axScatter.set_ylabel(r'$Y$')
# create new axes on the right and on the top of the current axes.
divider = make_axes_locatable(axScatter)
axHistx = divider.append_axes("top", size=1.2, pad=0.1, sharex=axScatter)
axHisty = divider.append_axes("right", size=1.2, pad=0.1, sharey=axScatter)
# the scatter plot:
# histograms
kde = gaussian_kde(x)
X_plot = np.linspace(xmin, xmax, 1000)
X_dens = kde.evaluate(X_plot)
axHistx.fill(X_plot, X_dens, fc='#AAAAFF',alpha=0.2)
hist(x, bins='knuth', ax=axHistx, color='black', histtype='step', normed=True)
kde = gaussian_kde(y)
Y_plot = np.linspace(ymin,ymax, 1000)
Y_dens = kde.evaluate(Y_plot)
axHisty.fill(Y_plot, Y_dens, fc='#AAAAFF' ,alpha=0.2)
hist(y, bins='knuth', ax=axHisty, color='black', histtype='step', normed=True, orientation='horizontal')
如何在右侧面板中旋转填充功能?
答案 0 :(得分:2)
您可以使用axHisty
轴的fill_betweenx
功能执行此操作:
axHisty.fill_betweenx(Y_plot, Y_dens, color='#AAAAFF' ,alpha=0.2)
请注意,fill_betweenx
并未将fc
作为kwarg
,而是color
。
我修改了matplotlib库中的scatter_hist.py
示例,使其具有与绘图相同的直方图和填充,并使用上面的fill_betweenx
行创建此图: