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

Skip to content

Commit 5eac537

Browse files
committed
add script for generating the matplotlib logo
* credit to Tim Hoffmann as the original author of the script
1 parent dbf92ab commit 5eac537

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

logos/mpl-logos2.py

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

0 commit comments

Comments
 (0)