【发布时间】:2020-11-23 13:29:58
【问题描述】:
我在 Jupyter 笔记本中创建了一个直方图,以显示 100 次网络访问的页面时间分布(以秒为单位)。
代码如下:
ax = df.hist(column='time_on_page', bins=25, grid=False, figsize=(12,8), color='#86bf91', zorder=2, rwidth=0.9)
ax = ax[0]
for x in ax:
# Despine
x.spines['right'].set_visible(False)
x.spines['top'].set_visible(False)
x.spines['left'].set_visible(False)
# Switch off ticks
x.tick_params(axis="both", which="both", bottom="off", top="off", labelbottom="on", left="off", right="off", labelleft="on")
# Draw horizontal axis lines
vals = x.get_yticks()
for tick in vals:
x.axhline(y=tick, linestyle='dashed', alpha=0.4, color='#eeeeee', zorder=1)
# Set title
x.set_title("Time on Page Histogram", fontsize=20, weight='bold', size=12)
# Set x-axis label
x.set_xlabel("Time on Page Duration (Seconds)", labelpad=20, weight='bold', size=12)
# Set y-axis label
x.set_ylabel("Page Views", labelpad=20, weight='bold', size=12)
# Format y-axis label
x.yaxis.set_major_formatter(StrMethodFormatter('{x:,g}'))
这会产生以下可视化效果:
我通常对外观感到满意,但我希望轴更具描述性,可能会显示每个 bin 的 bin 范围以及每个 bin 占总数的百分比。
已在 Matplotlib 文档中查找此内容,但似乎找不到任何可以让我实现最终目标的内容。
非常感谢任何帮助。
【问题讨论】:
标签: python matplotlib jupyter-notebook histogram distribution