Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 199fd92

Browse files
committed
Add example code for current logo
1 parent e285dde commit 199fd92

File tree

1 file changed

+148
-76
lines changed

1 file changed

+148
-76
lines changed

examples/misc/logos2.py

Lines changed: 148 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,159 @@
11
"""
2-
================
3-
Matplotlib Logos
4-
================
2+
===============
3+
Matplotlib logo
4+
===============
55
6-
Displays some matplotlib logos.
6+
This example generates the current matplotlib logo.
77
8-
Thanks to Tony Yu <[email protected]> for the logo design
8+
Thanks to Tony Yu <[email protected]> for the original logo design.
99
"""
1010

1111
import numpy as np
1212
import matplotlib as mpl
1313
import matplotlib.pyplot as plt
1414
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)
15+
import matplotlib.font_manager
16+
from matplotlib.patches import Circle, Rectangle, PathPatch
17+
from matplotlib.textpath import TextPath
18+
import matplotlib.transforms as mtrans
19+
20+
MPL_BLUE = '#11557c'
21+
22+
23+
def get_font_properties():
24+
# The original font is Calibri, if that is not installed, we fall back
25+
# to Carlito, which is metrically equivalent.
26+
if 'Calibri' in matplotlib.font_manager.findfont('Calibri:bold'):
27+
return matplotlib.font_manager.FontProperties(family='Calibri',
28+
weight='bold')
29+
if 'Carlito' in matplotlib.font_manager.findfont('Carlito:bold'):
30+
print('Original font not found. Falling back to Carlito. '
31+
'The logo text will not be in the correct font.')
32+
return matplotlib.font_manager.FontProperties(family='Carlito',
33+
weight='bold')
34+
print('Original font not found. '
35+
'The logo text will not be in the correct font.')
36+
return None
37+
38+
39+
def create_icon_axes(fig, ax_position, lw_bars, lw_grid, lw_border, rgrid):
40+
"""
41+
Create a polar axes containing the matplotlib radar plot.
42+
43+
Parameters
44+
----------
45+
fig : matplotlib.figure.Figure
46+
The figure to draw into.
47+
ax_position : (float, float, float, float)
48+
The position of the created Axes in figure coordinates as
49+
(x, y, width, height).
50+
lw_bars : float
51+
The linewidth of the bars.
52+
lw_grid : float
53+
The linewidth of the grid.
54+
lw_border : float
55+
The linewidth of the Axes border.
56+
rgrid : array-like
57+
Positions of the radial grid.
58+
59+
Returns
60+
-------
61+
ax : matplotlib.axes.Axes
62+
The created Axes.
63+
"""
64+
with plt.rc_context({'axes.edgecolor': MPL_BLUE,
65+
'axes.linewidth': lw_border}):
66+
ax = fig.add_axes(ax_position, projection='polar')
67+
ax.set_axisbelow(True)
68+
69+
N = 7
70+
arc = 2. * np.pi
71+
theta = np.arange(0.0, arc, arc / N)
72+
radii = np.array([2, 6, 8, 7, 4, 5, 8])
73+
width = np.pi / 4 * np.array([0.4, 0.4, 0.6, 0.8, 0.2, 0.5, 0.3])
74+
bars = ax.bar(theta, radii, width=width, bottom=0.0, align='edge',
75+
edgecolor='0.3', lw=lw_bars)
76+
for r, bar in zip(radii, bars):
77+
color = *cm.jet(r / 10.)[:3], 0.6 # color from jet with alpha=0.6
78+
bar.set_facecolor(color)
79+
80+
ax.tick_params(labelbottom=False, labeltop=False,
81+
labelleft=False, labelright=False)
82+
83+
ax.grid(lw=lw_grid, color='0.9')
84+
ax.set_rmax(9)
85+
ax.set_yticks(rgrid)
86+
87+
# the actual visible background - extends a bit beyond the axis
88+
ax.add_patch(Rectangle((0, 0), arc, 9.58,
89+
facecolor='white', zorder=0,
90+
clip_on=False, in_layout=False))
91+
return ax
92+
93+
94+
def create_text_axes(fig, height_px):
95+
"""Create an axes in *fig* that contains 'matplotlib' as Text."""
96+
ax = fig.add_axes((0, 0, 1, 1))
97+
ax.set_aspect("equal")
5098
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-
8299

83-
if __name__ == '__main__':
84-
main_axes = add_math_background()
85-
add_polar_bar()
86-
add_matplotlib_text(main_axes)
87-
plt.show()
100+
path = TextPath((0, 0), "matplotlib", size=height_px * 0.8,
101+
prop=get_font_properties())
102+
103+
angle = 4.25 # degrees
104+
trans = mtrans.Affine2D().skew_deg(angle, 0)
105+
106+
patch = PathPatch(path, transform=trans + ax.transData, color=MPL_BLUE,
107+
lw=0)
108+
ax.add_patch(patch)
109+
ax.autoscale()
110+
111+
112+
def make_logo(height_px, lw_bars, lw_grid, lw_border, rgrid, with_text=False):
113+
"""
114+
Create a full figure with the Matplotlib logo.
115+
116+
Parameters
117+
----------
118+
height_px : int
119+
Height of the figure in pixel.
120+
lw_bars : float
121+
The linewidth of the bar border.
122+
lw_grid : float
123+
The linewidth of the grid.
124+
lw_border : float
125+
The linewidth of icon border.
126+
rgrid : sequence of float
127+
The radial grid positions.
128+
with_text : bool
129+
Whether to draw only the icon or to include 'matplotlib' as text.
130+
"""
131+
dpi = 100
132+
height = height_px / dpi
133+
figsize = (5 * height, height) if with_text else (height, height)
134+
fig = plt.figure(figsize=figsize, dpi=dpi)
135+
fig.patch.set_alpha(0)
136+
137+
if with_text:
138+
create_text_axes(fig, height_px)
139+
ax_pos = (0.535, 0.12, .17, 0.75) if with_text else (0.03, 0.03, .94, .94)
140+
ax = create_icon_axes(fig, ax_pos, lw_bars, lw_grid, lw_border, rgrid)
141+
142+
return fig, ax
143+
144+
##############################################################################
145+
# A large logo:
146+
147+
make_logo(height_px=110, lw_bars=0.7, lw_grid=0.5, lw_border=1,
148+
rgrid=[1, 3, 5, 7])
149+
150+
##############################################################################
151+
# A small 32px logo:
152+
153+
make_logo(height_px=32, lw_bars=0.3, lw_grid=0.3, lw_border=0.3, rgrid=[5])
154+
155+
##############################################################################
156+
# A large logo including text, as used on the matplotlib website.
157+
158+
make_logo(height_px=110, lw_bars=0.7, lw_grid=0.5, lw_border=1,
159+
rgrid=[1, 3, 5, 7], with_text=True)

0 commit comments

Comments
 (0)