1

I'm trying to plot the values of a set of categories on a daily basis. The data is sorted based on the dates. But when I plot it with a seaborn line plot, it gets totally mixed up! (I added sort = False but didn't work)

Here is the code:

line1 = sns.lineplot(data = df_2,  x = 'date', y = 'cost', hue = 'category',
                 sort = False, style="category", markers=True,  dashes=False)

line1.set_xticklabels(df_2.date, rotation=90);

messedup plot

It looks like the line plot is trying to sort the date values by itself and since my dates are like '%m-%d', it sorts the values first based on the days and then months!

Zephyr
  • 11,891
  • 53
  • 45
  • 80
mitra mirshafiee
  • 393
  • 6
  • 17
  • 1
    Welcome to Stack Overflow! I would help if you could provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) that includes a toy dataset (refer to [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)) – Diziet Asahi Aug 01 '20 at 12:17
  • Hi! Thank you for answering. [This](https://drive.google.com/file/d/1dyQTRr98sW0JbfDQPcZjPSMVhUyvgaF7/view?usp=sharing) is the data I'm trying to plot and It is the df_2 that I used in the code in my question. I think you can remake it. please let me know if there was anything more you needed. @DizietAsahi – mitra mirshafiee Aug 05 '20 at 07:01

1 Answers1

1

As per your data, you are getting the correct line plot. Your data is already sorted by date, so, sort = False is not working in this case. You can see the below two plots.

plt.figure(figsize = (20, 8))
line = sns.lineplot(data = data, x = 'date', y = 'cost', hue = 'category')
line.set_xticklabels(data.date, rotation=90)
plt.show()

enter image description here

plt.figure(figsize = (20, 8))
line = sns.lineplot(data = data, x = 'date', y = 'cost', hue = 'category', sort = False)
line.set_xticklabels(data.date, rotation=90)
plt.show()

enter image description here

Manish KC
  • 103
  • 1
  • 9
  • thanks for your response, but how should I change the code to get all days and all categories of my excel spreadsheet? – mitra mirshafiee Aug 14 '20 at 08:28
  • The above plot has all the dates and all the categories (with different lineplots in different colors, which color is which category is shown on the legend, top right corner of the plot). – Manish KC Aug 14 '20 at 17:11