- Creating a plot is all about the shape of the DataFrame.
- One way to accomplish this is by converting the DataFrame from wide to long, with
melt
, but this isn't necessary.
- The primary requirement, is set
'State'
as the index.
- Plots can be generated directly with
df
, or df.T
(.T
is the transpose of the DataFrame).
- The OP requests a line plot, but this is discrete data, and the correct way to visualize discrete data is with a bar plot, not a line plot.
pandas v1.2.3
, seaborn v0.11.1
, and matplotlib v3.3.4
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = {'State': ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut'],
'ACT17': [100, 65, 62, 100, 31, 100, 31],
'ACT18': [100, 33, 66, 100, 27, 30, 26],
'ACT19': [100, 38, 73, 100, 23, 27, 22],
'ACT20': [100, 33, 71, 100, 19, 25, 19]}
df = pd.DataFrame(data)
# set State as the index - this is important
df.set_index('State', inplace=True)
# display(df)
ACT17 ACT18 ACT19 ACT20
State
Alabama 100 100 100 100
Alaska 65 33 38 33
Arizona 62 66 73 71
Arkansas 100 100 100 100
California 31 27 23 19
Colorado 100 30 27 25
Connecticut 31 26 22 19
# display(df.T)
State Alabama Alaska Arizona Arkansas California Colorado Connecticut
ACT17 100 65 62 100 31 100 31
ACT18 100 33 66 100 27 30 26
ACT19 100 38 73 100 23 27 22
ACT20 100 33 71 100 19 25 19
Plot 1
df.T.plot()
plt.legend(title='State', bbox_to_anchor=(1.05, 1), loc='upper left')
# get rid of the ticks between the labels - not necessary
plt.xticks(ticks=range(0, len(df.T)))
plt.show()

Plot 2 & 3
- Use
pandas.DataFrame.plot
with kind='bar'
or kind='barh'
- The bar plot is much better at conveying the yearly changes in the data, and allows for an easy comparison between states.
df.plot(kind='bar')
plt.legend(title='Year', bbox_to_anchor=(1.05, 1), loc='upper left')
plt.show()


Plot 4
- Use
seaborn.lineplot
- Will correctly plot a line plot from a wide dataframe with the columns and index labels.
sns.lineplot(data=df.T)
plt.legend(title='State', bbox_to_anchor=(1.05, 1), loc='upper left')
plt.show()
