Ask Your Question

Axes and label colors in plot

asked 2020-11-29 10:07:58 +0200

Cyrille gravatar image

As I use a blackboard background, when plotting a function or anything else, through transparency. I need to work with white axes and white labels. How to change the colors of the axes and of the different labels. Incidently, is there a way to change the color of an output ?

edit retag flag offensive close merge delete


Using x^2 and sin as toy examples, since no example was provided in the question.

slelievre gravatar imageslelievre ( 2020-12-02 17:33:24 +0200 )edit

By "change the color of an output", do you mean change the line color of a function graph, or other graphics object, after it has been plotted?

For example, having defining p = plot(x^2) which by default is plotted in blue, is the question about changing the plot's color from blue to another color without starting from scratch?

slelievre gravatar imageslelievre ( 2020-12-02 18:08:43 +0200 )edit

In the answer, the axes color is changed to blue, as white would be hard to see on Ask Sage's white background.

slelievre gravatar imageslelievre ( 2020-12-02 18:10:38 +0200 )edit

slelievre I have forgoten to ask for the color of the axes labels.

Cyrille gravatar imageCyrille ( 2020-12-05 22:22:23 +0200 )edit

Edited answer to recall how to explore available methods. Also added how to change axes label colors.

Easy to guess though... Since axes color is changed with p.axes_color('beige') and tick label color is changed with p.tick_label_color('beige') one might guess that axes label color is changed with p.axes_label_color('beige').

In case of doubt type the beginning of a guess then TAB, for example: then TAB reveals methods axes, axes_color, axes_label_color, axes_labels, axes_labels_size, axes_range, axes_width.

slelievre gravatar imageslelievre ( 2020-12-07 02:27:19 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-12-02 17:31:19 +0200

slelievre gravatar image

updated 2020-12-07 02:30:01 +0200

Graphics objects have methods to get or set many of their settings.

Among these methods,

  • the method axes_color lets you get or set the axes color
  • the method axes_label_color lets you get or set the axes label color
  • the method tick_label_color lets you get or set the tick label color

Let us illustrate them on an example.

Create a graph with transparent background and axes labels:

sage: p = plot(x^2, color='orange', transparent=True, axes_labels=('$x$', '$y$'))

To know what methods exist for p, explore using TAB completion: type p. then press the TAB key.

This reveals, among other methods, axes_color, axes_label_color, tick_label_color.

Explore the documentation with ?, for instance p.axes_color? and find out that, if called with no argument, it gets the current setting of axes color, and if called with an argument, it sets the axes color to that value.

Check what the axes color, axes label color, and tick label color are set to:

sage: p.axes_color(), p.axes_label_color(), p.tick_label_color()
((0, 0, 0), (0, 0, 0), (0, 0, 0))

They are all black. Change them:

sage: p.axes_color('steelblue')
sage: p.axes_label_color('steelblue')
sage: p.tick_label_color('steelblue')

View the graph with its new color for axes, axes labels, and tick labels:

sage:, figsize=5)

SageMath plot with transparent background and changed axes color

The axes are, as usual, displayed on top of the graph, as if the graph was behind or under them.

To have the graph on top of the axes instead, set the z-order when plotting.

sage: opt = {'ticks': (pi/2, 1), 'tick_formatter': (pi, None), 'transparent': True}
sage: q = plot(sin, (-7, 7), axes_labels=('$t$', '$z$'), color='orange', zorder=3, **opt)
sage: q.axes_color('steelblue')
sage: q.axes_label_color('steelblue')
sage: q.tick_label_color('steelblue')
sage:, xmax=6.28, aspect_ratio=1)

SageMath plot with transparency and changed axes color

edit flag offensive delete link more


Thank you for this answer

LexMath gravatar imageLexMath ( 2022-08-15 20:39:24 +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: 2020-11-29 10:07:58 +0200

Seen: 1,108 times

Last updated: Dec 07 '20