Separate bar chart-python [colab]
People How do I make the bars of income and expenses separate and not overlapped on the chart? Thank you for being able to help.
import pandas as pd
import matplotlib.pyplot as plt
#saldo em caixa no início do período
saldo_inicial = 20000
valores = {
'meses': ['janeiro', 'fevereiro', 'março'],
'receitas': [80000, 50000, 90000],
'gastos': [60000, 70000, 65000],
}
print(pd.DataFrame(valores))
#definindo o fluxo de caixa de cada período
fluxo_janeiro = saldo_inicial + valores['receitas'][0] - valores['gastos'][0]
fluxo_fevereiro = fluxo_janeiro + valores['receitas'][1] - valores['gastos'][1]
fluxo_marco = fluxo_fevereiro + valores['receitas'][2] - valores['gastos'][2]
print('\n')
print('>Série com o fluxo de caixa de janeiro à março:')
fluxo_de_caixa = [fluxo_janeiro, fluxo_fevereiro, fluxo_marco]
#criando um novo dicionáriouma para Series do fluxo de caixa
valores_series = {
'meses': 'fluxo de caixa',
'janeiro': fluxo_janeiro,
'fevereiro': fluxo_fevereiro,
'março': fluxo_marco,
}
#criando Series
df = pd.Series(valores_series);
print(df);
#criando gráficos
print('\n')
plt.rc('figure', figsize = (15, 8))
area = plt.figure()
g1 = plt.plot(valores['meses'], fluxo_de_caixa, label = 'Fluxo de Caixa', color = 'gray', marker='o') #plotando fluxo de caixa
g2 = plt.bar(valores['meses'], valores['receitas'], label = 'Receitas', color = 'blue') #plotando receitas
g3 = plt.bar(valores['meses'], valores['gastos'], label = 'Gastos', color = 'red') #plotando gastos
plt.title('Fluxo de caixa')
plt.legend()
plt.plot()
1 answers
You have to assign a scaling to the columns, I redone the part of your code for you to use as a reference.There are other good examples in the matplotlib library https://matplotlib.org/tutorials/index.html
# importing libraries import pandas as pd
Import matplotlib.pyplot as plt
Import numpy as np#import these additional libs
From pylab import rcParams
RcParams ['figure.figsize'] = 10.4 # Setting the chart size
# cash balance at beginning of period start_pay = 20000
Values = {
'months': ['January', 'February','March'],
'revenue': [80000, 50000, 90000],
'expenditure': [60000, 70000, 65000],
}
Print (pd.DataFrame (values))
#defining the cash flow for each period flow_january = sald_initial + values ['revenue'] [0] - values ['expenditure'] [0] february_flow = january_flow + values 'recipes' - values 'expenses' mark_flow = february_flow + values ['revenue'] [2] - values ['expenditure'] [2]
Print ('\n') print ('>series with cash flow from January to March:') box_flow = [bank_flow, bank_flow, bank_flow]
# creating a new dictionary for cash flow Series series_values = { 'months':'cash flow', 'January': flow_january, 'February': february_flow, 'March': mark_flow, }
# creating Series df = pd.Series (series_values); print (df);
Assigning to a dataframe and generating the graph-new part of the code
Date = pd.DataFrame (values)
Labels = list (data['months'])
Recipes = list (round (date ['recipes'], 2))
Expenses = list (round (data ['expenses'], 2))
X = np.arange(len (labels))
Fig, ax = plt.subplots ()
Rects1 = ax.bar (x, recipes, width=0.1, label= 'recipes')
Rects2 = ax.bar (x + 0.1, expenses, width=0.1, label= 'expenses')
G1 = plt.plot (values ['months'], box_flow, label = 'Cash Flow', color = 'gray', marker='o')
Adding labels name, title, etc.
Ax.set_title ('cash flow') ax.set_xticks (x) ax.set_xticklabels (labels) plt.xticks (x, labels, rotation= 'vertical') ax.legend ()
Plt.show ()