I took Jake's code from Scatterplot with Marginal Histograms and ran into a problem with trying to histogram date data on the x axis. The scatterplot and y axis histogram work well, revealing:
The code for the top histogram is:
%% The histogram for the x axis
\begin{axis}[
date coordinates in=x, xticklabel={\year}, date ZERO=1996-01-01,
anchor=south west, axis y line*=right, axis x line*=bottom,
at=(main axis.north west), xmin=1996-01-01, xmax=2015-12-01,
height=3cm, yshift=1.2cm, ymajorgrids,
x axis line style={opacity=0}, ymin=0, ymax=25,
xtick=\empty, ytick={0,5,10,15,20,25},
]
%\addplot [
% hist={data=x}, % By default, the y values
% fill=yellow!50 %would be used for calculating the histogram
% ] table {bikeplotoa.dat};
\end{axis}
I read that date coordinates in=x,
convert the date format to Julean data in Integer form (pg.332).
How do I histogram date format data? Is there any way to get this to work with the hist routine?
This is what pdflatex spits out:
! Package PGF Math Error: Could not parse input '1996-01-01' as a
floating point number, sorry.
The unreadable part was near '-01-01'..
The essentail MWE is:
\documentclass[12ptl]{article}
%\usepackage[letterpaper,margin=2cm]{geometry}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=newest}
\begin{document}
\begin{center}
\begin{tikzpicture}[
/pgfplots/scale only axis,
/pgfplots/width=0.7\linewidth, %6cm,
/pgfplots/height=0.7\linewidth %6cm
]
% The scatterplot
\begin{axis}[
date coordinates in=x, xticklabel={\year}, date ZERO=1997-10-02,
xtick={1997-01-01,1999-01-01,2001-01-01,2003-01-01,2005-01-01,2007-01-01,2009-01-01,
2011-01-01,2013-01-01,2015-01-01},
minor xtick={1998-01-01,2000-01-01,2002-01-01,2004-01-01,2006-01-01,2008-01-01,
2010-01-01, 2012-01-01,2014-01-01,2016-01-01},
name=main axis, % Name the axis, so we can position
% the histograms relative to this axis
axis y line*=right, axis x line*=top, tick align=outside,
fill=green!50, xmin=1996-01-01, xmax=2015-12-01, ymin=0, ymax=55,
separate axis lines, xmajorgrids, xminorgrids, ymajorgrids,
xlabel=Year, ylabel=Miles, minor tick num=2,
]
\addplot [only marks, mark size=1.5] table {bikeplotoa.dat};
\end{axis}
%% The histogram for the x axis
\begin{axis}[
date coordinates in=x, xticklabel={\year}, date ZERO=1996-01-01,
anchor=south west, axis y line*=right, axis x line*=bottom,
at=(main axis.north west), xmin=1996-01-01, xmax=2015-12-01,
height=3cm, yshift=1.2cm, ymajorgrids,
x axis line style={opacity=0}, ymin=0, ymax=25,
xtick=\empty, ytick={0,5,10,15,20,25},
]
%\addplot [
% hist={data=x}, % By default, the y values
% fill=yellow!50 %would be used for calculating the histogram
% ] table {bikeplotoa.dat};
\end{axis}
% The histogram for the y axis
\begin{axis}[ ymin=0, ymax=55,
anchor=north west, axis y line*=left, axis x line*=top,
y axis line style={opacity=0},
at=(main axis.north east), xmajorgrids,
width=4cm, xshift=1.5cm, xmin=0, xmax=160,
ytick=\empty, xtick={0,40,80,120,160},
]
\addplot [
% For swapping the x and y axis, we have to change a couple of options...
hist={handler/.style={xbar interval}, bins=11,
data min=0, data max=55}, % ... use bars instead of columns ...
x filter/.code=\pgfmathparse{rawy}, % ... interpret the x values of the histogram as y values
y filter/.code=\pgfmathparse{rawx}, % ... and vice versa.
fill=blue!50,
] table {bikeplotoa.dat};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
The file: bikeplotoa.dat contains 670 lines in the form:
date miles
1997-10-02 15.6
1997-10-03 8.6
1997-10-04 12.1
1997-10-05 15.1
1997-10-06 10.5
1997-10-07 2.9
1997-10-08 10.9
1997-10-09 8.1
1997-10-11 13.3
1997-10-12 9.5
1997-10-12 9.5
1997-10-17 7.9
1997-10-18 9
1997-10-23 11.3
1997-10-27 5.7
Regards, Dave
Best Answer
As already seen in Ben's answer it seems that you have to convert the dates to some numeric value to make it work.
I present a solution where I extend your data table by converting the dates with the
\pgfcalendardatetojulian
command from thepgfcalendar
library/package and then use this new column for the histogram.For more details have a look at the comments in the code.