簡體   English   中英

在圖中旋轉matplotlib的填充功能

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

我正在嘗試制作三個聯合情節。 其中一個圖的框架相對於另一個圖旋轉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')

如何旋轉右側面板中的填充功能?

在此處輸入圖片說明

您可以使用axHisty軸的fill_betweenx函數執行此操作:

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

請注意fill_betweenx不會將fc視為kwarg ,但kwarg color作為color

我從matplotlib畫廊修改了scatter_hist.py示例,使其具有直方圖並以與繪圖相同的樣式進行填充,並使用上面的fill_betweenx行創建了該繪圖:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM