ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 21 Nov 2011 14:18:48 -0600Piecewise functions and legend labelhttp://ask.sagemath.org/question/8495/piecewise-functions-and-legend-label/If I plot a piecewise function with a legend label like this:
plot(Piecewise([[(0,1),x],[(1,2),x^2]]),legend_label='f(x)')
I get the legend twice. Is there a way to ensure that it appears only one time, i.e. one legend for the whole function?Sat, 19 Nov 2011 10:37:47 -0600http://ask.sagemath.org/question/8495/piecewise-functions-and-legend-label/Answer by DSM for <p>If I plot a piecewise function with a legend label like this:</p>
<pre><code>plot(Piecewise([[(0,1),x],[(1,2),x^2]]),legend_label='f(x)')
</code></pre>
<p>I get the legend twice. Is there a way to ensure that it appears only one time, i.e. one legend for the whole function?</p>
http://ask.sagemath.org/question/8495/piecewise-functions-and-legend-label/?answer=12927#post-id-12927Here's a slightly different (banana-themed) workaround. Run the following before making the plot:
# monkeypatch legend duplication of trac #11225
def fix_piecewise(fn):
import types
def fixed_plot(self, *args, **kwargs):
from sage.plot.all import plot
return sum([plot(f, a, b, *args, **(dict((k,v) for k,v in kwargs.items() if i == 0 or k != 'legend_label')))
for i, ((a,b),f) in enumerate(self.list())])
def fn2(*args, **kwargs):
ans = fn(*args, **kwargs)
ans.plot = types.MethodType(fixed_plot, ans)
return ans
return fn2
Piecewise = fix_piecewise(Piecewise)
after which it should behave.
Mon, 21 Nov 2011 14:18:48 -0600http://ask.sagemath.org/question/8495/piecewise-functions-and-legend-label/?answer=12927#post-id-12927Answer by kcrisman for <p>If I plot a piecewise function with a legend label like this:</p>
<pre><code>plot(Piecewise([[(0,1),x],[(1,2),x^2]]),legend_label='f(x)')
</code></pre>
<p>I get the legend twice. Is there a way to ensure that it appears only one time, i.e. one legend for the whole function?</p>
http://ask.sagemath.org/question/8495/piecewise-functions-and-legend-label/?answer=12923#post-id-12923This is a bug, I think. Here is the documentation for `Piecewise` plotting.
def plot(self, *args, **kwds):
"""
Returns the plot of self.
Keyword arguments are passed onto the plot command for each piece
of the function. E.g., the plot_points keyword affects each
segment of the plot.
So that includes the legend ones, which however, is *not* what you want, as opposed to some of the other options. I've added this to the [piecewise plotting bug ticket](http://trac.sagemath.org/sage_trac/ticket/11225#comment:6).
Update: Here is a workaround of sorts. Unfortunately, it means plotting each part separately, only labeling one of them.
sage: P = plot(Piecewise([[(0,1),x]]),legend_label='f(x)')
sage: Q = plot(Piecewise([[(1,2),x^2]]),legend_label='')
sage: R = plot(Piecewise([[(0,1),x^2]]),legend_label='g(x)',color='red')
sage: S = plot(Piecewise([[(1,2),x]]),legend_label='',color='red')
sage: P+Q+R+S
Or you could just skip the `legend_label` option totally on `Q` and `S`.Sat, 19 Nov 2011 13:58:45 -0600http://ask.sagemath.org/question/8495/piecewise-functions-and-legend-label/?answer=12923#post-id-12923Comment by sagefan for <p>This is a bug, I think. Here is the documentation for <code>Piecewise</code> plotting.</p>
<pre><code>def plot(self, *args, **kwds):
"""
Returns the plot of self.
Keyword arguments are passed onto the plot command for each piece
of the function. E.g., the plot_points keyword affects each
segment of the plot.
</code></pre>
<p>So that includes the legend ones, which however, is <em>not</em> what you want, as opposed to some of the other options. I've added this to the <a href="http://trac.sagemath.org/sage_trac/ticket/11225#comment:6">piecewise plotting bug ticket</a>.</p>
<p>Update: Here is a workaround of sorts. Unfortunately, it means plotting each part separately, only labeling one of them.</p>
<pre><code>sage: P = plot(Piecewise([[(0,1),x]]),legend_label='f(x)')
sage: Q = plot(Piecewise([[(1,2),x^2]]),legend_label='')
sage: R = plot(Piecewise([[(0,1),x^2]]),legend_label='g(x)',color='red')
sage: S = plot(Piecewise([[(1,2),x]]),legend_label='',color='red')
sage: P+Q+R+S
</code></pre>
<p>Or you could just skip the <code>legend_label</code> option totally on <code>Q</code> and <code>S</code>.</p>
http://ask.sagemath.org/question/8495/piecewise-functions-and-legend-label/?comment=20853#post-id-20853Is there any workaround?Sat, 19 Nov 2011 20:24:03 -0600http://ask.sagemath.org/question/8495/piecewise-functions-and-legend-label/?comment=20853#post-id-20853