Skip to content

utils.draw.setFigure

Class · Decorator · Context · Source

mdnc.utils.draw.setFigure(
    style=None, font_name=None, font_size=None, use_tex=None
)

A context decorator class, which is used for changing the figure's configurations locally for a specific function. Could be used by two different ways:

Example
1
2
3
4
5
@mdnc.utils.draw.setFigure(font_size=12)
def plot_curve():
    plot_figures1(...)
    plot_figures2(...)
    ...
1
2
3
4
with mdnc.utils.draw.setFigure(font_size=12):
    plot_figures1(...)
    plot_figures2(...)
    ...

Arguments

Requries

The following arguments would take effect only when they are configured explicitly.

Argument Type Description
style str or
dict or
Path or
list
The local stylesheet of the figure. The details could be found in matplotlib.style.context. We could also find some examples here.
font_name str The local font family name for the output figure. The specified font should be installed and available for any software.
font_size int The local font size for the output figure.
use_tex bool Whether to use LaTeX backend for the output figure. Recommend to enable it when drawing figures for a paper.
Warning

If you are using a not installed or not supported font as font_name, the context decorator would not raise an error, but only show some warnings. This behavior is the same as matplotlib.

Warning

In the above argument list, the latter argument would override the former argument. For example, if some style has already specified font_size, configuring the argument font_size would override the configuration in the stylesheet. Please pay attention to your desired configurations.

Examples

As decorator
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import numpy as np
import matplotlib.pyplot as plt
import mdnc

@mdnc.utils.draw.setFigure(style='classic', font_size=16, font_name='arial')
def plot_local_setting():
    t = np.linspace(-10, 10, 100)
    plt.plot(t, 1 / (1 + np.exp(-t)))
    plt.title('In the context, font: arial.')
    plt.show()

plot_local_setting()

As context
1
2
3
4
5
6
7
8
9
import numpy as np
import matplotlib.pyplot as plt
import mdnc

with mdnc.utils.draw.setFigure(style='classic', font_size=16, font_name='arial'):
    t = np.linspace(-10, 10, 100)
    plt.plot(t, 1 / (1 + np.exp(-t)))
    plt.title('In the context, font: arial.')
    plt.show()


Last update: March 14, 2021

Comments