【问题标题】:How to show the names of columns of a dataframe in the axes in a Python histogram?如何在 Python 直方图中的轴中显示数据框列的名称?
【发布时间】:2019-12-11 16:13:33
【问题描述】:

我需要在 Python 中显示数据框变量的直方图,我需要显示有多少人拥有音乐专辑的直方图。我这样做了:

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 轴上显示专辑,但我得到了错误

【问题讨论】:

  • 如果您使用的是 pandas,请查看link

标签: python dataframe matplotlib seaborn


【解决方案1】:

IIUC,我认为您正在寻找这样的东西,您可以使用 pandasmatplotlib 本身的 bar 情节来实现这一目标,而无需 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()

输出:

【讨论】:

  • 谢谢。问题是我必须使用seaborn,所以我尝试了这个: import matplotlib.pyplot as plt import pandas as pd import seaborn as sns sns.barplot(x='title', y='have', data=agrupa.reset_index( )) 现在我得到了这个错误: 153 if isinstance(input, string_types): 154 err = "Could not interpret input '{}'".format(input) --> 155 raise ValueError(err) 156 157 # 弄清楚绘图方向ValueError:无法解释输入'title'
【解决方案2】:

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

我可以给你一些建议:

  • Matplotlib、Pandas、Seaborn、NumPy...

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

我建议你使用 matplotlib,因为它比纯 python 最简单和容易

总结 我实际上不知道你是否知道 matplotlib 是什么,但如果不知道,我会告诉你

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

【讨论】:

  • 我实际上认为 seaborn 比 matplotlib 和 pandas 更难。如果我是你,我会使用两者之一来制作你想要的直方图。几个小时后我会回来告诉你如何使用 seaborn 我必须去某个地方
猜你喜欢
  • 1970-01-01
  • 2017-01-02
  • 1970-01-01
  • 2018-08-17
  • 2013-08-14
  • 1970-01-01
  • 2013-07-15
  • 2019-11-19
  • 1970-01-01
相关资源
最近更新 更多