Sea container does not show dates on x axis well

Below I have the following script that creates a simple time series chart:

%matplotlib inline import datetime import pandas as pd import seaborn as sns import matplotlib.pyplot as plt fig, ax = plt.subplots() df = [] start_date = datetime.datetime(2015, 7, 1) for i in range(10): for j in [1,2]: unit = 'Ones' if j == 1 else 'Twos' date = start_date + datetime.timedelta(days=i) df.append({ 'Date': date.strftime('%Y%m%d'), 'Value': i * j, 'Unit': unit }) df = pd.DataFrame(df) sns.tsplot(df, time='Date', value='Value', unit='Unit', ax=ax) fig.autofmt_xdate() 

And the result of this is the following:

enter image description here

As you can see, the x axis has strange numbers for datetimes, not the usual "nice" views that come with matplotlib and other charting utilities. I tried many things, reformatted the data, but it is never clean. Does anyone know the way?

+7
python matplotlib datetime seaborn
source share
2 answers

Matplotlib presents dates as floating-point numbers (in days), so if you (or pandas or marine) do not say that your values โ€‹โ€‹represent dates, they will not format ticks as dates. I am not a marine expert, but it looks like it (or pandas) will convert datetime objects to matplotlib dates, but then it will not assign the correct locators and formatting elements for the axes. That is why you get these strange numbers, which are actually only days from 01/01/01/01. Therefore, you will have to take care of ticks manually (which is better in most cases, as it gives you more control).

So, you need to assign a date locator that decides where to put the ticks, and a date format that will then format the strings for the label shortcuts.

 import datetime import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import matplotlib.dates as mdates # build up the data df = [] start_date = datetime.datetime(2015, 7, 1) for i in range(10): for j in [1,2]: unit = 'Ones' if j == 1 else 'Twos' date = start_date + datetime.timedelta(days=i) # I believe it makes more sense to directly convert the datetime to a # "matplotlib"-date (float), instead of creating strings and then let # pandas parse the string again df.append({ 'Date': mdates.date2num(date), 'Value': i * j, 'Unit': unit }) df = pd.DataFrame(df) # build the figure fig, ax = plt.subplots() sns.tsplot(df, time='Date', value='Value', unit='Unit', ax=ax) # assign locator and formatter for the xaxis ticks. ax.xaxis.set_major_locator(mdates.AutoDateLocator()) ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y.%m.%d')) # put the labels at 45deg since they tend to be too long fig.autofmt_xdate() plt.show() 

Result:

enter image description here

+8
source share

For me, @hitzg will respond to the results in "OverflowError: signed integer is greater than maximum" in the depths of a DateFormatter.

Looking at my data framework, my indexes have datetime64, not datetime. Pandas converts them though. The following works fine for me:

 import matplotlib as mpl def myFormatter(x, pos): return pd.to_datetime(x) [ . . . ] ax.xaxis.set_major_formatter(mpl.ticker.FuncFormatter(myFormatter)) 
+7
source share

All Articles