部分遮蔽python中的直方图

时间:2016-04-10 02:38:33

标签: python histogram

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

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)并且在直方图下方,任何人都可以告诉我如何接近它?

enter image description here

1 个答案:

答案 0 :(得分:1)

它非常简单:你只需用适当的颜色绘制条形直方图,并确保线宽(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')

enter image description here

它留给读者做练习,为图的右边部分做类似的事情。