python - python中的部分阴影直方图

标签 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

最佳答案

这相当简单:您只需使用适当的颜色覆盖条形直方图,并确保线宽 (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

留给读者的练习是对图的右侧部分执行类似的操作。

关于python - python中的部分阴影直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36525121/

相关文章:

python - matplotlib 将 y 轴减小一个因子以表示百分比频率

python - 使用 tweepy 和多个 API key 获取 Twitter 关注者

python - 使用 CountVectorizer 时如何限制 token 长度?

python - matplotlib.pyplot.hist 错误的规范属性

matlab - matlab中的快速二维直方图

python - 在Python中返回给定x和y的二维PDF的值?

python - MaxPool 上的负维度大小

python - python中的随机字节字符串

python - 检查模块中运行脚本文件的名称

r - 在 ggplot2 中添加第二个 x 轴