mpl_preferences¶
- pyhelpers.settings.mpl_preferences(reset=False, backend=None, font_name='Times New Roman', font_size=13, legend_spacing=0.7, fig_style=None)[source]¶
Alter some Matplotlib parameters for plotting.
This function allows customising various Matplotlib parameters such as backend, font settings, legend spacing and figure style.
- Parameters:
reset (bool) – Whether to reset all parameters to their default settings; defaults to
False
.backend (str | None) – Specify the backend used for rendering and GUI integration; defaults to
None
.font_name (str | None) – Name of the font to be used; defaults to
'Times New Roman'
.font_size (int | float) – Font size; defaults to
13
.legend_spacing (float | int) – Spacing between labels in the plot legend; defaults to
0.7
.fig_style (str | None) – Style of the figure; defaults to
None
.
Examples:
>>> import numpy as np >>> import matplotlib >>> matplotlib.use('TkAgg') >>> import matplotlib.pyplot as plt >>> np.random.seed(0) >>> random_array = np.random.rand(1000, 2) >>> random_array array([[0.5488135 , 0.71518937], [0.60276338, 0.54488318], [0.4236548 , 0.64589411], ..., [0.41443887, 0.79128155], [0.72119811, 0.48010781], [0.64386404, 0.50177313]]) >>> def example_plot(arr): ... fig = plt.figure(constrained_layout=True) ... ax = fig.add_subplot(aspect='equal', adjustable='box') ... ax.scatter(arr[:500, 0], arr[:500, 1], label='Group0') ... ax.scatter(arr[500:, 0], arr[500:, 1], label='Group1') ... ax.legend(frameon=False, bbox_to_anchor=(1.0, 0.95)) ... fig.show() >>> example_plot(random_array) >>> # from pyhelpers.store import save_fig >>> # save_fig("docs/source/_images/settings-mpl_preferences-demo-1.svg", verbose=True) >>> # save_fig("docs/source/_images/settings-mpl_preferences-demo-1.pdf", verbose=True)
>>> from pyhelpers.settings import mpl_preferences >>> mpl_preferences(fig_style='ggplot') >>> example_plot(random_array) >>> # path_to_fig_ = "docs/source/_images/settings-mpl_preferences-demo-2" >>> # save_fig(f"{path_to_fig_}.svg", verbose=True) >>> # save_fig(f"{path_to_fig_}.pdf", verbose=True)
Reset to default settings:
>>> mpl_preferences(reset=True) >>> # The altered parameters are now set to their default values. >>> example_plot(random_array)
(The above code should display the same figure as Figure 6.)