2015-11-03 63 views
1

我试图让三个联合情节。其中一块地块的框架相对于另一块旋转90度,并垂直于另一块的轴线。所以,我可以在这个框架上的直方图但是当我使用kde并生成数据,并使用fill叠加到hist也不会转动。旋转matplotlib的填充功能,在图

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

如何在右侧面板中旋转填充功能?

enter image description here

回答

1

可以使用axHisty轴的fill_betweenx功能来做到这一点:

axHisty.fill_betweenx(Y_plot, Y_dens, color='#AAAAFF' ,alpha=0.2) 

fill_betweenx不采取fc作为kwarg,但确实需要color

我修改了scatter_hist.py例如,从matplotlib画廊有直方图,并在相同的风格罢了作为你的情节,以及所使用的fill_betweenx线之上,创建这个情节:

enter image description here

+0

非常感谢!你是如何隐藏顶部和右侧面板的蜱? – Dalek

+0

我没有 - 从我链接的例子中自动发生。但是:'axHistx.set_xticklabels([])'和'axHisty.set_yticklabels([])' – tom

+0

我可以使用此命令行隐藏蜱:在axHistx.get_yticklabels()'为TY:你可以删除标签 ty.set_visible(False)',但顶部面板('1e-14')左上角的刻度仍然存在。我不知道如何摆脱它。你有什么建议吗? – Dalek