set fixed xrange

Hi there,

I'm using matplotlib (version 0.98.5.2) to generate a couple of bar-charts for a website.
Until now I've not been able to find a way set the x axis to a fixed range.

Suppose my data is turnover per hour. Now: If there is zero turnover at the beginning
or the end of a period matplotlib omits that value and limits the xrange so that non-zero
hours reside on the edges which would be confusing for the users.

This is the code:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(5,4),dpi=72)
ax = fig.add_subplot(111)
ax.set_ylabel('EUR')
ax.set_xlabel('Hour')
ax.set_xlim((1,24))
ax.bar(data.keys(),data.values(),width=1,align='center')
fig.savefig(filename)

I searched the docs but I didn't find anything that could fix my problem.
It would be great if anybody knew (and told me) what I am doing wrong.

Thanks in advance

Till Backhaus

Try moving the call to ax.set_xlim to after the call to ax.bar.

Ryan

···

On Thu, Apr 23, 2009 at 8:40 AM, Till Backhaus <till@…2568…> wrote:

Hi there,

I’m using matplotlib (version 0.98.5.2) to generate a couple of bar-

charts for a website.

Until now I’ve not been able to find a way set the x axis to a fixed

range.

Suppose my data is turnover per hour. Now: If there is zero turnover

at the beginning

or the end of a period matplotlib omits that value and limits the

xrange so that non-zero

hours reside on the edges which would be confusing for the users.

This is the code:

import matplotlib

matplotlib.use(‘Agg’)

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(5,4),dpi=72)

ax = fig.add_subplot(111)

ax.set_ylabel(‘EUR’)

ax.set_xlabel(‘Hour’)

ax.set_xlim((1,24))

ax.bar(data.keys(),data.values(),width=1,align=‘center’)

fig.savefig(filename)

Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
Sent from Norman, Oklahoma, United States