Axes formatting

Hello, i am trying to make time series plots with the date on the x axis. The python code I managed to
work read a .csv file with entries like

Date-Time,T2am,T2
11-Fev-11-14:44:56,31.2,26.8
11-Fev-11-14:59:56,33,26.9
11-Fev-11-15:14:56,28.5,27…

Here is the main part of the code

datafile = (‘t2vst2.csv’)
print ‘loading’, datafile

times, temp1, temp2 = np.loadtxt(
datafile, delimiter=’,’,
converters={0:strpdate2num(’%d-%b-%y-%H:%M:%S’) },
skiprows=1, usecols=(0,1,2), unpack=True)

fig = figure()
ax = fig.add_subplot(111)
ax.plot_date(times, temp1, ‘r-’,linewidth=2.0)
ax.plot_date(times, temp2,
‘g-’,linewidth=2.0)
ax.autoscale_view()
ax.grid(‘True’)
ylabel(‘Temperatura em graus Celsius’)
fig.autofmt_xdate()
show()

Matplotlib chooses to plot starting on Feb 10 til Feb 20 when the data goes from
Feb 11 to Feb 17.

Does anybody know how to make the plot area start right at the
beginning of data and finish right at the end, so it spans the whole x axis?

Thanks in advance

···

2011/3/9 Luciano Fleischfresser <l_fle@...9...>:

[...]
Does anybody know how to make the plot area start right at the
beginning of data and finish right at the end, so it spans the whole x axis?

I think this should do the trick:

axes.autoscale(axis='x', tight='True')

Goyo

Hi Goyo,

Thanks a lot. tight=‘True’ worked fine in my autoscale_view(). It’s doing just what I needed.

I have to confess that object-oriented programming seems very counter-intuitive to me.
Hopefully it will come more naturally soon.

Along the same lines of my original query, this one I think is more obscure, so here it goes:

Same script, just another .csv file with the same format. This time around, the plot comes up
with UTC times for the labels on the x-axis. The first one came with the dates.

Does anybody know how to tell matplotlib to always put dates?

Here is the code again:

from matplotlib.dates import strpdate2num
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
import numpy as np
from pylab import

···

datafile = (‘t2vst2.csv’)
print ‘loading’, datafile

times, temp1, temp2 = np.loadtxt(
datafile, delimiter=‘,’,
converters={0:strpdate2num(‘%d-%b-%y-%H:%M:%S’) },
skiprows=1, usecols=(0,1,2), unpack=True)

fig = figure()
ax = fig.add_subplot(111)
ax.plot_date(times, temp1, ‘r-’,linewidth=2.0)
ax.plot_date(times, temp2, ‘g-’,linewidth=2.0)
ax.autoscale_view(tight=‘True’, scalex=True, scaley=False)
fig.autofmt_xdate()
show()

Thanks again.


From: Goyo <goyodiaz@…287…>
To: Luciano Fleischfresser <lfle@…3463…>
Cc: Luciano Fleischfresser <l_fle@…9…>; matplotlib-users@lists.sourceforge.net
Sent: Wed, March 9, 2011 5:59:19 PM
Subject: Re: [Matplotlib-users] Axes formatting

2011/3/9 Luciano Fleischfresser <l_fle@…9…>:

[…]
Does anybody know how to make the plot area start right at the
beginning of data and finish right at the end, so it spans the whole x axis?

I think this should do the trick:

axes.autoscale(axis=‘x’, tight=‘True’)

Goyo

2011/3/11 Luciano Fleischfresser <l_fle@...9...>:

[...]
I have to confess that object-oriented programming seems very
counter-intuitive to me.
Hopefully it will come more naturally soon.

This has nothing to do with OOP, you just need to know what
command/function/method does what you want. It happens that in
matplotlib plots are autoscaled by default but not tightly. You can
change this behavoir using autoscale. There is a functional version
pyplot.autoscale but for scripting is usually better the
axes.autoscale version because it make explicit in your code which
axes is affected. For interactive plotting, the functional version may
be more convenient.

Along the same lines of my original query, this one I think is more obscure,
so here it goes:

Same script, just another .csv file with the same format. This time around,
the plot comes up
with UTC times for the labels on the x-axis. The first one came with the
dates.

Does anybody know how to tell matplotlib to always put dates?

Matplotlib automatically choose a formatter for labels, based on the
interval to plot and the like. If you don't like it, use your own.
Maybe this:

import matplotlib.dates as mdates
formatter = mdates.DateFormatter('%d-%b-%y-%H:%M:%S')
axes.xaxis.set_major_formatter(formatter)

Goyo