1

When plotting a DataFrame with pandas:

import pandas as pd
import matplotlib.pyplot as plt 
from StringIO import StringIO

mycsv = StringIO("""
time;openBid;highBid;lowBid;closeBid;openAsk;highAsk;lowAsk;closeAsk;volume
2015-12-06T22:00:00.000000Z;1.08703;1.08713;1.08703;1.08713;1.08793;1.08813;1.08793;1.08813;2
2015-12-07T22:00:05.000000Z;1.08682;1.08682;1.08662;1.08662;1.08782;1.08782;1.08762;1.08762;2
2015-12-08T22:01:20.000000Z;1.08683;1.08684;1.08681;1.08684;1.08759;1.08768;1.08759;1.08768;4
2015-12-09T22:01:30.000000Z;1.08676;1.08676;1.08676;1.08676;1.0876;1.0876;1.0876;1.0876;1
2015-12-10T00:03:00.000000Z;1.08675;1.08675;1.08675;1.08675;1.08737;1.08737;1.08737;1.08737;1
2015-12-06T22:03:10.000000Z;1.08675;1.08675;1.08675;1.08675;1.08728;1.08728;1.08728;1.08728;1
2015-12-06T22:03:50.000000Z;1.08676;1.08676;1.08676;1.08676;1.08728;1.08728;1.08728;1.08728;1
""")

df = pd.read_csv(mycsv, delimiter=';', parse_dates=True, index_col='time', header=0)
spr = df['lowAsk']-df['lowBid']
spr.plot()
plt.show()

I get this:

enter image description here

Question:

How to add ticks+labels+grid on x-axis for each day?

Example: Sun 06, Mon 07, Tue 08, etc.

I tried various things like plt.axis(...) but it was mostly unsucessful.

Community
  • 1
  • 1
Basj
  • 41,386
  • 99
  • 383
  • 673
  • 1
    Remark: [this](http://stackoverflow.com/a/13674286/1422096) didn't really help me in this case – Basj Dec 18 '15 at 13:53

1 Answers1

1

You can do this with DayLocator and DatesFormatter from matplotlib.dates.

From the docs:

DayLocator

Make ticks on occurances of each day of the month

And

DateFormatter

Use a strftime() format string.

So, in your case, we want to set the format string to "%a %d" to get Mon 07, etc.

We set these for the major_locator and major_formatter of the xaxis of the Axes returned by pandas when you call spr.plot()

Heres you example:

import pandas as pd
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates
from StringIO import StringIO

mycsv = StringIO("""
time;openBid;highBid;lowBid;closeBid;openAsk;highAsk;lowAsk;closeAsk;volume
2015-12-06T22:00:00.000000Z;1.08703;1.08713;1.08703;1.08713;1.08793;1.08813;1.08793;1.08813;2
2015-12-07T22:00:05.000000Z;1.08682;1.08682;1.08662;1.08662;1.08782;1.08782;1.08762;1.08762;2
2015-12-08T22:01:20.000000Z;1.08683;1.08684;1.08681;1.08684;1.08759;1.08768;1.08759;1.08768;4
2015-12-09T22:01:30.000000Z;1.08676;1.08676;1.08676;1.08676;1.0876;1.0876;1.0876;1.0876;1
2015-12-10T00:03:00.000000Z;1.08675;1.08675;1.08675;1.08675;1.08737;1.08737;1.08737;1.08737;1
2015-12-06T22:03:10.000000Z;1.08675;1.08675;1.08675;1.08675;1.08728;1.08728;1.08728;1.08728;1
2015-12-06T22:03:50.000000Z;1.08676;1.08676;1.08676;1.08676;1.08728;1.08728;1.08728;1.08728;1
""")
df = pd.read_csv(mycsv, delimiter=';', parse_dates=True, index_col='time', header=0)
spr = df['lowAsk']-df['lowBid']

ax=spr.plot()

ax.xaxis.set_major_locator(mdates.DayLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%a %d'))

plt.show()

enter image description here

Community
  • 1
  • 1
tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • Thanks a lot! Can I add a little something: Is it possible to add also a few hours ticks per day? i.e. for example 0h 6h 12h 18h ... + also vertical grid for each new day? – Basj Dec 18 '15 at 17:37
  • Try setting the minor_locator to an mates.HourLocator. Check the docs for how to do select hours. I'll edit the answer when I get a chance later – tmdavison Dec 18 '15 at 19:00