|
3 | 3 | Plotting |
4 | 4 | ======== |
5 | 5 |
|
6 | | -The plotting subpackage provides a set of functions to create plots that follow the Merantix Momentum style guide. |
7 | | -The style is based on the [default Matplotlib style](https://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/mpl-data/stylelib/classic.mplstyle). |
| 6 | +The plotting subpackage provides functions to create plots following the Merantix Momentum style guide. |
8 | 7 |
|
9 | | -Download the fonts from Google Fonts:: |
| 8 | +Fonts |
| 9 | +----- |
10 | 10 |
|
11 | | - !wget 'https://github.com/google/fonts/raw/main/ofl/spacegrotesk/SpaceGrotesk%5Bwght%5D.ttf' |
12 | | - !wget 'https://github.com/google/fonts/raw/main/ofl/inter/Inter%5Bopsz%2Cwght%5D.ttf' |
| 11 | +Download and install the required fonts from Google Fonts:: |
13 | 12 |
|
14 | | -Add the fonts to the font manager:: |
| 13 | + wget 'https://github.com/google/fonts/raw/main/ofl/spacegrotesk/SpaceGrotesk%5Bwght%5D.ttf' |
| 14 | + wget 'https://github.com/google/fonts/raw/main/ofl/inter/Inter%5Bopsz%2Cwght%5D.ttf' |
15 | 15 |
|
16 | | - from matplotlib import font_manager as fm, pyplot as plt |
17 | | - font_files = fm.findSystemFonts('.') |
18 | | - for font_file in font_files: |
| 16 | +Install them to your system (e.g., Font Book on macOS), then register with matplotlib:: |
| 17 | + |
| 18 | + from matplotlib import font_manager as fm |
| 19 | + for font_file in fm.findSystemFonts(): |
19 | 20 | fm.fontManager.addfont(font_file) |
20 | 21 |
|
21 | | -Alternatively, install the fonts to the system after downloading (e.g., adding them to Font Book on macOS) and add them to the font manager (otherwise they are not in the font cache):: |
| 22 | +Styles and Variants |
| 23 | +------------------- |
22 | 24 |
|
23 | | - from matplotlib import font_manager as fm, pyplot as plt |
24 | | - font_files = fm.findSystemFonts() |
25 | | - for font_file in font_files: |
26 | | - fm.fontManager.addfont(font_file) |
| 25 | +Two styles are available via ``MXM_STYLE``: |
27 | 26 |
|
28 | | -Examples |
29 | | --------- |
| 27 | +- ``MXM_STYLE.LIGHT`` — light backgrounds (variant 0: white, variant 1: offwhite) |
| 28 | +- ``MXM_STYLE.DARK`` — dark backgrounds (variant 0: blueberry, variant 1: black, variant 2: admiral) |
30 | 29 |
|
31 | | -Example Usage 1:: |
| 30 | +Use ``get_mpl_context(style, variant)`` to apply the theme as a context manager. |
32 | 31 |
|
33 | | - import matplotlib.pyplot as plt |
| 32 | +Title Font |
| 33 | +---------- |
34 | 34 |
|
35 | | - style = MXM_STYLE.LIGHT |
36 | | - with get_mpl_context(style) as ctx: |
37 | | - plt.figure(figsize=(10,5)) |
38 | | - N = 10 |
39 | | - plt.bar((range(N)), (range(1, N+1, 1)), label='series 1', color=get_color_cycle(style=style)) |
40 | | - plt.title("Amazing title") |
41 | | - plt.xlabel('Awesome Label') |
42 | | - plt.ylabel('Awesome Label') |
| 35 | +Matplotlib has no rcParam for title font family, so titles use the body font by default. |
| 36 | +Apply the title font explicitly using the ``TITLE_FONT`` dict:: |
43 | 37 |
|
44 | | - plt.legend() |
| 38 | + fig.suptitle("My Title", **plotting.TITLE_FONT) |
| 39 | + ax.set_title("Axes Title", **plotting.TITLE_FONT) |
45 | 40 |
|
46 | | -Example Usage 2:: |
| 41 | +Colors |
| 42 | +------ |
47 | 43 |
|
48 | | - import matplotlib.pyplot as plt |
| 44 | +Individual colors can be imported directly:: |
| 45 | + |
| 46 | + from scaffold.plotting import ELECTRIC, CARIBBEAN, CORAL, ADMIRAL, PINE |
49 | 47 |
|
50 | | - style = MXM_STYLE.DARK |
51 | | - with get_mpl_context(style) as ctx: |
52 | | - plt.figure(figsize=(10,5)) |
53 | | - N = 20 |
54 | | - for c in get_color_cycle(style=style): |
55 | | - plt.plot(np.random.random(N), label=f'series {c}', color=c) |
56 | | - plt.title("Amazing title") |
57 | | - plt.xlabel('Awesome Label') |
58 | | - plt.ylabel('Awesome Label') |
| 48 | +Or get the full color cycle for a style:: |
59 | 49 |
|
60 | | - plt.legend() |
| 50 | + from scaffold.plotting import get_color_cycle, MXM_STYLE |
| 51 | + colors = get_color_cycle(style=MXM_STYLE.LIGHT) |
61 | 52 |
|
62 | | -Example Usage 3:: |
| 53 | +Example |
| 54 | +------- |
63 | 55 |
|
64 | | - import seaborn as sns |
| 56 | +:: |
| 57 | + |
| 58 | + from scaffold import plotting |
| 59 | + import matplotlib.pyplot as plt |
65 | 60 | import numpy as np |
66 | 61 |
|
67 | | - # demo data set |
68 | | - N = 10 |
69 | | - data = { |
70 | | - 'idx': range(N), |
71 | | - 'x': np.random.random(N), |
72 | | - 'y': np.random.random(N), |
73 | | - 'size': np.random.random(N) |
74 | | - } |
75 | | - with get_mpl_context(): |
76 | | - sns.scatterplot(data=data, x="x", y="y", |
77 | | - size="size", hue='idx', legend=False, |
78 | | - sizes=(100, 500)).set(title='Awesome Title') |
79 | | - |
80 | | -Applying the rc params to all plots:: |
| 62 | + style = plotting.MXM_STYLE.LIGHT |
| 63 | + variant = 0 |
| 64 | + |
| 65 | + with plotting.get_mpl_context(style, variant): |
| 66 | + fig, ax = plt.subplots(figsize=(10, 5)) |
| 67 | + fig.suptitle("Example plot", **plotting.TITLE_FONT) |
| 68 | + |
| 69 | + N = 5 |
| 70 | + x = np.arange(N) |
| 71 | + for i in range(3): |
| 72 | + ax.bar(x + i * 0.25, np.random.randint(3, 15, N), width=0.2) |
| 73 | + |
| 74 | + ax.set_xlabel("Category") |
| 75 | + ax.set_ylabel("Value") |
| 76 | + ax.legend(["A", "B", "C"], loc="upper center", bbox_to_anchor=(0.5, -0.1), ncol=3) |
| 77 | + |
| 78 | + plt.tight_layout() |
| 79 | + plt.show() |
| 80 | + |
| 81 | +Logo Helper |
| 82 | +----------- |
| 83 | + |
| 84 | +Use ``plotting.add_logo()`` to place a logo in the corner of a figure:: |
| 85 | + |
| 86 | + plotting.add_logo(fig, style=style, variant=variant, position="upper right") |
| 87 | + |
| 88 | +Custom logos can be passed via the ``logo_path`` argument (supports any fsspec-compatible path):: |
| 89 | + |
| 90 | + plotting.add_logo(fig, logo_path="gs://my-bucket/logo.png") |
| 91 | + |
| 92 | +Applying Globally |
| 93 | +----------------- |
| 94 | + |
| 95 | +To apply the style to all plots without a context manager:: |
81 | 96 |
|
82 | 97 | import matplotlib.pyplot as plt |
| 98 | + from scaffold.plotting import get_rc_params, MXM_STYLE |
83 | 99 |
|
84 | | - style = MXM_STYLE.LIGHT |
85 | | - rc = get_rc_params(style) |
86 | | - plt.rcParams.update(rc) |
| 100 | + plt.rcParams.update(get_rc_params(MXM_STYLE.LIGHT, variant=0)) |
0 commit comments