Two bugs when plotting date/time histograms

Hi!

Below is a sample program. It demonstrates two bugs when plotting date/time histograms.

1. When the y scale is made "log", the histogram points plot as lines, but when the y scale is not log, they histogram plots as bars. I do not think that the look of the bars should change depending on whether or not the Y scale is logarithmic.

2. When the "agg.pdf" is removed, specifying "log" for the yscale produces a TypeError on the mac (see below)

I would really like to know how to plot a histogram with real bars ,rather than little lines.

Thanks!

import matplotlib
matplotlib.use('agg.pdf')

if __name__=="__main__":
    import datetime
    import numpy as np
    import matplotlib
    import matplotlib.pyplot as pyplot
    import matplotlib.dates as mdates
    import matplotlib.mlab as mlab

    dates_and_counts = [[datetime.datetime(90,i,1), i*10] for i in range(1,13)]

    dates, counts = zip(*dates_and_counts)

    years = mdates.YearLocator() # every year
    months = mdates.MonthLocator() # every month
    yearsFmt = mdates.DateFormatter('%Y')

    fig = pyplot.figure()
    ax = fig.add_subplot(111)
    ax.bar(dates,counts)

    ax.set_ylabel('file count')
    ax.set_xlabel('file modification time (mtime)')

    ax.set_yscale('log')

    ax.xaxis.set_major_locator(years)
    ax.xaxis.set_major_formatter(yearsFmt)

    datemin = datetime.date(min(dates).year, 1, 1)
    datemax = datetime.date(max(dates).year, 1, 1)
    ax.set_xlim(datemin, datemax)
    ax.set_ylim(0,max(counts))

    # format the coords message box
    def price(x): return '$%1.2f'%x
    ax.format_xdata = mdates.DateFormatter('%Y-%m-%d')
    ax.format_ydata = price
    ax.grid(True)

    # rotates and right aligns the x labels, and moves the bottom of the
    # axes up to make room for them
    fig.autofmt_xdate()
    pyplot.savefig("hist.pdf",format='pdf')

12:12 PM m:~$ python f.py
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/figure.py", line 772, in draw
    for a in self.axes: a.draw(renderer)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/axes.py", line 1601, in draw
    a.draw(renderer)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/axis.py", line 710, in draw
    tick.draw(renderer)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/axis.py", line 193, in draw
    self.label1.draw(renderer)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/text.py", line 502, in draw
    ismath=ismath)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/backends/backend_macosx.py", line 120, in draw_text
    self._draw_mathtext(gc, x, y, s, prop, angle)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/backends/backend_macosx.py", line 112, in _draw_mathtext
    gc.draw_mathtext(x, y, angle, 255 - image.as_array())
TypeError: image has incorrect type (should be uint8)
12:12 PM m:~$ %
V/r,

Simson Garfinkel

Simson Garfinkel wrote:

import matplotlib
matplotlib.use('agg.pdf')

Simson,

Sorry to be addressing a side point, not your real questions, but your example shows up a bug in matplotlib.use, which I will fix shortly. There is an agg backend, and there is a pdf backend, but there is no agg.pdf. You are simply getting the agg backend, and when you save the figure you are using the pdf backend, which is fine. Only the cairo backend has "sub-backends" or versions, like "cairo.pdf" or "cairo.png" or "cairo.ps" or "cairo.svg".

Eric

Eric,

Thanks for addressing the side-issue. If there were a simple way to list all of the backends, that might help?

Any idea about the main point?

···

On Jan 7, 2009, at 12:52 PM, Eric Firing wrote:

Simson Garfinkel wrote:

import matplotlib
matplotlib.use('agg.pdf')

Simson,

Sorry to be addressing a side point, not your real questions, but your example shows up a bug in matplotlib.use, which I will fix shortly. There is an agg backend, and there is a pdf backend, but there is no agg.pdf. You are simply getting the agg backend, and when you save the figure you are using the pdf backend, which is fine. Only the cairo backend has "sub-backends" or versions, like "cairo.pdf" or "cairo.png" or "cairo.ps" or "cairo.svg".

Eric

Simson Garfinkel wrote:

Hi!

Below is a sample program. It demonstrates two bugs when plotting date/ time histograms.

1. When the y scale is made "log", the histogram points plot as lines, but when the y scale is not log, they histogram plots as bars. I do not think that the look of the bars should change depending on whether or not the Y scale is logarithmic.

Simson,

I verified the strange behavior with log and/or date, but looking at the code did not yield any understanding of what the problems are. I hope someone who has worked on the bar code recently will sort this one out. Definitely, there is at least one major bug that needs to be fixed.

2. When the "agg.pdf" is removed, specifying "log" for the yscale produces a TypeError on the mac (see below)

This is mac-specific, and I don't have a mac, so I can't help with this, either.

Eric

···

I would really like to know how to plot a histogram with real bars ,rather than little lines.

Thanks!

