Customize / set the order X axis graph bars matplotlib

0

I generated a chart, with values for each year, from 2011 to 2015. but the generation of the graph, on the X-axis, the years are grouped according to the values, are not in the correct sequence (2011, 2012, 2013, 2014, 2015). What command can I use to fix, determine that the X-axis is ordered based on the order of years and not on the values of each?

The code looks like this:

#Já que cada linha do dataframe é uma ocorrência registrada, então quais os dias que tiveram a maior ocorrência?
semana = {'Friday': 'Sexta-feira', 'Wednesday': 'Quarta-feira', 'Tuesday': 'Terça-feira', 'Thursday': 'Quinta-feira', 'Monday': 'Segunda-feira', 'Saturday': 'Sábado', 'Sunday': 'Domingo'}
dfbh['dia_da_semana'].replace(semana, inplace=True)
dfbh['dia_da_semana'].value_counts()

The result of value_counts is:

  

Friday 16022
  Wednesday 14526
  Tuesday 14479
  Thursday 14354
  Monday 14255
  Saturday 13883
  Sunday 10620
  Name: day_day, dtype: int64

Following is plotted with the following code:

dfbh['dia_da_semana'].value_counts().plot(kind = 'bar', figsize=(10, 5), fontsize = 10, color=['b', 'r', 'b', 'r', 'b', 'r', 'b']);

And the graph is generated in the above sequence. I want to change to the normal sequence of the week. Maybe a sort_index() ??

    
asked by anonymous 07.06.2018 / 04:36

2 answers

0

There are some ways to resolve this, as you said, a sort_index() might work, but that depends on how flexible the sorting algorithm is.

Below is a solution with the use of an extra dataframe and a merge :

import pandas as pd
import matplotlib.pyplot as plt
df=pd.DataFrame({'day':['Mon', 'Tues', 'Fri','Weds', 'Sun','Thurs', 'Sat'],   
'cc':[34,65,23,66,23,51,22]})
Out[5]: 
   cc    day
0  34    Mon
1  65   Tues
2  23    Fri
3  66   Weds
4  23    Sun
5  51  Thurs
6  22    Sat


df2 = pd.DataFrame({
    'day': ['Mon', 'Tues', 'Weds', 'Thurs', 'Fri', 'Sat', 'Sun'],
    'num': [0, 1, 2, 3, 4, 5, 6]})
df = pd.merge(df, df2, on='day')
df

Out: 
   cc    day  num
0  34    Mon    0
1  65   Tues    1
2  23    Fri    4
3  66   Weds    2
4  23    Sun    6
5  51  Thurs    3
6  22    Sat    5


df = df.sort_values('num')
df.plot(kind='bar', x='day')

and this gives the graph below

Seehereitplotsnumaswell,butthisiseasytosolveusinganappropriateplot,straightfrommatplotlib.

Asasidenote,Iusedoneofthemethodsdescribed in this OS query . The other procedures described there are also interesting.

    
08.06.2018 / 13:29
0

Solved! Thanks a lot for the help. How did I solve it? using loc [] I created a variable before:

dias_ordenados = ['Segunda-feira', 'Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sábado', 'Domingo']

Then, in the graphic code that looked like this:

dfbh['dia_da_semana'].value_counts().plot(kind = 'bar', figsize=(10, 5), fontsize = 10, color=['b', 'r', 'b', 'r', 'b', 'r', 'b']);

I split the code and created another variable:

dias2 = dfbh['dia da_semana'].value_counts()

And there I made the code of the graph with the loc [], like this:

dias2.loc[dias_ordenados].plot(kind = 'bar', figsize=(10, 5), fontsize = 10, color=['b', 'r', 'b', 'r', 'b', 'r', 'b']);

And the graph, which before was being sorted by values obtained by value_counts, was sorted by the variable I created and called with loc [].

    
11.06.2018 / 03:46