Thursday, June 24, 2021

【PYTHON】Choosing the right method for plotting

 from sqlalchemy import create_engine

engine = create_engine('sqlite:///chinook.db')


with engine.connect() as connection:

  sql_df = pd.read_sql_table('invoices', connection)


sql_df.head()


sql_df.groupby('BillingCountry').sum().sort_values(by='Total', ascending=False)[:3]


top_3_countries = sql_df.groupby('BillingCountry').sum().sort_values(by='Total', ascending=False)[:3].index.values


top_3_countries


sql_df.set_index('InvoiceDate', inplace=True)


gb = sql_df[sql_df['BillingCountry'].isin(top_3_countries)]. \

groupby([pd.Grouper(freq='M'), 'BillingCountry']).sum(). \

groupby(level=-1).cumsum()


gb


gb.reset_index(inplace=True)


sql_df.head()


sns.lineplot(data=gb, x='InvoiceDate', y='Total', hue='BillingCountry')


# saving figure for book

sns.lineplot(data=gb, x='InvoiceDate', y='Total', hue='BillingCountry')

plt.tight_layout()

plt.savefig('seaborn_lineplot_color.png', dpi=300)

No comments:

Post a Comment

End of Summer Sale ☀️😎

20% OFF Inside!🤯 ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏...