python 示例显示如何使用累积比绘制直方图。使用seaborn.FacetGrid()绘制多个直方图。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 示例显示如何使用累积比绘制直方图。使用seaborn.FacetGrid()绘制多个直方图。相关的知识,希望对你有一定的参考价值。
"""Example to show how to plot histogram with accumulate ratio.
Plot multiple histogram using seaborn.FacetGrid()
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
% matplotlib inline
sns.set(style="darkgrid", palette="muted", color_codes=True)
# Genarate toy data
np.random.seed(0)
size = 200
x1 = np.random.normal(size=size)
group = np.random.choice((0, 1), size=size)
df = pd.DataFrame({'x1': x1, 'group': group})
# Plot multiple histogram using seaborn.FacetGrid()
g = sns.FacetGrid(data=df, col='group', hue='group', size=5)
g = g.map(plt.hist, 'x1', alpha=0.7, label='Frequency')
# Add cumulative ratio to each ax in gridobject
for ax in g.axes.ravel():
# Get patches in ax
patches = ax.patches
# Calc values plot with secondary axis
# Calc x value (position to plot)
bins_part= [patch.get_xy()[0] + patch.get_width() for patch in patches]
bins = [patches[0].get_xy()[0]] + bins_part
x2 = np.convolve(bins, np.ones(2) / 2, mode="same")[1:]
# Calc cumlative ratio
n = [patch.get_height() for patch in patches]
y2 = np.add.accumulate(n) / sum(n)
# Plot secondary value
ax2 = ax.twinx()
lines = ax2.plot(x2, y2, ls='--', marker='o', color='r',
label='Cumulative ratio')
ax2.grid(visible=False)
# Add 1st y label
ax.set_ylabel('Frequency')
# Add 2nd y label
ax2.set_ylabel('Cumulative ratio')
# Add Legend
plt.legend(handles=[patches[0], lines[0]])
plt.savefig('fig_facetgrid.png', dpi=150)
plt.tight_layout()
plt.show()
以上是关于python 示例显示如何使用累积比绘制直方图。使用seaborn.FacetGrid()绘制多个直方图。的主要内容,如果未能解决你的问题,请参考以下文章
Highcharts直方图 - 使用累积列百分比绘制样条曲线
累积分布图python
使用 csv 文件作为输入在 python 中绘制直方图
如何按范围对csv文件中的列进行分组并使用python绘制直方图?
直方图的绘制 基于python-matplotlib库
Plotly:如何以 Root 样式绘制直方图,仅显示直方图的轮廓?