带有Matplotlib/Seaborn的Barplot/直方图
但是,我正在尝试获得一个简单的barplot/直方图,但是,输出是空的:
df = pd.DataFrame()
df["Subject_Age"] = X["Subject.Age"]
bins = [0, 15, 25, 35, 45, 55, 65, 75, 100]
labels = ['0 - 15', '16 - 25', '26 - 35', '36 - 45', '46 - 55', '56 - 65', '66 - 75', '76+']
binned_values = np.histogram(df['Subject_Age'], bins=bins)[0].tolist()
df_hist = pd.DataFrame.from_dict(dict(zip(labels, binned_values)), orient='index').reset_index()
binned_values = np.histogram(df['Subject_Age'], bins=bins)[0].tolist()
df_hist = pd.DataFrame.from_dict(dict(zip(labels, binned_values)), orient='index').reset_index()
df_hist.columns = ['Age range', 'Counts']
sn.barplot(data = df, x = 'Age range', y = 'Counts', palette = 'rocket',
ci = 'sd',
order = ['0-15', '16-25', '26-35', '36-45', '46-55', '56-65', '66-75', '76+']);
它会导致一个空的barplot:
预先感谢您的输入!
I am trying to achieve a simple barplot/histogram, however, the output is empty:
df = pd.DataFrame()
df["Subject_Age"] = X["Subject.Age"]
bins = [0, 15, 25, 35, 45, 55, 65, 75, 100]
labels = ['0 - 15', '16 - 25', '26 - 35', '36 - 45', '46 - 55', '56 - 65', '66 - 75', '76+']
binned_values = np.histogram(df['Subject_Age'], bins=bins)[0].tolist()
df_hist = pd.DataFrame.from_dict(dict(zip(labels, binned_values)), orient='index').reset_index()
binned_values = np.histogram(df['Subject_Age'], bins=bins)[0].tolist()
df_hist = pd.DataFrame.from_dict(dict(zip(labels, binned_values)), orient='index').reset_index()
df_hist.columns = ['Age range', 'Counts']
sn.barplot(data = df, x = 'Age range', y = 'Counts', palette = 'rocket',
ci = 'sd',
order = ['0-15', '16-25', '26-35', '36-45', '46-55', '56-65', '66-75', '76+']);
Which results in an empty barplot:
Thanks in advance for your input!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这个:
Try this one: