Henrique
Henrique

Reputation: 65

How to generate a bar chart with data from a csv?

I have a csv with several columns, one of them is the city column. There are several cities and also the same city, repeated several times. I would like to set up a bar chart with how many cities appear in CSV. Example:

Y   X
5   Belo Horizonte
1   Vespasiano
4   São Paulo

I made the following code, but I have gotten error, which is right after the code.

Code:

import matplotlib.pyplot as plt; plt.rcdefaults()
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

#lendo o arquivo
tb_usuarios = 'tb_usuarios.csv'
usuarios = pd.read_csv(tb_usuarios,
header=0,
index_col=False
)
print(usuarios.head())
usuarios["vc_municipio"] = usuarios["vc_municipio"].dropna()
usuarios["vc_municipio"] = usuarios["vc_municipio"].str.upper()
municipio = usuarios.groupby(['vc_municipio'])
print(municipio)
y_pos = usuarios.groupby(['vc_municipio'])['vc_municipio'].count()
print(y_pos)

plt.bar(y_pos, municipio, align='center', alpha=0.5)
plt.xticks(y_pos, municipio)
plt.ylabel('Qtd')
plt.title('Municipio')

plt.show()

Error:

Traceback (most recent call last):
  File "C:/Users/Henrique Mendes/PycharmProjects/emprestimo/venv1/emprestimo.py", line 20, in <module>
    plt.bar(y_pos, municipio, align='center', alpha=0.5)
  File "C:\Users\Henrique Mendes\PycharmProjects\emprestimo\venv1\lib\site-packages\matplotlib\pyplot.py", line 2440, in bar
    **({"data": data} if data is not None else {}), **kwargs)
  File "C:\Users\Henrique Mendes\PycharmProjects\emprestimo\venv1\lib\site-packages\matplotlib\__init__.py", line 1601, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "C:\Users\Henrique Mendes\PycharmProjects\emprestimo\venv1\lib\site-packages\matplotlib\axes\_axes.py", line 2348, in bar
    self._process_unit_info(xdata=x, ydata=height, kwargs=kwargs)
  File "C:\Users\Henrique Mendes\PycharmProjects\emprestimo\venv1\lib\site-packages\matplotlib\axes\_base.py", line 2126, in _process_unit_info
    kwargs = _process_single_axis(ydata, self.yaxis, 'yunits', kwargs)
  File "C:\Users\Henrique Mendes\PycharmProjects\emprestimo\venv1\lib\site-packages\matplotlib\axes\_base.py", line 2108, in _process_single_axis
    axis.update_units(data)
  File "C:\Users\Henrique Mendes\PycharmProjects\emprestimo\venv1\lib\site-packages\matplotlib\axis.py", line 1493, in update_units
    default = self.converter.default_units(data, self)
  File "C:\Users\Henrique Mendes\PycharmProjects\emprestimo\venv1\lib\site-packages\matplotlib\category.py", line 115, in default_units
    axis.set_units(UnitData(data))
  File "C:\Users\Henrique Mendes\PycharmProjects\emprestimo\venv1\lib\site-packages\matplotlib\category.py", line 181, in __init__
    self.update(data)
  File "C:\Users\Henrique Mendes\PycharmProjects\emprestimo\venv1\lib\site-packages\matplotlib\category.py", line 215, in update
    for val in OrderedDict.fromkeys(data):
TypeError: unhashable type: 'numpy.ndarray'

My outputs:

"C:\Users\Henrique Mendes\PycharmProjects\emprestimo\venv1\Scripts\python.exe" "C:/Users/Henrique Mendes/PycharmProjects/emprestimo/venv1/emprestimo.py"
   pr_usuario  bl_administrador dt_nascimento  ... dt_cheque es_anexo dt_anexo
0           2                 0    24/02/1980  ...       NaN      NaN      NaN
1           3                 0    05/09/1985  ...       NaN      NaN      NaN
2           4                 1    20/03/1984  ...       NaN      NaN      NaN
3           5                 1    20/01/1982  ...       NaN      NaN      NaN
4           6                 0    25/05/1985  ...       NaN      NaN      NaN

