如何在pandas groupby直方图中显示标签名称 [英] How to show label names in pandas groupby histogram plot
本文介绍了如何在pandas groupby直方图中显示标签名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用 pandas 在单个图中绘制多个直方图,但缺少一些东西:
I can plot multiple histograms in a single plot using pandas but there are few things missing:
- 如何给标签.
- 我只能绘制一个图形,如何将其更改为 layout=(3,1) 或其他内容.
- 此外,在图 1 中,所有垃圾箱都填充了纯色,很难知道哪个是哪个,如何用不同的标记(例如十字、斜线等)填充?
这是 MWE:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('iris')
df.groupby('species')['sepal_length'].hist(alpha=0.7,label='species')
plt.legend()
输出:
如何赋予不同的颜色?
df.hist('sepal_length',by='species',layout=(3,1))
plt.tight_layout()
给出:
推荐答案
您可以解析为groupby
:
fig,ax = plt.subplots()
hatches = ('\\', '//', '..') # fill pattern
for (i, d),hatch in zip(df.groupby('species'), hatches):
d['sepal_length'].hist(alpha=0.7, ax=ax, label=i, hatch=hatch)
ax.legend()
输出:
这篇关于如何在pandas groupby直方图中显示标签名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文