PyPlot.plot_date: How do I alter the time format?

1,252 views
Skip to first unread message

RecentConvert

unread,
Nov 20, 2014, 7:58:00 AM11/20/14
to julia...@googlegroups.com
The time format automatically updates based on the time span but at the time span that I'm working on it's difficult to make sense of it. How do I alter the time format?

The Matplotlib documentation's plot_date section says how to do it but I have had no luck doing it in Julia so far.

Steven G. Johnson

unread,
Nov 21, 2014, 10:41:43 AM11/21/14
to julia...@googlegroups.com
If you post what you did in Python, we should be able to translate it to Julia.

RecentConvert

unread,
Nov 26, 2014, 8:38:43 AM11/26/14
to julia...@googlegroups.com
using PyPlot
using Dates

# Create Data
dt
= Millisecond(200)
time
= [DateTime(2014,11,20):dt:DateTime(2014,11,24)]
y
= fill!(Array(Float64,length(time)),42)
#y = floor(100*rand(length(time))) # Causes error: "OverflowError: Allocated too many blocks"

font1
= ["fontname"=>"Sans","style"=>"normal"]
time
= float64(time)/1000/60/60/24 # Convert time from milliseconds from day 0 to days from day 0

# Plot
fig
= figure("Test Plot",figsize=(16,10)) # Create a figure and save the handle
ax1
= axes()
p1
= plot_date(time,y,linestyle="-",marker="None",label="test")
axis
("tight")
title
("Random Data Against Time\n" * timespan)
grid
("on")
xlabel
("Time")
ylabel
("Stuff",fontdict=font1)
fig
[:autofmt_xdate](bottom=0.2,rotation=30,ha="right")
fig
[:canvas][:draw]() # Update the figure
PyPlot.tight_layout()

It would be much easier to have midnight be the date and every 4, 6, or 8 hours be time of day. I would be happy with just the dates part though.

Nat Wilson

unread,
Nov 26, 2014, 10:52:54 AM11/26/14
to julia...@googlegroups.com
You might try something like:

majorformatter = matplotlib[:dates][:DateFormatter]("%m/%d")
minorformatter = matplotlib[:dates][:DateFormatter]("%H:%M")
majorlocator = matplotlib[:dates][:DayLocator](interval=1)
minorlocator = matplotlib[:dates][:HourLocator](byhour=(8, 16))
ax1[:xaxis][:set_major_formatter](majorformatter)
ax1[:xaxis][:set_minor_formatter](minorformatter)
ax1[:xaxis][:set_major_locator](majorlocator)
ax1[:xaxis][:set_minor_locator](minorlocator)


See an example here:
http://matplotlib.org/examples/pylab_examples/date_demo2.html

Nat

RecentConvert

unread,
Nov 27, 2014, 3:06:38 AM11/27/14
to julia...@googlegroups.com
That's it! It should help with translating more things as well. It hadn't crossed my mind that I could access matplotlib in this manner.

# Julia 0.3.2
# 27.11.2014


using PyPlot
using Dates

# Create Data

dt
= Millisecond(100)
time
= [DateTime(2014,11,20):dt:DateTime(2014,11,22)]
y
= fill!(Array(Float64,length(time)),42)
#y = floor(100*rand(length(time))) # Fails unless the time span is very short


font1
= ["fontname"=>"Sans","style"=>"normal"]

time2
= float64(time)/1000/60/60/24 # Convert time from milliseconds from day 0 to days from day 0
timespan
= "\n" * Dates.format(minimum(time),"yyyy-mm-dd HH:MM:SS") * " - " * Dates.format(maximum(time),"yyyy-mm-dd HH:MM:SS")

majorformatter
= matplotlib[:dates][:DateFormatter]("%d.%m.%Y")

minorformatter
= matplotlib[:dates][:DateFormatter]("%H:%M")
majorlocator
= matplotlib[:dates][:DayLocator](interval=1)
minorlocator
= matplotlib[:dates][:HourLocator](byhour=(8, 16))

# Plot
fig
= figure("Test Plot",figsize=(16,10)) # Create a figure and save the handle
ax1
= axes()

p1
= plot_date(time2,y,linestyle="-",marker="None",label="test")

axis
("tight")
title
("Random Data Against Time\n" * timespan)
grid
("on")
xlabel
("Time")
ylabel
("Stuff",fontdict=font1)

ax1
[:xaxis][:set_major_formatter](majorformatter)
ax1
[:xaxis][:set_minor_formatter](minorformatter)
ax1
[:xaxis][:set_major_locator](majorlocator)
ax1
[:xaxis][:set_minor_locator](minorlocator)
Reply all
Reply to author
Forward
0 new messages