[5 rows x 30 columns]
{'BELO HORIZONTE': Int64Index([0, 1, 2, 3, 6, 9, 10, 14, 17, 20, 22, 25], dtype='int64'), 'BRASILIA': Int64Index([4], dtype='int64'), 'CONTAGEM': Int64Index([23], dtype='int64'), 'CURITIBA': Int64Index([5, 7, 15, 18, 19], dtype='int64'), 'SANTA LUZIA': Int64Index([21], dtype='int64'), 'VESPASIANO': Int64Index([24], dtype='int64')}
vc_municipio
BELO HORIZONTE    12
BRASILIA           1
CONTAGEM           1
CURITIBA           5
SANTA LUZIA        1
VESPASIANO         1
Name: vc_municipio, dtype: int64

How can I do this chart?

Upvotes: 0

Views: 1050

Answers (2)

Trenton McKinney
Trenton McKinney

Reputation: 62513

Use pandas:

Your data:

  • assuming your data is in a .csv with the following form
0.0,BELO HORIZONTE
1.0,BELO HORIZONTE
2.0,BELO HORIZONTE
3.0,BELO HORIZONTE
6.0,BELO HORIZONTE
9.0,BELO HORIZONTE
10.0,BELO HORIZONTE
14.0,BELO HORIZONTE
17.0,BELO HORIZONTE
20.0,BELO HORIZONTE
22.0,BELO HORIZONTE
25.0,BELO HORIZONTE
4.0,BRASILIA
23.0,CONTAGEM
5.0,CURITIBA
7.0,CURITIBA
15.0,CURITIBA
18.0,CURITIBA
19.0,CURITIBA
21.0,SANTA LUZIA
24.0,VESPASIANO

Create the dataframe:

import pandas as pd
import matplotlib.pyplot as plt


df = pd.read_csv('test.csv', header=None)
df.columns = ['value', 'city']

    value            city
0     0.0  BELO HORIZONTE
1     1.0  BELO HORIZONTE
2     2.0  BELO HORIZONTE
3     3.0  BELO HORIZONTE
4     6.0  BELO HORIZONTE
5     9.0  BELO HORIZONTE
6    10.0  BELO HORIZONTE
7    14.0  BELO HORIZONTE
8    17.0  BELO HORIZONTE
9    20.0  BELO HORIZONTE
10   22.0  BELO HORIZONTE
11   25.0  BELO HORIZONTE
12    4.0        BRASILIA
13   23.0        CONTAGEM
14    5.0        CURITIBA
15    7.0        CURITIBA
16   15.0        CURITIBA
17   18.0        CURITIBA
18   19.0        CURITIBA
19   21.0     SANTA LUZIA
20   24.0      VESPASIANO

Groupby and plot the data:

# groupby & count
city_count = df.groupby('city').count()

                value
city                 
BELO HORIZONTE     12
BRASILIA            1
CONTAGEM            1
CURITIBA            5
SANTA LUZIA         1
VESPASIANO          1

# plot
city_count.plot.bar()
plt.ylabel('Qtd')
plt.title('Municipio')
plt.show()

enter image description here

Plot with seaborn:

import seaborn as sns

sns.barplot(x=city_count.index, y='value', data=city_count)
plt.xticks(rotation=45)
plt.show()

enter image description here

Upvotes: 1

zglin
zglin

Reputation: 2919

municipio = usuarios.groupby(['vc_municipio']) returns a groupby object in pandas which is causing your error as matplotlib doesn't handle that.

plt.bar takes x values followed by y values (see docs).

matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)

Luckily for you, when you do a groupby in pandas it automatically consolidates x values (or categories) as indices for you.

Assuming that municipio is meant to be a list of categories (you want the count by city?) then the following should work.

Replacing your code

plt.bar(y_pos, municipio, align='center', alpha=0.5)

with

plt.bar(y_pos.index, y_pos, align='center', alpha=0.5)

Alternatively, you can use the pandas version of plt.bar (which extends matplot lib) to natively handle some of the dataframe quirks.

Upvotes: 1

Related Questions