Re: plotting time data
- To: mathgroup at smc.vnet.net
- Subject: [mg19005] Re: plotting time data
- From: king at dircon.co.uk (Nigel King)
- Date: Tue, 3 Aug 1999 13:44:44 -0400
- Organization: Consultant
- References: <7nretf$idn@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
James L. Fisher <jlfisher at imt.net> wrote: > I would like to plot some time data where I have the dates and/or times on > the horizontal axis and the data on the vericle axis. What software would > I have to get inorder to plot this data over time. The data should be > plotted as points with a line joining the points. Getting the horizontal to > be labeled with the time and/or date is the trouble . I could use ListPlot > otherwise. > > Thanks > James I wrote these routines which deal with dates but haven't extended them to times. I hope that you can find them useful. I am not sure they are entirely campatible with v4. as you can see from 2.2 below they have been araound for some time. It would be nice if some nice person at wolfram would take them on and incorporate something as good if not better into Graphics`Graphics`. (*:Mathematica Version: 2.2 *) (*:Package Version: 0.2 *) (*:Name: DateFunctions *) (*:Context: NigelsPacks`DateFunctions` *) (*:Title: Additional Date Functions for plotting graphs with dates on the bottom Axis-Improvements to Log Axis control also achieved *) (*:Authors: Nigel King *) (*:Summary: *) (*:Copyright: Dropped *) (*:History: New package designed Jan 1999 *) (* :Keywords: DatePlot, DateLogPlot, DateLogListPlot, DateListPlot, DateScale, DateString, LogScale *) (* :Sources: *) (* :Warning: None*) (* :Mathematica Version: 3.0.1 *) (* *) (* :Limitation: *) (* :Discussion: *) Needs["Graphics`Graphics`"] BeginPackage["NigelsPacks`DateFunctions`"] EndPackage[] BeginPackage["Graphics`Graphics`", {"Graphics`Common`GraphicsCommon`"}] DatePlot::usage= "DatePlot[f, {x, xmin, xmax}] generates a plot of f as a function of x from \ xmin to xmax where x is in seconds"; DateLogPlot::usage= "DateLogPlot[f, {x, xmin, xmax}] generates a plot of Log[f] as a function \ of x in seconds converted to dates"; DateLogListPlot::usage="DateLogListPlot[{y1, y2, ...}]"; DateListPlot::usage= "DateListPlot[{y1, y2, ... }] plots a list of values (not very useful for \ dates). The x coordinates for each point are taken to be 1, 2, ... . \ ListPlot[{{x1, y1}, {x2, y2}, ... }] plots a list of values with specified x \ and y coordinates where the x values are dates in seconds."; DateScale::usage= "DateScale[xmin, xmax, n] gives a list of \"nice\" dates between xmin and \ xmax"; DateString::usage= "DateString[dateInSeconds] produce the string form of a date currently in \ the compact form \"dmmmyy\""; Begin["`Private`"] SetAttributes[{DateLogPlot}, HoldFirst]; Options[DatePlot] = Options[DateLogPlot]= Options[ParametricPlot]; Options[DateLogListPlot] =Options[DateListPlot] = Options[ListPlot]; DatePlot[fun_, range_, opts___]:= Plot[fun,range, Evaluate[tickopts[DateScale, Automatic, DatePlot, {#&,#&}, Flatten[{opts}]]], opts, Evaluate[Options[DatePlot]] ] DateLogPlot[fun_,range_,opts___] := ScaledPlot[fun,range, ScaleFunction -> {#&, Log[10,#]&}, tickopts[DateScale, LogScale, DateLogPlot, {#&, Log[10,#]&}, Flatten[{opts}]], scaleplotrange[DateLogPlot, Flatten[{opts}]], opts, Options[DateLogPlot] ] DateLogListPlot[data_, opts___]:= ScaledListPlot[data, ScaleFunction -> {#&, Log[10,#]&}, tickopts[DateScale, LogScale, DateLogListPlot, {#&, Log[10,#]&}, Flatten[{opts}]], scaleplotrange[DateLogListPlot, Flatten[{opts}]], opts, Options[DateLogListPlot] ] DateListPlot[data_, opts___]:= ListPlot[data, Evaluate[tickopts[DateScale, Automatic, DatePlot, {#&,#&}, Flatten[{opts}]]], opts, Evaluate[Options[DateListPlot]] ] scaleplotrange[type:(DateLogPlot |DateLogListPlot), opts_] := PlotRange -> Replace[PlotRange/.opts/.Options[type], {{x:(_List | _Symbol), y:(_List | _Symbol)} :> {x,log10[y]}, y_List :> log10[y]}] DateScale[minimum_,maximum_,n_Integer:8]:= Module[{w,r=maximum-minimum,spacing}, spacing=r/n; w=First[Select[{1,2,3,7,14,31,62,92,123,184,366,2 366, 3 366,4 366,5 366}*24*60*60, (spacing <= #)&]]; If[w<28*24*60*60,Map[DateTicks,Range[Round[minimum/w]*w,maximum,w]], d1={}; For[i=iMonth[minimum,w],i<maximum,i=iMonth[i+w],AppendTo[d1,i]]; Map[DateTicks,d1]]] DateString[x_]:= Module[{d=ToDate[Evaluate[Round[x]]]}, ToString[d[[3]]]<>""<> StringTake["JanFebMarAprMayJunJulAugSepOctNovDec", {d[[2]]*3-2,d[[2]]*3}]<>""<>ToString[Mod[d[[1]],100]]] DateTicks[x_]:={x,DateString[x]}; iMonth[x_]:=Module[{y=ToDate[Round[x]]},y=FromDate[{y[[1]],y[[2]],1,0,0, 0}]]; iMonth[x_,w_]:=Module[{y=ToDate[Round[x]],w1=Round[w/24/60/60/30]}, y=FromDate[{y[[1]],Round[y[[2]]/w1]w1+1,1,0,0,0}]]; GenGrid[min_, max_, n_Integer:6] := Module[{nmin=N[min], nmax=N[max], imin, imax, nper, t, tl} , imin=Round[nmin]-1 ; imax=Round[nmax]+1 ; (*If[imin == imax, imax+=1];*) nper = Floor[n/(max - min)] ; If[nper > 0, t = 10.^Range[imin, imax] ; tl = Take[ $LogPreferences, Min[nper, Length[$LogPreferences]] ] ; t = Flatten[Outer[Times, t, tl]] ; t = Sort[t]; t=Map[#[[2]]&, Select[Partition[t,3,1],#[[3]]>=10^min&&#[[1]]<=10^max&]] , (* else *) nper = Ceiling[(imax - imin)/n] ; t = 10.^Range[imin, imax, nper] ] ; Map[ If[ Log[10., #] < 5 && # == Round[#] , Round[#], #,#]&, t] ] MinorLogTicks[pts_] := Flatten[ Map[ MinorAux2, Map[{#[[1]], If[#[[2]]/#[[1]]<15, MinorAux1[#[[2]]-#[[1]]],{Times,10,Log[10,#[[2]]/#[[1]]]}]}&, Partition[pts,2,1]]], 1 ] MinorAux2[{xst_,{fun_,del_ ,n_}}]:= Module[{x= xst,pts},pts=Table[x=fun[x,del],{n-1}] ; Map[{Log[10., #],"",{0.6/160.,0.},{Thickness[0.001]}}&,pts]] MinorAux1[x_] := {Plus,x /#, #}&[RealDigits[ N[Rationalize[x]] ][[1,1]]] $LogPreferences = {1, 5, 2, 3, 1.5, 7, 4, 6, 1.2, 8, 9, 2.5,3.5,4.5,1.1, 1.3, 1.4,1.7,5.5,6.5,7.5,8.5,9.5,1.6,1.8,1.9,2.2,2.7,3.2,3.7,4.2,4.7}; End[ ] EndPackage[ ]