I am trying to generate a plot with matplotlib from a dataframe. What I want to show on the graph is a correlation between the age of people and the money they earn and spend. The problem is that I can't make the graph understandable or readable.
I tried to group the information so that, for example, the ages and their respective values are not repeated, but I have not succeeded.
If you could help me to correctly generate the graph please. Thank you
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
data = [{'Genre':1,'Age':19,'annualincome':15,'annualexpenses':39},
{'Genre':1,'Age':21,'annualincome':15,'annualexpenses':81},
{'Genre':0,'Age':20,'annualincome':16,'annualexpenses':6},
{'Genre':0,'Age':23,'annualincome':16,'annualexpenses':77},
{'Genre':0,'Age':31,'annualincome':17,'annualexpenses':40},
{'Genre':0,'Age':22,'annualincome':17,'annualexpenses':76},
{'Genre':0,'Age':35,'annualincome':18,'annualexpenses':6},
{'Genre':0,'Age':23,'annualincome':18,'annualexpenses':94},
{'Genre':1,'Age':64,'annualincome':19,'annualexpenses':3},
{'Genre':0,'Age':30,'annualincome':19,'annualexpenses':72},
{'Genre':1,'Age':67,'annualincome':19,'annualexpenses':14},
{'Genre':0,'Age':35,'annualincome':19,'annualexpenses':99},
{'Genre':0,'Age':58,'annualincome':20,'annualexpenses':15},
{'Genre':0,'Age':24,'annualincome':20,'annualexpenses':77},
{'Genre':1,'Age':37,'annualincome':20,'annualexpenses':13},
{'Genre':1,'Age':22,'annualincome':20,'annualexpenses':79},
{'Genre':0,'Age':35,'annualincome':21,'annualexpenses':35},
{'Genre':1,'Age':20,'annualincome':21,'annualexpenses':66},
{'Genre':1,'Age':52,'annualincome':23,'annualexpenses':29},
{'Genre':0,'Age':35,'annualincome':23,'annualexpenses':98},
{'Genre':1,'Age':35,'annualincome':24,'annualexpenses':35},
{'Genre':1,'Age':25,'annualincome':24,'annualexpenses':73},
{'Genre':0,'Age':46,'annualincome':25,'annualexpenses':5},
{'Genre':1,'Age':31,'annualincome':25,'annualexpenses':73},
{'Genre':0,'Age':54,'annualincome':28,'annualexpenses':14},
{'Genre':1,'Age':29,'annualincome':28,'annualexpenses':82},
{'Genre':0,'Age':45,'annualincome':28,'annualexpenses':32},
{'Genre':1,'Age':35,'annualincome':28,'annualexpenses':61},
{'Genre':0,'Age':40,'annualincome':29,'annualexpenses':31},
{'Genre':0,'Age':23,'annualincome':29,'annualexpenses':87},
{'Genre':1,'Age':60,'annualincome':30,'annualexpenses':4},
{'Genre':0,'Age':21,'annualincome':30,'annualexpenses':73},
{'Genre':1,'Age':53,'annualincome':33,'annualexpenses':4},
{'Genre':1,'Age':18,'annualincome':33,'annualexpenses':92},
{'Genre':0,'Age':49,'annualincome':33,'annualexpenses':14},
{'Genre':0,'Age':21,'annualincome':33,'annualexpenses':81},
{'Genre':0,'Age':42,'annualincome':34,'annualexpenses':17},
{'Genre':0,'Age':30,'annualincome':34,'annualexpenses':73},
{'Genre':0,'Age':36,'annualincome':37,'annualexpenses':26},
{'Genre':0,'Age':20,'annualincome':37,'annualexpenses':75},
{'Genre':0,'Age':65,'annualincome':38,'annualexpenses':35},
{'Genre':1,'Age':24,'annualincome':38,'annualexpenses':92},
{'Genre':1,'Age':48,'annualincome':39,'annualexpenses':36},
{'Genre':0,'Age':31,'annualincome':39,'annualexpenses':61},
{'Genre':0,'Age':49,'annualincome':39,'annualexpenses':28},
{'Genre':0,'Age':24,'annualincome':39,'annualexpenses':65},
{'Genre':0,'Age':50,'annualincome':40,'annualexpenses':55},
{'Genre':0,'Age':27,'annualincome':40,'annualexpenses':47},
{'Genre':0,'Age':29,'annualincome':40,'annualexpenses':42},
{'Genre':0,'Age':31,'annualincome':40,'annualexpenses':42},
{'Genre':0,'Age':49,'annualincome':42,'annualexpenses':52},
{'Genre':1,'Age':33,'annualincome':42,'annualexpenses':60},
{'Genre':0,'Age':31,'annualincome':43,'annualexpenses':54},
{'Genre':1,'Age':59,'annualincome':43,'annualexpenses':60},
{'Genre':0,'Age':50,'annualincome':43,'annualexpenses':45},
{'Genre':1,'Age':47,'annualincome':43,'annualexpenses':41},
{'Genre':0,'Age':51,'annualincome':44,'annualexpenses':50},
{'Genre':1,'Age':69,'annualincome':44,'annualexpenses':46},
{'Genre':0,'Age':27,'annualincome':46,'annualexpenses':51},
{'Genre':1,'Age':53,'annualincome':46,'annualexpenses':46},
{'Genre':1,'Age':70,'annualincome':46,'annualexpenses':56},
{'Genre':1,'Age':19,'annualincome':46,'annualexpenses':55},
{'Genre':0,'Age':67,'annualincome':47,'annualexpenses':52},
{'Genre':0,'Age':54,'annualincome':47,'annualexpenses':59},
{'Genre':1,'Age':63,'annualincome':48,'annualexpenses':51},
{'Genre':1,'Age':18,'annualincome':48,'annualexpenses':59},
{'Genre':0,'Age':43,'annualincome':48,'annualexpenses':50},
{'Genre':0,'Age':68,'annualincome':48,'annualexpenses':48},
{'Genre':1,'Age':19,'annualincome':48,'annualexpenses':59},
{'Genre':0,'Age':32,'annualincome':48,'annualexpenses':47},
{'Genre':1,'Age':70,'annualincome':49,'annualexpenses':55},
{'Genre':0,'Age':47,'annualincome':49,'annualexpenses':42},
{'Genre':0,'Age':60,'annualincome':50,'annualexpenses':49},
{'Genre':0,'Age':60,'annualincome':50,'annualexpenses':56},
{'Genre':1,'Age':59,'annualincome':54,'annualexpenses':47},
{'Genre':1,'Age':26,'annualincome':54,'annualexpenses':54},
{'Genre':0,'Age':45,'annualincome':54,'annualexpenses':53},
{'Genre':1,'Age':40,'annualincome':54,'annualexpenses':48},
{'Genre':0,'Age':23,'annualincome':54,'annualexpenses':52},
{'Genre':0,'Age':49,'annualincome':54,'annualexpenses':42},
{'Genre':1,'Age':57,'annualincome':54,'annualexpenses':51},
{'Genre':1,'Age':38,'annualincome':54,'annualexpenses':55},
{'Genre':1,'Age':67,'annualincome':54,'annualexpenses':41},
{'Genre':0,'Age':46,'annualincome':54,'annualexpenses':44},
{'Genre':0,'Age':21,'annualincome':54,'annualexpenses':57},
{'Genre':1,'Age':48,'annualincome':54,'annualexpenses':46},
{'Genre':0,'Age':55,'annualincome':57,'annualexpenses':58},
{'Genre':0,'Age':22,'annualincome':57,'annualexpenses':55},
{'Genre':0,'Age':34,'annualincome':58,'annualexpenses':60},
{'Genre':0,'Age':50,'annualincome':58,'annualexpenses':46},
{'Genre':0,'Age':68,'annualincome':59,'annualexpenses':55},
{'Genre':1,'Age':18,'annualincome':59,'annualexpenses':41},
{'Genre':1,'Age':48,'annualincome':60,'annualexpenses':49},
{'Genre':0,'Age':40,'annualincome':60,'annualexpenses':40},
{'Genre':0,'Age':32,'annualincome':60,'annualexpenses':42},
{'Genre':1,'Age':24,'annualincome':60,'annualexpenses':52},
{'Genre':0,'Age':47,'annualincome':60,'annualexpenses':47},
{'Genre':0,'Age':27,'annualincome':60,'annualexpenses':50},
{'Genre':1,'Age':48,'annualincome':61,'annualexpenses':42},
{'Genre':1,'Age':20,'annualincome':61,'annualexpenses':49},
{'Genre':0,'Age':23,'annualincome':62,'annualexpenses':41},
{'Genre':0,'Age':49,'annualincome':62,'annualexpenses':48},
{'Genre':1,'Age':67,'annualincome':62,'annualexpenses':59},
{'Genre':1,'Age':26,'annualincome':62,'annualexpenses':55},
{'Genre':1,'Age':49,'annualincome':62,'annualexpenses':56},
{'Genre':0,'Age':21,'annualincome':62,'annualexpenses':42},
{'Genre':0,'Age':66,'annualincome':63,'annualexpenses':50},
{'Genre':1,'Age':54,'annualincome':63,'annualexpenses':46},
{'Genre':1,'Age':68,'annualincome':63,'annualexpenses':43},
{'Genre':1,'Age':66,'annualincome':63,'annualexpenses':48},
{'Genre':1,'Age':65,'annualincome':63,'annualexpenses':52},
{'Genre':0,'Age':19,'annualincome':63,'annualexpenses':54},
{'Genre':0,'Age':38,'annualincome':64,'annualexpenses':42},
{'Genre':1,'Age':19,'annualincome':64,'annualexpenses':46},
{'Genre':0,'Age':18,'annualincome':65,'annualexpenses':48},
{'Genre':0,'Age':19,'annualincome':65,'annualexpenses':50},
{'Genre':0,'Age':63,'annualincome':65,'annualexpenses':43},
{'Genre':0,'Age':49,'annualincome':65,'annualexpenses':59},
{'Genre':0,'Age':51,'annualincome':67,'annualexpenses':43},
{'Genre':0,'Age':50,'annualincome':67,'annualexpenses':57},
{'Genre':1,'Age':27,'annualincome':67,'annualexpenses':56},
{'Genre':0,'Age':38,'annualincome':67,'annualexpenses':40},
{'Genre':0,'Age':40,'annualincome':69,'annualexpenses':58},
{'Genre':1,'Age':39,'annualincome':69,'annualexpenses':91},
{'Genre':0,'Age':23,'annualincome':70,'annualexpenses':29},
{'Genre':0,'Age':31,'annualincome':70,'annualexpenses':77},
{'Genre':1,'Age':43,'annualincome':71,'annualexpenses':35},
{'Genre':1,'Age':40,'annualincome':71,'annualexpenses':95},
{'Genre':1,'Age':59,'annualincome':71,'annualexpenses':11},
{'Genre':1,'Age':38,'annualincome':71,'annualexpenses':75},
{'Genre':1,'Age':47,'annualincome':71,'annualexpenses':9},
{'Genre':1,'Age':39,'annualincome':71,'annualexpenses':75},
{'Genre':0,'Age':25,'annualincome':72,'annualexpenses':34},
{'Genre':0,'Age':31,'annualincome':72,'annualexpenses':71},
{'Genre':1,'Age':20,'annualincome':73,'annualexpenses':5},
{'Genre':0,'Age':29,'annualincome':73,'annualexpenses':88},
{'Genre':0,'Age':44,'annualincome':73,'annualexpenses':7},
{'Genre':1,'Age':32,'annualincome':73,'annualexpenses':73},
{'Genre':1,'Age':19,'annualincome':74,'annualexpenses':10},
{'Genre':0,'Age':35,'annualincome':74,'annualexpenses':72},
{'Genre':0,'Age':57,'annualincome':75,'annualexpenses':5},
{'Genre':1,'Age':32,'annualincome':75,'annualexpenses':93},
{'Genre':0,'Age':28,'annualincome':76,'annualexpenses':40},
{'Genre':0,'Age':32,'annualincome':76,'annualexpenses':87},
{'Genre':1,'Age':25,'annualincome':77,'annualexpenses':12},
{'Genre':1,'Age':28,'annualincome':77,'annualexpenses':97},
{'Genre':1,'Age':48,'annualincome':77,'annualexpenses':36},
{'Genre':0,'Age':32,'annualincome':77,'annualexpenses':74},
{'Genre':0,'Age':34,'annualincome':78,'annualexpenses':22},
{'Genre':1,'Age':34,'annualincome':78,'annualexpenses':90},
{'Genre':1,'Age':43,'annualincome':78,'annualexpenses':17},
{'Genre':1,'Age':39,'annualincome':78,'annualexpenses':88},
{'Genre':0,'Age':44,'annualincome':78,'annualexpenses':20},
{'Genre':0,'Age':38,'annualincome':78,'annualexpenses':76},
{'Genre':0,'Age':47,'annualincome':78,'annualexpenses':16},
{'Genre':0,'Age':27,'annualincome':78,'annualexpenses':89},
{'Genre':1,'Age':37,'annualincome':78,'annualexpenses':1},
{'Genre':0,'Age':30,'annualincome':78,'annualexpenses':78},
{'Genre':1,'Age':34,'annualincome':78,'annualexpenses':1},
{'Genre':0,'Age':30,'annualincome':78,'annualexpenses':73},
{'Genre':0,'Age':56,'annualincome':79,'annualexpenses':35},
{'Genre':0,'Age':29,'annualincome':79,'annualexpenses':83},
{'Genre':1,'Age':19,'annualincome':81,'annualexpenses':5},
{'Genre':0,'Age':31,'annualincome':81,'annualexpenses':93},
{'Genre':1,'Age':50,'annualincome':85,'annualexpenses':26},
{'Genre':0,'Age':36,'annualincome':85,'annualexpenses':75},
{'Genre':1,'Age':42,'annualincome':86,'annualexpenses':20},
{'Genre':0,'Age':33,'annualincome':86,'annualexpenses':95},
{'Genre':0,'Age':36,'annualincome':87,'annualexpenses':27},
{'Genre':1,'Age':32,'annualincome':87,'annualexpenses':63},
{'Genre':1,'Age':40,'annualincome':87,'annualexpenses':13},
{'Genre':1,'Age':28,'annualincome':87,'annualexpenses':75},
{'Genre':1,'Age':36,'annualincome':87,'annualexpenses':10},
{'Genre':1,'Age':36,'annualincome':87,'annualexpenses':92},
{'Genre':0,'Age':52,'annualincome':88,'annualexpenses':13},
{'Genre':0,'Age':30,'annualincome':88,'annualexpenses':86},
{'Genre':1,'Age':58,'annualincome':88,'annualexpenses':15},
{'Genre':1,'Age':27,'annualincome':88,'annualexpenses':69},
{'Genre':1,'Age':59,'annualincome':93,'annualexpenses':14},
{'Genre':1,'Age':35,'annualincome':93,'annualexpenses':90},
{'Genre':0,'Age':37,'annualincome':97,'annualexpenses':32},
{'Genre':0,'Age':32,'annualincome':97,'annualexpenses':86},
{'Genre':1,'Age':46,'annualincome':98,'annualexpenses':15},
{'Genre':0,'Age':29,'annualincome':98,'annualexpenses':88},
{'Genre':0,'Age':41,'annualincome':99,'annualexpenses':39},
{'Genre':1,'Age':30,'annualincome':99,'annualexpenses':97},
{'Genre':0,'Age':54,'annualincome':101,'annualexpenses':24},
{'Genre':1,'Age':28,'annualincome':101,'annualexpenses':68},
{'Genre':0,'Age':41,'annualincome':103,'annualexpenses':17},
{'Genre':0,'Age':36,'annualincome':103,'annualexpenses':85},
{'Genre':0,'Age':34,'annualincome':103,'annualexpenses':23},
{'Genre':0,'Age':32,'annualincome':103,'annualexpenses':69},
{'Genre':1,'Age':33,'annualincome':113,'annualexpenses':8},
{'Genre':0,'Age':38,'annualincome':113,'annualexpenses':91},
{'Genre':0,'Age':47,'annualincome':120,'annualexpenses':16},
{'Genre':0,'Age':35,'annualincome':120,'annualexpenses':79},
{'Genre':0,'Age':45,'annualincome':126,'annualexpenses':28},
{'Genre':1,'Age':32,'annualincome':126,'annualexpenses':74},
{'Genre':1,'Age':32,'annualincome':137,'annualexpenses':18},
{'Genre':1,'Age':30,'annualincome':137,'annualexpenses':83}
]
df = pd.DataFrame.from_dict(data, orient='columns')
edades = df.Age
ingresos = df.annualincome
egresos = df.annualexpenses
x1 = np.arange(len(edades)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rect1 = ax.bar(x1 - width/2, ingresos, width, label='annualincome')
rect2 = ax.bar(x1 + width/2, egresos, width, label='annualexpenses')
ax.set_ylabel('Scores')
ax.set_title('Scores by ingresos y egresos')
ax.set_xticks(x1)
ax.set_xticklabels(edades)
ax.legend()
def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = rect.get_height()
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
autolabel(rect1)
autolabel(rect2)
fig.tight_layout()
plt.show()