import matplotlib
matplotlib.use('agg.pdf')

if __name__=="__main__":
    import datetime
    import numpy as np
    import matplotlib
    import matplotlib.pyplot as pyplot
    import matplotlib.dates as mdates
    import matplotlib.mlab as mlab

    dates_and_counts = [[datetime.datetime(90,i,1), i*10] for i in range(1,13)]

    dates, counts = zip(*dates_and_counts)

    years = mdates.YearLocator() # every year
    months = mdates.MonthLocator() # every month
    yearsFmt = mdates.DateFormatter('%Y')

    fig = pyplot.figure()
    ax = fig.add_subplot(111)
    ax.bar(dates,counts)

    ax.set_ylabel('file count')
    ax.set_xlabel('file modification time (mtime)')

    ax.set_yscale('log')

    ax.xaxis.set_major_locator(years)
    ax.xaxis.set_major_formatter(yearsFmt)

    datemin = datetime.date(min(dates).year, 1, 1)
    datemax = datetime.date(max(dates).year, 1, 1)
    ax.set_xlim(datemin, datemax)
    ax.set_ylim(0,max(counts))

    # format the coords message box
    def price(x): return '$%1.2f'%x
    ax.format_xdata = mdates.DateFormatter('%Y-%m-%d')
    ax.format_ydata = price
    ax.grid(True)

    # rotates and right aligns the x labels, and moves the bottom of the
    # axes up to make room for them
    fig.autofmt_xdate()
    pyplot.savefig("hist.pdf",format='pdf')

12:12 PM m:~$ python f.py
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/site-packages/matplotlib/figure.py", line 772, in draw
    for a in self.axes: a.draw(renderer)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/site-packages/matplotlib/axes.py", line 1601, in draw
    a.draw(renderer)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/site-packages/matplotlib/axis.py", line 710, in draw
    tick.draw(renderer)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/site-packages/matplotlib/axis.py", line 193, in draw
    self.label1.draw(renderer)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/site-packages/matplotlib/text.py", line 502, in draw
    ismath=ismath)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/site-packages/matplotlib/backends/backend_macosx.py", line 120, in draw_text
    self._draw_mathtext(gc, x, y, s, prop, angle)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/site-packages/matplotlib/backends/backend_macosx.py", line 112, in _draw_mathtext
    gc.draw_mathtext(x, y, angle, 255 - image.as_array())
TypeError: image has incorrect type (should be uint8)
12:12 PM m:~$ %
V/r,

Simson Garfinkel

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Thanks, Eric. Any idea for a work-around on the bar graphs?

As far as the Mac goes, I'm happy to get you a log-in on one, if you want.

-Simson

···

On Jan 8, 2009, at 2:31 PM, Eric Firing wrote:

Simson Garfinkel wrote:

Hi!
Below is a sample program. It demonstrates two bugs when plotting date/ time histograms.
1. When the y scale is made "log", the histogram points plot as lines, but when the y scale is not log, they histogram plots as bars. I do not think that the look of the bars should change depending on whether or not the Y scale is logarithmic.

Simson,

I verified the strange behavior with log and/or date, but looking at the code did not yield any understanding of what the problems are. I hope someone who has worked on the bar code recently will sort this one out. Definitely, there is at least one major bug that needs to be fixed.

2. When the "agg.pdf" is removed, specifying "log" for the yscale produces a TypeError on the mac (see below)

This is mac-specific, and I don't have a mac, so I can't help with this, either.

Simson Garfinkel wrote:

Thanks, Eric. Any idea for a work-around on the bar graphs?

There appear to be three problems, each with a workaround or solution:

1) You need to set the log scale *before* calling bar. The bar method checks for log scaling, and if found, it sets the bottom of the bars to a positive value (1e-100) instead of to zero. If you set the log scale *after* calling bar, the zero bottom value gets masked out as invalid.

2) The unit support for datetime objects doesn't quite know what to do with the bar width parameter; it tries to convert it, and I haven't tried to track down exactly what it ends up with. What I have found is that if you use a value of width=20 as a kwarg in bar, you will get close enough that you can make more adjustments to taste. This is an ugly hack.

3) The datetime objects want full years, e.g. 1990, not just the last two digits. I haven't tried to figure out why, but the x-limits don't get calculated sensibly if you use 90 instead of 1990. It presumably has to do with the ticker that is invoked for datetime. So I think you need to either make your own modification of the ticker (or formatter), or use all 4 year digits.

As far as the Mac goes, I'm happy to get you a log-in on one, if you want.

Thanks, but I really don't want to try to delve into the brand-new mac native backend.

Eric

···

-Simson

On Jan 8, 2009, at 2:31 PM, Eric Firing wrote:

Simson Garfinkel wrote:

Hi!
Below is a sample program. It demonstrates two bugs when plotting date/ time histograms.
1. When the y scale is made "log", the histogram points plot as lines, but when the y scale is not log, they histogram plots as bars. I do not think that the look of the bars should change depending on whether or not the Y scale is logarithmic.

