0

I'm trying to plot a line of best fit for this graph. I'm having trouble since x is a categorical variable, and all the solutions I've found don't work if one of them is a categorical variable.

enter image description here

Here's my code for the plot -

plt.title('Timeline of cases in Maharashtra')

plt.scatter(maharashtra_confirmed['Date'], maharashtra_confirmed['Maharashtra'], label = 'Maharashtra', color = 'orange')
plt.xticks(rotation = 90)
ax = plt.axes()
ax.xaxis.set_major_locator(ticker.MultipleLocator(2))
plt.legend()
plt.show()
sbhhdp
  • 353
  • 1
  • 3
  • 12
queenkrazykat
  • 11
  • 1
  • 1
  • 1
  • `lt.plot(np.unique(x), np.poly1d(np.polyfit(x, y, 1))(np.unique(x)))` from [here](https://stackoverflow.com/questions/22239691/code-for-best-fit-straight-line-of-a-scatter-plot-in-python). – Parth Shah Jul 10 '20 at 06:23
  • Does [this help](https://stackoverflow.com/questions/60556547/exponentialsmoothing-what-prediction-method-to-use-for-this-date-plot)? – Asmus Jul 10 '20 at 06:29

1 Answers1

0

For categorical variables, a fit would not make sense from a theoretical view point. But what you have here is not per-se categorical data, but (as far as I can guess from your input) just data that can not automatically be interpreted as non-categorical.

So you have to convert the dates into a more adequate representation. datetime.datetime.strptime should help you here.

koalo
  • 2,113
  • 20
  • 31