legend on a plot with broken_barh()

So, it looks like broken_barh's do not show up on the legend...is there work
around for this?

Or,

Is there a way to fake a legend? A way to set legend to whatever I want?

Thanks,

gsal

···

--
View this message in context: http://matplotlib.1069221.n5.nabble.com/legend-on-a-plot-with-broken-barh-tp40145.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

To fake a legend, try using so-called proxy artists:
http://matplotlib.org/users/legend_guide.html#using-proxy-artist

-paul

···

On Fri, Jan 4, 2013 at 8:41 AM, gsal <salgerman@...287...> wrote:

So, it looks like broken_barh's do not show up on the legend...is there
work
around for this?

Or,

Is there a way to fake a legend? A way to set legend to whatever I want?

Thanks,

gsal

can you provide an example? The reference help is only two lines!

Given:
[code]
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.broken_barh([ (110, 30), (150, 10) ] , (10, 9), facecolors='b',
label='barh')

ax.set_xlim((0,200))
ax.set_ylim((0,50))

ax.legend()

plt.show()
[/code]

How do I import what in order to, say, create a plot
"plot([0.0],[0.0],'bs')" so that I can at least plot a marker of the same
color as my broken_barh so that when the legend is added, the correct icon
precedes the label?

I tried adding

pp = plt.plot([80],[40],'bs', label='proxy artist')

to the previous program, right before the legend command, but it actually
plots the marker, too.

Is there a way to import "plot" or "Line2D" or something so that I can
produce an artist that is NOT related to the plot and, hence, not plotted?
(is that what "proxy artist" means?).

···

--
View this message in context: http://matplotlib.1069221.n5.nabble.com/legend-on-a-plot-with-broken-barh-tp40145p40149.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

[Forgot to reply-all, sorry for the dup, gsal]

···

On Fri, Jan 4, 2013 at 1:22 PM, gsal <salgerman@…287…> wrote:

can you provide an example? The reference help is only two lines!

Given:


> import numpy as np
> 
> import matplotlib.pyplot as plt
> 
> 
> 
> fig = plt.figure()
> 
> ax = fig.add_subplot(111)
> 
> ax.broken_barh([ (110, 30), (150, 10) ] , (10, 9), facecolors='b',
> 
> label='barh')
> 
> 
> 
> ax.set_xlim((0,200))
> 
> ax.set_ylim((0,50))
> 
> 
> 
> ax.legend()
> 
> 
> 
> plt.show()
> 

How do I import what in order to, say, create a plot

“plot([0.0],[0.0],‘bs’)” so that I can at least plot a marker of the same

color as my broken_barh so that when the legend is added, the correct icon

precedes the label?

I tried adding

pp = plt.plot([80],[40],‘bs’, label=‘proxy artist’)

to the previous program, right before the legend command, but it actually

plots the marker, too.

Is there a way to import “plot” or “Line2D” or something so that I can

produce an artist that is NOT related to the plot and, hence, not plotted?

(is that what “proxy artist” means?).

Yes. Proxy artists are created in memory but never added to the axes object. Here’s an expanded version of the example:

import matplotlib.patches as mpatch

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

patch = mpatch.Rectangle((0, 0), 1, 1, fc=“r”)

ax.legend([patch], [“Proxy artist”])

plt.show()

So for your example, I’d do the following:

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.patches as mpatch

fig = plt.figure()

ax = fig.add_subplot(111)

ax.broken_barh([ (110, 30), (150, 10) ] , (10, 9), facecolors=‘b’, label=‘barh’)

ax.set_xlim((0,200))

ax.set_ylim((0,50))

fakeredbar = mpatch.Rectangle((0, 0), 1, 1, fc=“r”)

fakebluebar = mpatch.Rectangle((0, 0), 1, 1, fc=“b”)

ax.legend([fakeredbar, fakebluebar], [‘Red Data’, ‘Blue Data’])

plt.show()

Now to me, it seems very strange that broken_barh doesn’t generate any items in a legend. Not sure why that is but it seems like a bug.

Hope that helps,

-paul

Great, thank you very much...that did the trick.

I don't know why broken_barh does not come in the legend, either; maybe is
because of the ambiguity as to which color to use in the legend...a single
broken barh command can have several bars with different colors.

gsal

···

--
View this message in context: http://matplotlib.1069221.n5.nabble.com/legend-on-a-plot-with-broken-barh-tp40145p40151.html
Sent from the matplotlib - users mailing list archive at Nabble.com.