Skip to main content
 首页 » 编程设计

python之部分遮蔽 python 中的直方图

2024年10月24日8lori

我有一个分布直方图,因为我会用这个分布做一些切割。我想为我的削减遮蔽该区域。我知道如何遮蔽所有内容,但我不知道如何遮蔽直方图的某些部分。下面是我的代码:

f, (ax1, ax2) = plt.subplots(1,2, sharex=False, sharey=True,figsize=(20,8)) 
ax1.xaxis.set_minor_locator(minor_locator3) 
ax1.yaxis.set_minor_locator(minor_locator4) 
ax1.tick_params('both', length=10, width=2, which='major') 
ax1.tick_params('both', length=5, width=2, which='minor') 
ax1.set_xlabel(r'$\log\mathcal{L} ~[\odot]$',fontsize=30) 
ax1.set_ylabel(r'Number of galaxies',fontsize=30) 
ax1.set_yscale('log') 
ax1.set_xlim(6,13.5) 
bins = np.linspace(6,13,141) 
ax1.hist(L,bins,histtype='step') 
 
ax2.xaxis.set_minor_locator(minor_locator5) 
ax2.yaxis.set_minor_locator(minor_locator6) 
ax2.tick_params('both', length=10, width=2, which='major') 
ax2.tick_params('both', length=5, width=2, which='minor') 
ax2.set_xlabel(r'$r_e ~{\rm [Kpc]}$',fontsize=30) 
ax2.set_yscale('log') 
bins = np.linspace(0,25,251) 
ax2.hist(R50,bins,histtype='step') 
plt.subplots_adjust(wspace=0.005, left=0.15, right=0.9, top=0.95, bottom=0.05) 
plt.savefig('R50_hist.eps',bbox_inches='tight') 

剧情如下:

我想将左侧面板从 x=10 遮蔽到 x=11(右侧面板从 x=2 到 x=10)和直方图下方,任何人都可以告诉我如何处理它吗?

请您参考如下方法:

这非常简单:您只需绘制一个条形直方图,使用适当的颜色,并确保线宽 (lw) 为零。

例如(显然我没有你的数据集):

import numpy as np 
from matplotlib import pyplot as plt 
 
N = 1e5 
lum = 10**10.5 * np.random.normal(loc=1, scale=1, size=1e5) 
lum = np.log10(lum) 
dist = np.random.normal(loc=5, scale=5, size=1e5) 
 
f, (ax1, ax2) = plt.subplots(1,2, sharex=False, sharey=True,figsize=(20,8)) 
# Not bothered with the locators 
#ax1.xaxis.set_minor_locator(minor_locator3) 
#ax1.yaxis.set_minor_locator(minor_locator4) 
ax1.tick_params('both', length=10, width=2, which='major') 
ax1.tick_params('both', length=5, width=2, which='minor') 
ax1.set_xlabel(r'$\log\mathcal{L} ~[\odot]$',fontsize=30) 
ax1.set_ylabel(r'Number of galaxies',fontsize=30) 
ax1.set_yscale('log') 
ax1.set_xlim(6,13.5) 
bins = np.linspace(6,13,141) 
ax1.hist(lum,bins,histtype='step') 
# Subselect the luminosities 
mask = (lum >= 10) & (lum <= 11) 
# Overplot colored bar histogram 
ax1.hist(lum[mask],bins,histtype='bar', color='red', lw=0) 
 
#ax2.xaxis.set_minor_locator(minor_locator5) 
#ax2.yaxis.set_minor_locator(minor_locator6) 
ax2.tick_params('both', length=10, width=2, which='major') 
ax2.tick_params('both', length=5, width=2, which='minor') 
ax2.set_xlabel(r'$r_e ~{\rm [Kpc]}$',fontsize=30) 
ax2.set_yscale('log') 
bins = np.linspace(0,25,251) 
ax2.hist(dist,bins,histtype='step') 
 
plt.subplots_adjust(wspace=0.005, left=0.15, right=0.9, top=0.95, bottom=0.05) 
plt.savefig('R50_hist.png',bbox_inches='tight') 

留给读者作为练习,对图中的右侧部分做类似的事情。