在figu中旋转matplotlib的fill函数

2024-06-29 01:12:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我在试着做一个三人合谋。其中一个绘图框相对于另一个旋转90度,并垂直于另一个绘图的轴。所以我可以在这个帧中绘制一个直方图图,但是当我使用kde生成数据并使用fill覆盖到{}时,它不会旋转。在

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


Tags: theimportplotnpgaussiankernelxminymax
1条回答
网友
1楼 · 发布于 2024-06-29 01:12:03

您可以使用axHisty轴的^{}函数来执行此操作:

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

注意fill_betweenx不将fc作为kwarg,而是取{}。在

我修改了matplotlib库中的^{}示例,使其具有与绘图相同的柱状图和填充样式,并使用上面的fill_betweenx行来创建此绘图:

enter image description here

相关问题 更多 >