Den*_*gin 3 python matplotlib histogram
有一个使用 DataFrame 作为数据源渲染的直方图:
import seaborn as sns
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (14,14)
df['rawValue'].hist(bins=100)
plt.show()
Run Code Online (Sandbox Code Playgroud)
Q.如何在直方图前面添加平滑曲线?(曲线与直方图共享相同的数据源)?
PS只是对想要的内容的描述(纯粹是示意性的):
据我所知,最常见的方法是使用核密度估计。您可以在此处和此处阅读有关如何在 Python 中实现它的信息。以下是如何使用pandas和seaborn在直方图上绘制 KDE 的几个示例:
导入包并为两个示例创建示例数据集
import numpy as np # v 1.19.2
import pandas as pd # v 1.1.3
import matplotlib.pyplot as plt # v 3.3.2
import seaborn as sns # v 0.11.0
# Create sample dataset
rng = np.random.default_rng(seed=123) # random number generator
df = pd.DataFrame(dict(variable = rng.normal(size=1000)))
Run Code Online (Sandbox Code Playgroud)
熊猫
# Plot pandas histogram from dataframe with df.plot.hist (not df.hist)
ax = df['variable'].plot.hist(bins=20, density=True, edgecolor='w', linewidth=0.5)
# Save default x-axis limits for final formatting because the pandas kde
# plot uses much wider limits which usually decreases readability
xlim = ax.get_xlim()
# Plot pandas KDE
df['variable'].plot.density(color='k', alpha=0.5, ax=ax) # same as df['var'].plot.kde()
# Reset x-axis limits and edit legend and add title
ax.set_xlim(xlim)
ax.legend(labels=['KDE'], frameon=False)
ax.set_title('Pandas histogram overlaid with KDE', fontsize=14, pad=15)
plt.show()
Run Code Online (Sandbox Code Playgroud)
西博恩
# Plot seaborn histogram overlaid with KDE
ax = sns.histplot(data=df['variable'], bins=20, stat='density', alpha= 1, kde=True,
edgecolor='white', linewidth=0.5,
line_kws=dict(color='black', alpha=0.5, linewidth=1.5, label='KDE'))
ax.get_lines()[0].set_color('black') # edit line color due to bug in sns v 0.11.0
# Edit legemd and add title
ax.legend(frameon=False)
ax.set_title('Seaborn histogram overlaid with KDE', fontsize=14, pad=15)
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10056 次 |
| 最近记录: |