|
1 | 1 | """
|
2 |
| -================ |
3 |
| -Matplotlib Logos |
4 |
| -================ |
| 2 | +=============== |
| 3 | +Matplotlib logo |
| 4 | +=============== |
5 | 5 |
|
6 |
| -Displays some matplotlib logos. |
7 |
| -
|
8 |
| -Thanks to Tony Yu <tsyu80@gmail.com> for the logo design |
| 6 | +This example generates the current matplotlib logo. |
9 | 7 | """
|
10 | 8 |
|
11 | 9 | import numpy as np
|
12 | 10 | import matplotlib as mpl
|
13 | 11 | import matplotlib.pyplot as plt
|
14 | 12 | import matplotlib.cm as cm
|
15 |
| - |
16 |
| -mpl.rcParams['xtick.labelsize'] = 10 |
17 |
| -mpl.rcParams['ytick.labelsize'] = 12 |
18 |
| -mpl.rcParams['axes.edgecolor'] = 'gray' |
19 |
| - |
20 |
| - |
21 |
| -axalpha = 0.05 |
22 |
| -figcolor = 'white' |
23 |
| -dpi = 80 |
24 |
| -fig = plt.figure(figsize=(6, 1.1), dpi=dpi) |
25 |
| -fig.patch.set_edgecolor(figcolor) |
26 |
| -fig.patch.set_facecolor(figcolor) |
27 |
| - |
28 |
| - |
29 |
| -def add_math_background(): |
30 |
| - ax = fig.add_axes([0., 0., 1., 1.]) |
31 |
| - |
32 |
| - text = [] |
33 |
| - text.append( |
34 |
| - (r"$W^{3\beta}_{\delta_1 \rho_1 \sigma_2} = " |
35 |
| - r"U^{3\beta}_{\delta_1 \rho_1} + \frac{1}{8 \pi 2}" |
36 |
| - r"\int^{\alpha_2}_{\alpha_2} d \alpha^\prime_2 " |
37 |
| - r"\left[\frac{ U^{2\beta}_{\delta_1 \rho_1} - " |
38 |
| - r"\alpha^\prime_2U^{1\beta}_{\rho_1 \sigma_2} " |
39 |
| - r"}{U^{0\beta}_{\rho_1 \sigma_2}}\right]$", (0.7, 0.2), 20)) |
40 |
| - text.append((r"$\frac{d\rho}{d t} + \rho \vec{v}\cdot\nabla\vec{v} " |
41 |
| - r"= -\nabla p + \mu\nabla^2 \vec{v} + \rho \vec{g}$", |
42 |
| - (0.35, 0.9), 20)) |
43 |
| - text.append((r"$\int_{-\infty}^\infty e^{-x^2}dx=\sqrt{\pi}$", |
44 |
| - (0.15, 0.3), 25)) |
45 |
| - text.append((r"$F_G = G\frac{m_1m_2}{r^2}$", |
46 |
| - (0.85, 0.7), 30)) |
47 |
| - for eq, (x, y), size in text: |
48 |
| - ax.text(x, y, eq, ha='center', va='center', color="#11557c", |
49 |
| - alpha=0.25, transform=ax.transAxes, fontsize=size) |
| 13 | +import matplotlib.font_manager |
| 14 | +from matplotlib.patches import Circle, Rectangle, PathPatch |
| 15 | +from matplotlib.textpath import TextPath |
| 16 | +import matplotlib.transforms as mtrans |
| 17 | + |
| 18 | +MPL_BLUE = '#11557c' |
| 19 | + |
| 20 | + |
| 21 | +def get_font_properties(): |
| 22 | + # The original font is Calibri, if that is not installed, we fall back |
| 23 | + # to Carlito, which is metrically equivalent. |
| 24 | + if 'Calibri' in matplotlib.font_manager.findfont('Calibri:bold'): |
| 25 | + return matplotlib.font_manager.FontProperties(family='Calibri', |
| 26 | + weight='bold') |
| 27 | + if 'Carlito' in matplotlib.font_manager.findfont('Carlito:bold'): |
| 28 | + print('Original font not found. Falling back to Carlito. ' |
| 29 | + 'The logo text will not be in the correct font.') |
| 30 | + return matplotlib.font_manager.FontProperties(family='Carlito', |
| 31 | + weight='bold') |
| 32 | + print('Original font not found. ' |
| 33 | + 'The logo text will not be in the correct font.') |
| 34 | + return None |
| 35 | + |
| 36 | + |
| 37 | +def create_icon_axes(fig, ax_position, lw_bars, lw_grid, lw_border, rgrid): |
| 38 | + """ |
| 39 | + Create a polar axes containing the matplotlib radar plot. |
| 40 | +
|
| 41 | + Parameters |
| 42 | + ---------- |
| 43 | + fig : matplotlib.figure.Figure |
| 44 | + The figure to draw into. |
| 45 | + ax_position : (float, float, float, float) |
| 46 | + The position of the created Axes in figure coordinates as |
| 47 | + (x, y, width, height). |
| 48 | + lw_bars : float |
| 49 | + The linewidth of the bars. |
| 50 | + lw_grid : float |
| 51 | + The linewidth of the grid. |
| 52 | + lw_border : float |
| 53 | + The linewidth of the Axes border. |
| 54 | + rgrid : array-like |
| 55 | + Positions of the radial grid. |
| 56 | +
|
| 57 | + Returns |
| 58 | + ------- |
| 59 | + ax : matplotlib.axes.Axes |
| 60 | + The created Axes. |
| 61 | + """ |
| 62 | + with plt.rc_context({'axes.edgecolor': MPL_BLUE, |
| 63 | + 'axes.linewidth': lw_border}): |
| 64 | + ax = fig.add_axes(ax_position, projection='polar') |
| 65 | + ax.set_axisbelow(True) |
| 66 | + |
| 67 | + N = 7 |
| 68 | + arc = 2. * np.pi |
| 69 | + theta = np.arange(0.0, arc, arc / N) |
| 70 | + radii = np.array([2, 6, 8, 7, 4, 5, 8]) |
| 71 | + width = np.pi / 4 * np.array([0.4, 0.4, 0.6, 0.8, 0.2, 0.5, 0.3]) |
| 72 | + bars = ax.bar(theta, radii, width=width, bottom=0.0, align='edge', |
| 73 | + edgecolor='0.3', lw=lw_bars) |
| 74 | + for r, bar in zip(radii, bars): |
| 75 | + color = *cm.jet(r / 10.)[:3], 0.6 # color from jet with alpha=0.6 |
| 76 | + bar.set_facecolor(color) |
| 77 | + |
| 78 | + ax.tick_params(labelbottom=False, labeltop=False, |
| 79 | + labelleft=False, labelright=False) |
| 80 | + |
| 81 | + ax.grid(lw=lw_grid, color='0.9') |
| 82 | + ax.set_rmax(9) |
| 83 | + ax.set_yticks(rgrid) |
| 84 | + |
| 85 | + # the actual visible background - extends a bit beyond the axis |
| 86 | + ax.add_patch(Rectangle((0, 0), arc, 9.58, |
| 87 | + facecolor='white', zorder=0, |
| 88 | + clip_on=False, in_layout=False)) |
| 89 | + return ax |
| 90 | + |
| 91 | + |
| 92 | +def create_text_axes(fig, height_px): |
| 93 | + """Create an axes in *fig* that contains 'matplotlib' as Text.""" |
| 94 | + ax = fig.add_axes((0, 0, 1, 1)) |
| 95 | + ax.set_aspect("equal") |
50 | 96 | ax.set_axis_off()
|
51 |
| - return ax |
52 |
| - |
53 |
| - |
54 |
| -def add_matplotlib_text(ax): |
55 |
| - ax.text(0.95, 0.5, 'matplotlib', color='#11557c', fontsize=65, |
56 |
| - ha='right', va='center', alpha=1.0, transform=ax.transAxes) |
57 |
| - |
58 |
| - |
59 |
| -def add_polar_bar(): |
60 |
| - ax = fig.add_axes([0.025, 0.075, 0.2, 0.85], projection='polar') |
61 |
| - |
62 |
| - ax.patch.set_alpha(axalpha) |
63 |
| - ax.set_axisbelow(True) |
64 |
| - N = 7 |
65 |
| - arc = 2. * np.pi |
66 |
| - theta = np.arange(0.0, arc, arc/N) |
67 |
| - radii = 10 * np.array([0.2, 0.6, 0.8, 0.7, 0.4, 0.5, 0.8]) |
68 |
| - width = np.pi / 4 * np.array([0.4, 0.4, 0.6, 0.8, 0.2, 0.5, 0.3]) |
69 |
| - bars = ax.bar(theta, radii, width=width, bottom=0.0) |
70 |
| - for r, bar in zip(radii, bars): |
71 |
| - bar.set_facecolor(cm.jet(r/10.)) |
72 |
| - bar.set_alpha(0.6) |
73 |
| - |
74 |
| - ax.tick_params(labelbottom=False, labeltop=False, |
75 |
| - labelleft=False, labelright=False) |
76 |
| - |
77 |
| - ax.grid(lw=0.8, alpha=0.9, ls='-', color='0.5') |
78 |
| - |
79 |
| - ax.set_yticks(np.arange(1, 9, 2)) |
80 |
| - ax.set_rmax(9) |
81 |
| - |
82 | 97 |
|
83 |
| -if __name__ == '__main__': |
84 |
| - main_axes = add_math_background() |
85 |
| - add_polar_bar() |
86 |
| - add_matplotlib_text(main_axes) |
87 |
| - plt.show() |
| 98 | + path = TextPath((0, 0), "matplotlib", size=height_px * 0.8, |
| 99 | + prop=get_font_properties()) |
| 100 | + |
| 101 | + angle = 4.25 # degrees |
| 102 | + trans = mtrans.Affine2D().skew_deg(angle, 0) |
| 103 | + |
| 104 | + patch = PathPatch(path, transform=trans + ax.transData, color=MPL_BLUE, |
| 105 | + lw=0) |
| 106 | + ax.add_patch(patch) |
| 107 | + ax.autoscale() |
| 108 | + |
| 109 | + |
| 110 | +def make_logo(height_px, lw_bars, lw_grid, lw_border, rgrid, with_text=False): |
| 111 | + """ |
| 112 | + Create a full figure with the Matplotlib logo. |
| 113 | +
|
| 114 | + Parameters |
| 115 | + ---------- |
| 116 | + height_px : int |
| 117 | + Height of the figure in pixel. |
| 118 | + lw_bars : float |
| 119 | + The linewidth of the bar border. |
| 120 | + lw_grid : float |
| 121 | + The linewidth of the grid. |
| 122 | + lw_border : float |
| 123 | + The linewidth of icon border. |
| 124 | + rgrid : sequence of float |
| 125 | + The radial grid positions. |
| 126 | + with_text : bool |
| 127 | + Whether to draw only the icon or to include 'matplotlib' as text. |
| 128 | + """ |
| 129 | + dpi = 100 |
| 130 | + height = height_px / dpi |
| 131 | + figsize = (5 * height, height) if with_text else (height, height) |
| 132 | + fig = plt.figure(figsize=figsize, dpi=dpi) |
| 133 | + fig.patch.set_alpha(0) |
| 134 | + |
| 135 | + if with_text: |
| 136 | + create_text_axes(fig, height_px) |
| 137 | + ax_pos = (0.535, 0.12, .17, 0.75) if with_text else (0.03, 0.03, .94, .94) |
| 138 | + ax = create_icon_axes(fig, ax_pos, lw_bars, lw_grid, lw_border, rgrid) |
| 139 | + |
| 140 | + return fig, ax |
| 141 | + |
| 142 | +############################################################################## |
| 143 | +# A large logo: |
| 144 | + |
| 145 | +make_logo(height_px=110, lw_bars=0.7, lw_grid=0.5, lw_border=1, |
| 146 | + rgrid=[1, 3, 5, 7]) |
| 147 | + |
| 148 | +############################################################################## |
| 149 | +# A small 32px logo: |
| 150 | + |
| 151 | +make_logo(height_px=32, lw_bars=0.3, lw_grid=0.3, lw_border=0.3, rgrid=[5]) |
| 152 | + |
| 153 | +############################################################################## |
| 154 | +# A large logo including text, as used on the matplotlib website. |
| 155 | + |
| 156 | +make_logo(height_px=110, lw_bars=0.7, lw_grid=0.5, lw_border=1, |
| 157 | + rgrid=[1, 3, 5, 7], with_text=True) |
0 commit comments