带有Matplotlib/Seaborn的Barplot/直方图

发布于 2025-02-02 14:14:50 字数 977 浏览 6 评论 0原文

但是,我正在尝试获得一个简单的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:

Output

Thanks in advance for your input!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裂开嘴轻声笑有多痛 2025-02-09 14:14:50

尝试这个:

import pandas as pd

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+']

df = pd.DataFrame()
# Data example
df['Age'] = [5, 10, 12, 18, 23, 26, 27, 28, 36, 46, 47, 49, 50, 55, 56, 57, 58, 59, 60, 67, 67, 80, 89, 89, 88, 91]

df_count = pd.cut(df['Age'], bins=bins, labels=labels).value_counts(sort=False)
df_count.plot.bar(y='Age', use_index=True).figure.show()

Try this one:

import pandas as pd

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+']

df = pd.DataFrame()
# Data example
df['Age'] = [5, 10, 12, 18, 23, 26, 27, 28, 36, 46, 47, 49, 50, 55, 56, 57, 58, 59, 60, 67, 67, 80, 89, 89, 88, 91]

df_count = pd.cut(df['Age'], bins=bins, labels=labels).value_counts(sort=False)
df_count.plot.bar(y='Age', use_index=True).figure.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文