4

I need to plot a time series. Dates on the X axis and values on the Y axsis, but I also need to specify the day of week on the X axsis.

ax = sns.lineplot(x='date', y='value', data=df)

I expect to be able to add day of week (another column from df) on the X axis. example with Excel

Itay
  • 16,601
  • 2
  • 51
  • 72
Diego
  • 53
  • 1
  • 7
  • A simple solution is to produce a `date2` column which is combined of `date` and `day_of_week`. Or you can try the solution [here](https://stackoverflow.com/questions/43968985/changing-the-formatting-of-a-datetime-axis-in-matplotlib/43969357#43969357). – Quang Hoang May 15 '19 at 16:39

1 Answers1

3

You can try to do this by adding a second x-axis. Please find below a code you'll need to adapt to your problem. I guess there are better ways to do that but it should works.

from matplotlib.ticker import MultipleLocator
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

x = np.arange(1000)
x2 = np.arange(1000)*2
y = np.sin(x/100.)

fig = plt.figure()

ax = plt.subplot(111)
sns.lineplot(x, y)
plt.xlim(0, 1000)
ax.xaxis.set_major_locator(MultipleLocator(200))

ax2 = ax.twiny()
sns.lineplot(x2, y, visible=False)
plt.xlim(0, 2000)
ax2.xaxis.set_major_locator(MultipleLocator(400))
ax2.spines['top'].set_position(('axes', -0.15))
ax2.spines['top'].set_visible(False)
plt.tick_params(which='both', top=False)

enter image description here

Mitchell van Zuylen
  • 3,905
  • 4
  • 27
  • 64
TVG
  • 370
  • 2
  • 11