0

I need to show a histogram of a dataframe variable in Python, I need to show a histogram of how many people have a music album. I did this:

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()

My problem is that the axes are not correct. I must show albumes in the x axes, but I got and error

Heikura
  • 1,009
  • 3
  • 13
  • 27
Alexa
  • 99
  • 1
  • 6

2 Answers2

1

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

There are some tools i can advice you:

  • Matplotlib, Pandas, Seaborn, NumPy...

You can actually also use Python itself without some tools or modules.

I would advice you to use matplotlib because it is the easiest and easier than pure python

Summary I actually don´t know if you know what matplotlib is but if not i will tell you

Matplotlib is a framework for Python where you can draw histograms, curves, Diagrams. It is actually used for data visualisation For more go to this website: https://matplotlib.org/ And for histograms : https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.hist.html

Gregory
  • 177
  • 6
  • I actually think seaborn is pretty harder than matplotlib and pandas . if i were you i would use one of both to make that histogram you want . After some hours i will come back and tell you how to do that with seaborn i have to go somewhere – Gregory Aug 03 '19 at 10:39
0

IIUC, I think you are looking for something like this and you can achieve this using a bar plot using pandas and matplotlib itself, without 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()

Output:

enter image description here

Ankur Sinha
  • 6,473
  • 7
  • 42
  • 73
  • Thank you. The problem is that I must use seaborn, so I tried this: import matplotlib.pyplot as plt import pandas as pd import seaborn as sns sns.barplot(x='title', y='have', data=agrupa.reset_index()) and now I got this error: 153 if isinstance(input, string_types): 154 err = "Could not interpret input '{}'".format(input) --> 155 raise ValueError(err) 156 157 # Figure out the plotting orientation ValueError: Could not interpret input 'title' – Alexa Aug 04 '19 at 16:05