How Do You Graph Multiple Items In A Dataframe On One Graph Using Pandas And Matplotlib.pyplot?
The dataframe I am trying to graph is below. I want to plot each fieldname as the legend item with x=year and y=value The name of the dataframe is my_gross fie
Solution 1:
The standard pandas
and matplotlib
approach is to pivot
to wide-form and plot
:
import pandas as pd
from matplotlib import pyplot as plt
plot_df = df.pivot(index='year',
columns='fieldName',
values='value')
plot_df.plot()
plt.tight_layout()
plt.show()
plot_df
:
fieldNamediluted_shares_outstandingepssales_revenueyear2015 96379000.07.360470e+085.817000e+092016 87775000.07.285207e+085.762000e+092017 85562000.08.944702e+086.641000e+092018 83530000.01.298734e+098.047000e+092019 77710000.01.451550e+099.351000e+092020 72929000.01.259110e+098.530000e+09
seaborn.lineplot
has built-in functionality with hue
without needing to reshape:
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
sns.lineplot(data=df, x='year', y='value', hue='fieldName')
plt.tight_layout()
plt.show()
Solution 2:
There're several ways to do it, depending on libraries available.
Using just pandas
(with matplotlib
being used by pandas in backend):
Loop over unique values in your 'fieldName' column, filter the DataFrame to only include that value, set index to year (this will be your x-axis), and choose the value you intent to plot ('value' Series), then plot it.
for fieldname in df['fieldName'].unique():
df[df['fieldName'] == fieldname].set_index('year')['value'].plot(label = fieldname)
plt.legend()
Outputs:
EDIT:
Seems like a relatively simple groupby works (no loops needed):
df.set_index('year').groupby('fieldName')['value'].plot()
plt.legend()
Outputs:
Post a Comment for "How Do You Graph Multiple Items In A Dataframe On One Graph Using Pandas And Matplotlib.pyplot?"