简体   繁体   English

在图中旋转matplotlib的填充功能

[英]rotate the fill function of matplotlib in a figure

I am trying to make a three joint plot. 我正在尝试制作三个联合情节。 The frame of one of the plots is rotated by 90 degrees with respect to the other and perpendicular to the axis of the other. 其中一个图的框架相对于另一个图旋转90度,并垂直于另一个图的轴。 So I can make a histogram plot in this frame but when I use kde and generate data and use fill to overlay to the hist it won't rotate. 因此,我可以在此帧中绘制直方图,但是当我使用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')

How can I rotate the fill function in right panel? 如何旋转右侧面板中的填充功能?

在此处输入图片说明

You can use the fill_betweenx function of the axHisty axes to do this: 您可以使用axHisty轴的fill_betweenx函数执行此操作:

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

Note the fill_betweenx doesn't take fc as a kwarg , but does take color . 请注意fill_betweenx不会将fc视为kwarg ,但kwarg color作为color

I modified the scatter_hist.py example from the matplotlib gallery to have histograms and fills in the same style as your plot, and used the fill_betweenx line above, to create this plot: 我从matplotlib画廊修改了scatter_hist.py示例,使其具有直方图并以与绘图相同的样式进行填充,并使用上面的fill_betweenx行创建了该绘图:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM