views:

46

answers:

2

Hi guys!

So I've this image : alt text

What I trying to do if to leave 'H37Rv' only in the purple bar.

My code is the following:

rects = ax.bar(ind, num, width, color=colors)

    for rect in rects:
        height = int(rect.get_height())

        if height < 5:
            yloc = height + 2
            clr = '#182866'
        else:
            yloc = height / 2.0
            clr = '#182866'

        p = 'H37Rv'
        xloc = rect.get_x() + (rect.get_width() / 2.0)
        ax.text(xloc, yloc, p, horizontalalignment='center', verticalalignment='center', color=clr, weight='bold')

I also tried this:

for rect in rects:
        if color == purple:
            height = int(rect.get_height())

            if height < 5:
                yloc = height + 2
                clr = '#182866'

but I get an error saying color is not defined.

Anyone has any idea how to solve this?

Thanks a lot!

+2  A: 

If you move the last three lines of your first example in one indent level, so they are part of the "else" clause that sets the colour to purple, that should do it.

[Edit: Sorry, I misread slightly. That would also leave the text in the 2nd bar. There's no way to get the colour of a rectangle as far as I know, but you could do:

rects = ax.bar(ind, num, width, color=colors)

rect = rects[-1]
height = int(rect.get_height())

if height < 5:
    yloc = height + 2
else:
    yloc = height / 2.0

clr = '#182866'
p = 'H37Rv'
xloc = rect.get_x() + (rect.get_width() / 2.0)
ax.text(xloc, yloc, p, horizontalalignment='center', verticalalignment='center', color=clr, weight='bold')

That would set the text in the last bar only.

If it could be any bar that might be purple, not necessarily the last one, well, you've got the list of colours you initialised the rectangles with, so:

rects = ax.bar(ind, num, width, color=colors)

for i in range(len(colors):
    if colors[i] == purple: # or however you specified "purple" in your colors list
       labelled_rects.append(i)

for i in labelled_rects:
    rect = rects[i]
    height = int(rect.get_height())

    if height < 5:
        yloc = height + 2
    else:
        yloc = height / 2.0

    clr = '#182866'
    p = 'H37Rv'
    xloc = rect.get_x() + (rect.get_width() / 2.0)
    ax.text(xloc, yloc, p, horizontalalignment='center', verticalalignment='center', color=clr, weight='bold')
Vicky
I'm not setting the color to purple in this example. if you're talking about this: clr = '#182866'then this is the dark blue we see in the letters, and its actually the same. the purple is inside a list colors, because it's a graph that changes depending on the request. NOt sure is I was clear enough..
Pat
@Pat: I've edited my answer, hope that helps
Vicky
thanks! I was trying to do something similar with @EOL example but yours it's also quite easy. :)
Pat
+2  A: 

You can get the color of a rectangle with rect.get_facecolor(), which allows you to place the label the way you want.

Alternatively, since you know which colors you used when drawing the bar plot, and if they are represented by a list, you can indeed easily get the list of purple rectangles.

EOL
thanks! I was looking for something like this, but @Vicky and also yours suggestion also does the trick :)
Pat