'Plot multiple line graphs from a dataframe using Matplotlib
Trying to produce a linegraph showing weekly fares by City types (urban, suburban, rural). Getting output as it is in the dataframe by week. I want to change the x axis label to month (Jan-Apr) instead of the dates while showing the output by week. Image below shows desired output. Mine was a bit messy as it was showing by week (based on the weekly data)
DataFrame used (Partial):
date Rural Suburban Urban
2019-01-06 187.92 721.60 1661.68
2019-01-13 67.65 1105.13 2050.43
2019-01-20 306.00 1218.20 1939.02
2019-01-27 179.69 1203.28 2129.51
2019-02-03 333.08 1042.79 2086.94
Below is my code:
#use the graph style fivethirtyeight.
plt.style.use('fivethirtyeight')
plt.plot(weekly_fare_df['Rural'], color='blue')
plt.plot(weekly_fare_df['Suburban'], color='red')
plt.plot(weekly_fare_df['Urban'], color='gold')
plt.show()
Solution 1:[1]
The recipe is to define an x = ['Jan 2019', '','','','Feb', '','','','Mar','','','','','Apr','','',''], and add it to plt.plot.
import matplotlib.pyplot as plt
x = ['Jan 2019', '','','','Feb', '','','','Mar','','','','','Apr','','','']
fig, ax = plt.subplots()
plt.style.use('fivethirtyeight')
plt.plot(x,weekly_fare_df['Rural'], color='blue', label='Rural')
plt.plot(x,weekly_fare_df['Suburban'], color='red', label='Suburban')
plt.plot(x,weekly_fare_df['Urban'], color='gold', label='Urban')
plt.title('Total Fare by City Type', fontsize=15)
plt.legend(title="type",loc=4, fontsize='small', fancybox=True)
plt.ylabel('Fare ($USD)', fontsize=13)
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |



