Piecewise plot not smooth on Jupyter

51 views
Skip to first unread message

Simon Willerton

unread,
Jan 21, 2018, 12:01:40 PM1/21/18
to sage-support
Hi!

There seems to be a bug in the plotting in the Jupyter notebook.  The following piecewise defined function should be smooth at r=1 but the plot is not; the first part of the plot seems to be lifted slightly.  I noticed this in a more complicated example but this is a reasonably minimal case.


r = var('r')
plot(2*r - r^2, r, 0, 1) + plot(1, r, 1, 2)

This bug doesn't seem to occur in CoCalc .sagews notebook but does occur in SageMath Kernels 7.6, 8.0, 8.1 in a CoCalc Jupyter notebook and offline on my Mac OS X Sierra in a Jupyter notebook.

Cheers,

Simon.

William Stein

unread,
Jan 21, 2018, 2:02:24 PM1/21/18
to sage-s...@googlegroups.com
It could be an issue with svg versus png output.



Cheers,

Simon.

--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
To post to this group, send email to sage-s...@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.
--
-- William Stein

Simon Willerton

unread,
Jan 21, 2018, 7:07:07 PM1/21/18
to sage-support
Indeed, that seems to be the problem.  Comparing the output from the following code gives the svg as smooth but the png not smooth.

p = plot(2*r- r^2, r, 0,1) + plot(1, r, 1, 2)
p.save("plot.svg")
p.save("plot.png")
 

Simon Willerton

unread,
Jan 22, 2018, 6:06:42 AM1/22/18
to sage-support
Following William's hint, I came up with a workaround.  The first solution I found at https://stackoverflow.com/a/45664729 didn't work on .sagews files, so combining that with https://stackoverflow.com/a/44100805, I ended up with the following.  Do tell me if there's a better way!


from IPython.display import SVG, display

def svg_show(pic, *args, **kwds):
    """
    Display the plot 'pic' using svg (rather than png).  If we are in Jupyter (ie. IPython)
    we need to do this explicitly.  Otherwise we can use the default show() function.
    """
    try:
        get_ipython
        pic.save('temp.svg', *args, **kwds)
        display(SVG(filename='temp.svg'))
    except:
        show(pic, *args, **kwds)


        
r = var('r')     
svg_show(plot(2*r- r^2, r, 0,1) + plot(1, r, 1, 2), ymin=0.5)

Volker Braun

unread,
Jan 24, 2018, 4:20:43 PM1/24/18
to sage-support
The easiest workaround is to switch to vector-graphics output:

%display graphics vector
Reply all
Reply to author
Forward
0 new messages