1

I'm trying to plot this table

    Year    Average Mi  Gasoline
0   2022    336.714286  4.00
1   2021    259.674419  2.84
2   2020    280.342105  2.25
3   2019    219.228571  2.67
5   2018    187.083333  2.72
4   2017    166.900000  2.33
6   2016    145.096774  2.17
7   2015    137.388889  2.44
8   2014    114.800000  3.46
9   2013    906.142857  3.44
10  2012    90.111111   3.63
14  2011    53.250000   3.46
11  2008    90.000000   3.37
12  2003    65.000000   1.54
13  2002    76.500000   1.35
16  2001    57.600000   1.46
15  2000    63.250000   1.57

in two plots. One is a bar graph the y is the average miles and the x is the year. The second one is a line graph where the y is the gasoline price and the x is the year.

I want them both to be on the same plot so I've been using the following code

fig, ax = plt.subplots(figsize=(12, 6))

sns.lineplot(data=df, x='Year', y='Gasoline', marker='o', sort=False, color='red', ax=ax)
ax2= ax.twinx()
sns.barplot(data=df, x='Year', y='Average Mi', alpha=0.5, ax=ax2)

Individually each of the plots work and show the data as I need it. However, running the above code only shows the barplot without the lineplot.

0 Answers0