Python graph does not show all values on the x-axis

Asked

Viewed 531 times

0

I used the pandas library to read a csv file and create a graph using matplotlib:

import pandas as pd
import matplotlib.pyplot as plt

brazil_dataset = pd.read_csv('/content/states.csv')

fig, ax = plt.subplots()
brazil_dataset.plot('UF', 'Population', ax=ax)
plt.show()

inserir a descrição da imagem aqui

However, not all states appear on the x-axis of the graph and the values of y are simplified. I would like to know how to generate the full graph.

  • Can share the dataset?

  • Already tried to change the size of the picture, to see if it appears all the data?

  • Unfortunately I’m not able to send the dataset link. Regarding changing the size; I have tried and it still didn’t work, it keeps hiding the information.

  • Editing the question you can’t put the link?

1 answer

0

Imports:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

Estados Brasileiros:

estados = np.array(['AC','AL','AP','AM','BA','CE',
                    'DF','ES','GO','MA','MT','MS',
                    'MG','PA','PB','PR','PE','PI',
                    'RJ','RN','RS','RO','RR','SC',
                    'SP','SE','TO'])

Fictitious data:

populacao = np.random.randint(1000, size = 27)

Creating the dataset:

brazil_dataset = pd.DataFrame({'UF': estados, 'Population' : populacao})

Plotting:

fig, ax = plt.subplots()
brazil_dataset.plot('UF', 'Population', ax = ax)
ax.set_xticks(range(len(brazil_dataset['UF'])))
ax.set_xticklabels(brazil_dataset['UF'])
plt.xticks(rotation = 45)
plt.show()

In ax.set_xticks Set how many Abels (labels) will exist on the chart. In ax.set_xticklabels we set the Flags(labels), the strings.

You can also plot without being directly by dataframe:

plt.figure(figsize=(14,10))
plt.plot('UF','Population', data = brazil_dataset)
plt.xticks(rotation = 45)
plt.show()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.