Ask Your Question

# plot sage Graphics() from jupyter notebok in CoCalc

I want to use sage as a python library from jupyter notebok in CoCalc. I have a problem with Sage plotting:

from sage.all import *
var('x')
plot(sin(x)).show()


This code outputs "Graphics object consisting of 1 graphics primitive".

Pyplot plotting works fine. There are a number of old posts with the similar problem ( ex. https://ask.sagemath.org/question/109...), some of them recommend changing matplotlib backend with %matplotlib inline which does not help. Is there a way to output sage Graphics() inline without using pyplot?

edit retag close merge delete

## Comments

To potential answerers: It seems like this might not be specific to CoCalc, but just in general Jupyter.

In your Jupyter worksheet, are you using the Sage kernel or the Python kernel?

You can select the "Kernel" menu and change kernel to use the SageMath kernel. This might solve your plotting issue. If not, can you make the worksheet public and link to it? This might help understand the problem.

My idea is to use Python kernel but import sage as a library (actually, only specific features, incl. plotting)

## 1 Answer

Sort by » oldest newest most voted

Here is something that works, although I don't view it as perfect.

from sage.all import *
from IPython.display import Image
var('x')
p=plot(sin(x),(x,-2*pi,2*pi))
p.save('tmp.png')
Image("tmp.png")

more

## Comments

Just for completeness - I include here pyplot solution which is also not perfect but at least does not require temporary files

import matplotlib.pyplot as plt
from sage.all import *
var('x')
Gr=plot(sin(x))
figu = plt.figure() # you may set the size here
main_plot = figu.add_axes((0.2,0.2,0.7,0.7))
Gr.matplotlib('a.svg', figure=figu, sub=main_plot)
figu.show()


## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2017-09-08 14:24:35 +0200

Seen: 953 times

Last updated: Sep 12 '17