Ask Your Question
1

Why specifying `stylesheet` parameter in plot function causes error?

asked 2022-02-13 12:44:24 +0200

SageUserNick gravatar image

updated 2022-02-13 12:45:42 +0200

I'm disappointed by how the default plot style looks, so I decided to find some fancier stylesheet. The official documentation says:

MATPLOTLIB STYLE SHEET OPTION:

stylesheet - (Default: classic) Support for loading a full matplotlib style sheet. Any style sheet listed in matplotlib.pyplot.style.available is acceptable. If a non-existing style is provided the default classic is applied.

When I try it with a simple example, I get

plot(sin, stylesheet="ggplot")
...
RuntimeError: Error in line(): option 'stylesheet' not valid.

However I found that this alternative works:

plot(sin).show(stylesheet="ggplot")

This is ugly, because I use IPython CLI and want to be able to show the plot by this syntax

p = plot(sin)
p

without typing .show(). Moreover, I'd like to configure my .sage/init.sage file so that the 'ggplot' stylesheet is used by default:

# .sage/init.sage
plot.options["stylesheet"] = "ggplot"

This doesn't work though, for sage substitutes this option into line() function which appears to have no such option.

Questions:

  1. Is this a bug, or a mistake in (my understanding of) the documentation? Should it be reported and if yes, where is the best place to do it?

  2. How do I achieve custom plotting style by default (perhaps by some setting in the init.sage file)?

I'm using SageMath version 9.5, Release Date: 2022-01-30 with Python 3.10.2, all downloaded from ArchLinux official repos.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2022-02-13 18:40:51 +0200

Emmanuel Charpentier gravatar image

Possible workaround :

sage: from matplotlib.pyplot import xkcd as mpx
sage: with mpx(): plot(sin, (-pi, pi))

with mpx(): creates a context manager where the effects of mpx apply ; this avoids setting a permanent switch toxkcd style that mpx() would triggere (and which I do not (yet) know how to reverse).

HTH (but doubting it...),

edit flag offensive delete link more

Comments

Thank you for an answer. That's a good workaround, but unfortunately still requires too much typing. Also, I can't figure out if it's possible to import ggplot (or other non-comical) style this way.

SageUserNick gravatar imageSageUserNick ( 2022-02-14 21:31:04 +0200 )edit

I can't figure out if it's possible to import ggplot (or other non-comical) style this way

See mwageringel's answer...

That's a good workaround, but unfortunately still requires too much typing.

Complain fiercely.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2022-02-15 04:55:10 +0200 )edit
2

answered 2022-02-14 08:32:36 +0200

mwageringel gravatar image

To answer your second question, you can put this in your init.sage file:

import matplotlib.style
matplotlib.style.use("ggplot")

However, some plots might not look good with a style different from the default.

edit flag offensive delete link more

Comments

Thanks, that helps.

SageUserNick gravatar imageSageUserNick ( 2022-02-14 21:34:58 +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

2 followers

Stats

Asked: 2022-02-13 12:44:24 +0200

Seen: 49 times

Last updated: Feb 14