Strange color handling in bar() and barh()

# Should show orange bars, but shows white, grey, and

    > black bars. from matplotlib.pylab import * figure() t =
    > [1,2,3] bar(t, t, color=(1.0, 0.5, 0.0)) show()

    > The culprit is len(left)!=3 in bar() and barh():

    > if (is_string_like(color) or (iterable(color) and
    > len(color)==3 and len(left)!=3) or not iterable(color)):
    > color = [color]*len(left)

    > Is this surprising behavior really what you want?

This is a corner case. matplotlib is pretty friendly when you set
colors. In particular, it allows you to use a float and it interprets
it as grayscale or an rgb tuple. What you are bumping into is the
unusual case when the length of your data is exactly three, and it is
ambiguous whether you want three grayscale values, different for each
bar, or a single rgb value for all three bars. It wouldn't happen
with 10 bars because a 10 length tuple of grayscale floats is not
ambiguous.

You can disambiguate by using hex strings or colorname strings

  colors = 'white', 'gray', 'black'
  bar(t,t,color=colors)

Hope this helps,
JDH

Yes, and that is what I had to do with the colors I got from a
colormap:

    color = rgb2hex(colormap(x)[:3])

However, what I really would have hoped for is a color class so that I
wouldn't have to worry about rgb versus rgba and the problem above. For
instance, colormap(x) would return a color instance that I can pass to
bar(), and bar() would also understand rgba automatically.

Something like:

import colors

class Color:
    def __init__(self, color):
        self.color = colors.colorConverter.to_rgba(color)

    def __getitem__(self, i):
        return self.color[i]

    def to_hex(self):
        return colors.rgb2hex(self.color[:3])

    def to_rgb(self):
        return self.color[:3]

    def to_rgba(self):
        return self.color

    etc.

···

On Tue, 2005-05-17 at 10:55 -0500, John Hunter wrote:

    > # Should show orange bars, but shows white, grey, and
    > black bars. from matplotlib.pylab import * figure() t =
    > [1,2,3] bar(t, t, color=(1.0, 0.5, 0.0)) show()

    > Is this surprising behavior really what you want?

This is a corner case. matplotlib is pretty friendly when you set
colors. In particular, it allows you to use a float and it interprets
it as grayscale or an rgb tuple. What you are bumping into is the
unusual case when the length of your data is exactly three, and it is
ambiguous whether you want three grayscale values, different for each
bar, or a single rgb value for all three bars. It wouldn't happen
with 10 bars because a 10 length tuple of grayscale floats is not
ambiguous.

You can disambiguate by using hex strings or colorname strings

--
Patrik