0

I'm trying to plot some date in matplotlib:

plt.ylabel("Word Frequency")
plt.xlabel("Date")

x_values = [datetime.datetime.strptime(key,"%Y-%m-%d").date() for key in final]
y_values = [9,2,9,4,5,6,7,8,9]

plt.xticks(rotation=90)
plt.plot(x_values,y_values)

plt.show()

This code is quite simple, but the result is seriously odd-looking:

matplotlib date on axis, cause gebrish data

When I did a print(x_values), I got :

[datetime.date(2021, 4, 28), datetime.date(2021, 4, 20), datetime.date(2021, 5, 8), datetime.date(2021, 4, 29), datetime.date(2021, 4, 14), datetime.date(2021, 5, 11), datetime.date(2021, 5, 4), datetime.date(2021, 4, 26), datetime.date(2021, 5, 2)]

And these values don't even reassemble the values plotted.

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
  • The line segments are connected in the order they appear. You need to sort the x.values to get a nice graph. Or create a scatter plot. – JohanC Jul 03 '21 at 18:38
  • @JohanC scatter is supposed to plot only, points... but there is something wrong with these data, please check this image : https://i.stack.imgur.com/O8dgz.png – Maifee Ul Asad Jul 03 '21 at 18:43
  • 1
    @MaifeeUlAsad You didn't ask for a scatter plot. You asked for a line plot. Cut-and-paste error. – Tim Roberts Jul 03 '21 at 18:45

1 Answers1

0

Mainly un-sorted list was the problem:

x_values = [datetime.datetime.strptime(key,"%Y-%m-%d").date() for key in final]
x_values.sort()
y_values = [9,2,9,4,5,6,7,8,9]
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86