gpt4 book ai didi

python - 在图中旋转matplotlib的填充函数

转载 作者:行者123 更新时间:2023-12-01 04:23:00 25 4
gpt4 key购买 nike

我正在尝试制作一个三联合图。其中一个图的框架相对于另一个图旋转 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')

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

enter image description here

最佳答案

您可以使用 fill_betweenx axHisty的功能轴来执行此操作:

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

关于python - 在图中旋转matplotlib的填充函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33501855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com