Создать график с помощью matplotlib - PullRequest
1 голос
/ 27 мая 2020

Я пытаюсь создать график с помощью matplotlib из фрейма данных. На графике я хочу показать взаимосвязь между возрастом людей и деньгами, которые они зарабатывают, и деньгами, которые они тратят. Проблема в том, что я не могу сделать график понятным или разборчивым.

Не могли бы вы помочь мне правильно сгенерировать график, пожалуйста. Спасибо

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')

#grafica edad dinero que ganan y gastan
age_money_expenses = df.drop(['Genre'],1)
age_money_expenses = age_money_expenses.groupby(['annualincome','annualexpenses']).sum()['Age']
fig, ax = plt.subplots(figsize=(20,12))

age_money_expenses.unstack().plot(ax=ax)

image

1 Ответ

2 голосов
/ 27 мая 2020

Есть много разных способов сделать это, но один из вариантов - это что-то вроде следующего: два расположенных рядом графика рассеяния с линиями тренда линейной регрессии. Вы, вероятно, захотите реорганизовать и это, а не повторять большую часть одного и того же кода дважды для двух подзаговоров.

import numpy as np 

poly = np.polynomial.polynomial

df = pd.DataFrame.from_dict(data, orient='columns')

age_money_expenses = df.drop(['Genre'],1)

fig, axes = plt.subplots(1,2, sharey=True)

ax0 = age_money_expenses.plot.scatter(
    x='Age',
    y='annualincome',
    title='Age vs. Annual Income', 
    ax=axes[0])

coefs1, stats1 = poly.polyfit( # linear regression
    age_money_expenses['Age'], 
    age_money_expenses['annualincome'], 
    1, 
    full=True)

axes[0].plot(
    age_money_expenses['Age'], 
    poly.polyval(age_money_expenses['Age'], coefs1), 
    "r--") # Add trendline

ssTot1 = np.sum( # total sum of squares
    (age_money_expenses['annualincome'] - np.average(age_money_expenses['annualincome'])) ** 2)

rSquared1 = 1 - stats1[0][0] / ssTot1 # stats1[0][0] is sum of squared residuals 

plt.gcf().text(0.38, 0.85, "$R^2 = %0.4f$" % rSquared1)

age_money_expenses.plot.scatter(
    x='Age',
    y='annualexpenses',
    title='Age vs. Annual Expenses', 
    ax=axes[1])

coefs2, stats2 = poly.polyfit(
    age_money_expenses['Age'], 
    age_money_expenses['annualexpenses'], 
    1, 
    full=True)

axes[1].plot(
    age_money_expenses['Age'], 
    poly.polyval(age_money_expenses['Age'], coefs2), 
    "r--")

ssTot2 = np.sum(
    (age_money_expenses['annualexpenses'] - np.average(age_money_expenses['annualexpenses'])) ** 2)

rSquared2 = 1 - stats2[0][0] / ssTot2

plt.gcf().text(0.83, 0.85, "$R^2 = %0.4f$" % rSquared2)

ax0.set_ylabel("Annual Income or Expenses")
plt.tight_layout()

Результат (изображение): График

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...