0

I am trying to plot forecast values in a line chart. The x-ticks are supposed to be the month and year (e.g April 2016). The problem is my plot is not ordered correctly and I cannot see the actual x-tick values.

This is the code I have

ax_forecast= forecasted.groupby('Month_Year')['yhat'].sum().plot(figsize=(18,10))
ax_forecast.get_yaxis().get_major_formatter().set_scientific(False)

This is the data I am trying to plot:

forecasted = {'yhat': {'April-2016': 43929615.41783013,
  'April-2017': 185274599.56791607,
  'August-2016': 60087106.8660238,
  'August-2017': 261765749.04794392,
  'December-2016': 124431182.12443578,
  'December-2017': 332058061.2991861,
  'February-2016': 34433800.70402009,
  'February-2017': 142399676.4087281,
  'January-2016': 34457502.66028841,
  'January-2017': 137696430.5545896,
  'July-2016': 54386802.22264939,
  'July-2017': 240745719.65645707,
  'June-2016': 47449151.90045062,
  'June-2017': 222406328.28095606,
  'March-2016': 42585010.655475326,
  'March-2017': 177069816.0218604,
  'May-2016': 46727385.96441124,
  'May-2017': 206100279.7210855,
  'November-2016': 102356396.73384078,
  'November-2017': 304690514.1417595,
  'October-2016': 88107961.21290714,
  'October-2017': 288239985.2960009,
  'September-2016': 70276527.38316415,
  'September-2017': 274392518.4590202}}

This is the plot I am getting:enter image description here

Emm
  • 2,367
  • 3
  • 24
  • 50
  • Your code example is not working for me by equaling `forecasts` to the data you posted. If you could refactor that a little so we can get results that would be nice, perhaps include the graph image result as well to the question body. – lucasgcb Mar 29 '19 at 09:17
  • @lucasgcb sorry about that have refactored and added image – Emm Mar 29 '19 at 09:27
  • Did you try explicitly setting Xticks? `plt.xticks(X_Axis, my_xticks)`? `my_xticks` can be extracted from your `forecasted` – Bhargav Kinnal Mar 29 '19 at 09:43
  • I'm still unable to run your code without problems. The following question might help you better: https://stackoverflow.com/questions/3100985/plot-with-custom-text-for-x-axis-points – lucasgcb Mar 29 '19 at 10:42
  • This is essentially [this issue](https://github.com/pandas-dev/pandas/issues/22334). You can use matplotlib to plot the strings instead. Or you convert your datestrings to real dates, in which case pandas plotting works just fine. – ImportanceOfBeingErnest Mar 30 '19 at 03:37

0 Answers0