如何在Python直方图的轴中显示dataframe列的名称?

2024-09-29 22:23:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在Python中显示dataframe变量的直方图,我需要显示有多少人拥有一张音乐专辑的直方图。我做到了:

import seaborn as sns
import matplotlib.pyplot as plt
sns.set()
_ = plt.hist(agrupa['have'])
_ = plt.xlabel('Albumes')
_ = plt.ylabel('Número de álbumes que tienen los usuarios')
plt.show()

我的问题是轴不对。我必须在x轴上显示相册,但我得到了一个错误


Tags: importdataframe音乐matplotlibhaveaspltseaborn
2条回答

IIUC,我认为你正在寻找这样的东西,你可以通过bar绘图,使用pandasmatplotlib本身,而不需要seaborn:

import matplotlib.pyplot as plt 
import pandas as pd

plt.figure()
df = pd.DataFrame()

## SAMPLE DATAFRAME
df['Album'] = ['ARR', 'Beatles', 'Beatles', 'ARR', 'BSB', 'Coldplay', 'Rammstein', 'Coldplay', 'Rammstein', 'ARR', 'Rammstein']
df['User_ID'] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

## PLOT THE GRAPH HERE
df.Album.value_counts().plot(kind = 'bar', title = 'Count of people having different albums')
plt.xlabel('Albumes')
plt.ylabel('Número de álbumes que tienen los usuarios')
plt.show()

输出:

enter image description here

This Question can only be answered if you tell us with which tool you want to use.

我可以给你一些建议:

  • Matplotlib,熊猫,海生的,纽比。。。你知道吗

实际上,您也可以在不使用某些工具或模块的情况下使用Python本身。你知道吗

我建议您使用matplotlib,因为它比纯python更简单

摘要 我不知道你是否知道matplotlib是什么,如果不知道我会告诉你

Matplotlib是Python的一个框架,您可以在其中绘制直方图、曲线和图表。 它实际上用于数据可视化 有关更多信息,请访问此网站:https://matplotlib.org/ 对于直方图:https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.hist.html

相关问题 更多 >

    热门问题