0

I am trying to plot a lineplot in seaborn with values for the y axis and dates for the x axis coming from a pandas dataframe.

When I create the lineplot without converting the date column to datetime object it puts the dates in the wrong order. When I do convert the date column to datetime format it gives strange x labels and only shows 5 of the dates.

df = pd.read_csv("dddd.csv")
df["date"] = pd.to_datetime(df["date"])
df = df.sort_values(["date"])
ax = sns.lineplot(y=df["google"],x=df["date"],color="red",data=df)

plt.show()

I want to just plot the data with the x labels being the dates have it in order. Here is some example data.

25-03-2019  -100
26-03-2019  -66.66666667
27-03-2019  -80
28-03-2019  -87.08333333
29-03-2019  -88.88888889
30-03-2019  -86.28526646
31-03-2019  -87.5
01-04-2019  -87.87878788
02-04-2019  -82.92682927
03-04-2019  -84.09090909
04-04-2019  -84.7826087
05-04-2019  -85.71428571
06-04-2019  -81.30677848
07-04-2019  -81.98051948
08-04-2019  -82.14285714
09-04-2019  -78.46153846
10-04-2019  -76.05633803
11-04-2019  -75
12-04-2019  -75
13-04-2019  -80
14-04-2019  -83.33333333
15-04-2019  -83.33333333
16-04-2019  -77.77777778
17-04-2019  -68
18-04-2019  -54.70085471
19-04-2019  -64.70588235
20-04-2019  -66.66666667
user1365234
  • 315
  • 1
  • 5
  • 16
  • Read [this](https://stackoverflow.com/questions/11376080/plot-numpy-datetime64-with-matplotlib), [this](https://stackoverflow.com/questions/12945971/pandas-timeseries-plot-setting-x-axis-major-and-minor-ticks-and-labels), [this](https://stackoverflow.com/questions/8010549/subplots-with-dates-on-the-x-axis), [this](https://stackoverflow.com/questions/35465409/using-datetime-as-ticks-in-matplotlib), [this](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.tick_params.html#matplotlib.axes.Axes.tick_params) or [this](https://matplotlib.org/api/axis_api.html#ticks-tick-labels-and-offset-text) – flurble Apr 30 '19 at 14:37

1 Answers1

0

Despite this question most certainly being a duplicate, here is some explanation:

  1. Without converting to datetime, your x-axis is unordered, because python doesn't understand the values are dates. Here it probably draws a tick for every point, because it doesn't know how to fit "16-04-2019" on a continuous scale. (Try plotting an array of strings on the x-axis as comparison).

  2. If you do convert to datetime, seaborn and pandas are intelligent enough, to notice your dates form a continuous scale. Because the ticklabels would overlap when drawing all of them, it leaves out some of them and draws ticks in regular intervals instead.

You can influence the location with ax.set_ticks(locations), their style with ax.tick_params(rotation=90, horizontal_alignment='left') or their labels with ax.set_xticklabels(labels). (keywords as example).

You can also use

import matplotlib.dates ad md

locs = md.YearLocator() # Locations for yearly ticks
format = md.MajorFormatter('%B %Y') # Format for labels

to access formatter functions and ax.set_major_locator / formatter to apply them to your plot.

To simply draw a tick for every datapoint, in your case use:

ax.set_xticks(df["Date"])
flurble
  • 1,086
  • 7
  • 21