From: gsal <sal...@gm...> - 2013-01-04 16:41:10
|
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. |
From: Paul H. <pmh...@gm...> - 2013-01-04 17:41:50
|
On Fri, Jan 4, 2013 at 8:41 AM, gsal <sal...@gm...> 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 > > To fake a legend, try using so-called proxy artists: http://matplotlib.org/users/legend_guide.html#using-proxy-artist -paul |
From: gsal <sal...@gm...> - 2013-01-04 21:22:45
|
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. |
From: Paul H. <pmh...@gm...> - 2013-01-04 22:24:10
|
[Forgot to reply-all, sorry for the dup, gsal] On Fri, Jan 4, 2013 at 1:22 PM, gsal <sal...@gm...> wrote: > 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?). > 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 |
From: gsal <sal...@gm...> - 2013-01-04 23:02:55
|
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. |