Ask Your Question

How to obtain high quality plots in the Jupyter notebook?

asked 2019-11-01 09:00:21 +0200

Tofi gravatar image

When plotting figures in the Jupyter notebook, the result is always a grained and low resolution image. With Python's matplotlib, this issue can be mitigated using the following command:

%config InlineBackend.figure_format = 'svg'

which makes matplotlib.pylplot.plot produce very high resolution figures in the notebook. Is there a way to obtain the same result with SageMath's plot command?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2019-11-01 16:23:29 +0200

Tofi gravatar image

updated 2019-11-01 16:28:07 +0200

So here is what I found after doing some research. It is not a perfect solution, but it gets the job done.

Figures appear to have low resolution in Jupyter because I am zooming into the notebook's page in my browser. At 100% zoom level, figures appear clear in the notebook, though I have the zoom level set to 150%, and the figures are scaled up in size without a change in resolution, so they become blurry. The problem is that at 100% zoom, the font size in the notebook becomes so small that it is unreadable (I think this is a problem for any laptop with 1080p screen resolution or higher).

The solution I found is to keep zoom level at 100% and to change the font size using Jupyter theme settings. Install Jupyter themes, then switch to a custom theme with large font size by running the following command in the terminal:

jt -t monokai -f roboto -fs 16 -ofs 16 -cellw 95% -N

Here is what each term means:

  • -t monokai sets the theme to monokai
  • -f roboto sets the font type to roboto
  • -fs 16 sets the input cell font size to 16
  • -ofs 16 sets the output cell font size to 16
  • -cellw 95% sets the cell width to 95% of the page's width
  • -N sets the notebook's name to visible. You can omit this flag if you don't want to the Notebook's title at the top of the page.

Now the font size looks great, and the figures are clear, except that they are small. We can increase the size of the figures and their font size using the figsize and fontsize optional arguments of the plot command. For example:

plot(sin(x), (x,0,2*pi), figsize=10, fontsize=16)

produces a large and clear figure with a readable font.

The only downside to this solution is that I have to include figsize=10, fontsize=16 in every plot command I use.

edit flag offensive delete link more


For drawing graphs, there is a way to change the default plot and show options:

sage: from sage.graphs.graph_plot import DEFAULT_PLOT_OPTIONS
sage: from sage.graphs.graph_plot import DEFAULT_SHOW_OPTIONS
{'figsize': [4, 4]}
sage: DEFAULT_SHOW_OPTIONS['figsize'] = [10,10]
sage: graphs.PetersenGraph().show()
Launched png viewer for Graphics object consisting of 26 graphics primitives

But it does not work for plot unless someone wants to work on it.

Sébastien gravatar imageSébastien ( 2019-11-04 23:00:33 +0200 )edit

Your Answer

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

Add Answer

Question Tools



Asked: 2019-11-01 09:00:21 +0200

Seen: 5,304 times

Last updated: Nov 01 '19