0

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()

My current output: enter image description here

My desired output is: enter image description here

Mr. T
  • 11,960
  • 10
  • 32
  • 54
SHAMAY
  • 1
  • 1
  • In general, if you are not happy with the automatic matplotlib tick labeling, you have to define the axis Locator and Formatter. You will find various examples on SO, for instance [here](https://stackoverflow.com/a/65343940/8881141). However, I am not sure what you want to change as the graph already shows monthly tick labels. – Mr. T Feb 05 '22 at 07:24
  • Above output is the desired output, current output shown as current output on the initial comment (edited later). – SHAMAY Feb 05 '22 at 14:30
  • Have you tried to set the locator and formatter as suggested? Get the current axis object with `ax = plt.gca()`, then set the tick frequency with the locator, e.g., `ax.xaxis.set_major_locator(MonthLocator(interval=1))` and the label format with the formatter, e.g., `ax.xaxis.set_major_formatter(DateFormatter("%b-%y"))`. Please read the linked documentations to understand how you can adapt the output to your needs. – Mr. T Feb 05 '22 at 15:18

1 Answers1

-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()

enter image description here

Ka Wa Yip
  • 2,546
  • 3
  • 22
  • 35
  • Thank you Ka-Wa Yip. Unfortunately, getting value error while trying to use the code. ValueError: x and y must have same first dimension, but have shapes (3,) and (17,). – SHAMAY Feb 05 '22 at 14:33
  • @SHAMAY change `x` to `x = ['Jan 2019', '','','','Feb', '','','','Mar','','','','','Apr','','','']` if your input is of size 17. – Ka Wa Yip Feb 05 '22 at 14:42