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.
- Parameters:
backend (str | None) – specify the backend used for rendering and GUI integration, defaults to
None
font_name (None | str) – name of a 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 plot legend, defaults to
0.7
fig_style (str | None) – style of the figure, defaults to
None
reset (bool) – whether to reset to default settings, defaults to
False
Examples:
>>> import numpy as np >>> 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)) ... ... plt.show() >>> example_plot(random_array)
>>> from pyhelpers.settings import mpl_preferences >>> mpl_preferences(fig_style='ggplot') >>> example_plot(random_array)
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 Fig. 1.)