Simson,

I verified the strange behavior with log and/or date, but looking at the code did not yield any understanding of what the problems are. I hope someone who has worked on the bar code recently will sort this one out. Definitely, there is at least one major bug that needs to be fixed.

2. When the "agg.pdf" is removed, specifying "log" for the yscale produces a TypeError on the mac (see below)

This is mac-specific, and I don't have a mac, so I can't help with this, either.

Eric Firing wrote:

Simson Garfinkel wrote:

Thanks, Eric. Any idea for a work-around on the bar graphs?

There appear to be three problems, each with a workaround or solution:

1) You need to set the log scale *before* calling bar. The bar method checks for log scaling, and if found, it sets the bottom of the bars to a positive value (1e-100) instead of to zero. If you set the log scale *after* calling bar, the zero bottom value gets masked out as invalid.

2) The unit support for datetime objects doesn't quite know what to do with the bar width parameter; it tries to convert it, and I haven't tried to track down exactly what it ends up with. What I have found is that if you use a value of width=20 as a kwarg in bar, you will get close enough that you can make more adjustments to taste. This is an ugly hack.

Correction: the unit conversion is perfectly sensible. It sees the width as a simple number and leaves it alone, so it is interpreted as being in the units that the datetime gets converted to: days.

Eric

···

3) The datetime objects want full years, e.g. 1990, not just the last two digits. I haven't tried to figure out why, but the x-limits don't get calculated sensibly if you use 90 instead of 1990. It presumably has to do with the ticker that is invoked for datetime. So I think you need to either make your own modification of the ticker (or formatter), or use all 4 year digits.

As far as the Mac goes, I'm happy to get you a log-in on one, if you want.

Thanks, but I really don't want to try to delve into the brand-new mac native backend.

Eric

-Simson

On Jan 8, 2009, at 2:31 PM, Eric Firing wrote:

Simson Garfinkel wrote:

Hi!
Below is a sample program. It demonstrates two bugs when plotting date/ time histograms.
1. When the y scale is made "log", the histogram points plot as lines, but when the y scale is not log, they histogram plots as bars. I do not think that the look of the bars should change depending on whether or not the Y scale is logarithmic.

Simson,

I verified the strange behavior with log and/or date, but looking at the code did not yield any understanding of what the problems are. I hope someone who has worked on the bar code recently will sort this one out. Definitely, there is at least one major bug that needs to be fixed.

2. When the "agg.pdf" is removed, specifying "log" for the yscale produces a TypeError on the mac (see below)

This is mac-specific, and I don't have a mac, so I can't help with this, either.

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Wow, Eric. That's a lot of stuff! Thanks for looking into this to me.
It would probably be useful to have a warning message or something if there are 0 values for the log axes.
What do you think?

···

On Jan 8, 2009, at 11:03 PM, Eric Firing wrote:

Simson Garfinkel wrote:

Thanks, Eric. Any idea for a work-around on the bar graphs?

There appear to be three problems, each with a workaround or solution:

1) You need to set the log scale *before* calling bar. The bar method checks for log scaling, and if found, it sets the bottom of the bars to a positive value (1e-100) instead of to zero. If you set the log scale *after* calling bar, the zero bottom value gets masked out as invalid.

2) The unit support for datetime objects doesn't quite know what to do with the bar width parameter; it tries to convert it, and I haven't tried to track down exactly what it ends up with. What I have found is that if you use a value of width=20 as a kwarg in bar, you will get close enough that you can make more adjustments to taste. This is an ugly hack.

3) The datetime objects want full years, e.g. 1990, not just the last two digits. I haven't tried to figure out why, but the x-limits don't get calculated sensibly if you use 90 instead of 1990. It presumably has to do with the ticker that is invoked for datetime. So I think you need to either make your own modification of the ticker (or formatter), or use all 4 year digits.

As far as the Mac goes, I'm happy to get you a log-in on one, if you want.

Thanks, but I really don't want to try to delve into the brand-new mac native backend.

Eric

-Simson
On Jan 8, 2009, at 2:31 PM, Eric Firing wrote:

Simson Garfinkel wrote:

Hi!
Below is a sample program. It demonstrates two bugs when plotting date/ time histograms.
1. When the y scale is made "log", the histogram points plot as lines, but when the y scale is not log, they histogram plots as bars. I do not think that the look of the bars should change depending on whether or not the Y scale is logarithmic.

Simson,

I verified the strange behavior with log and/or date, but looking at the code did not yield any understanding of what the problems are. I hope someone who has worked on the bar code recently will sort this one out. Definitely, there is at least one major bug that needs to be fixed.

2. When the "agg.pdf" is removed, specifying "log" for the yscale produces a TypeError on the mac (see below)

This is mac-specific, and I don't have a mac, so I can't help with this, either.