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

Skip to content

Commit 96c8a57

Browse files
committed
Add example code for current logo
1 parent 2829d51 commit 96c8a57

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

examples/misc/logos2.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,156 @@
88
Thanks to Tony Yu <[email protected]> for the logo design
99
"""
1010

11+
import numpy as np
12+
import matplotlib as mpl
13+
import matplotlib.pyplot as plt
14+
import matplotlib.cm as cm
15+
from matplotlib.patches import Circle, Rectangle
16+
17+
18+
MPL_BLUE = '#11557c'
19+
20+
21+
def get_fontdict():
22+
mgr = FontManager()
23+
if 'Calibri' in mgr.findfont('Calibri:bold'):
24+
return {'fontfamily': 'Calibri', 'fontweight': 'bold'}
25+
if 'Carlito' in mgr.findfont('Carlito:bold'):
26+
return {'fontfamily': 'Carlito', 'fontweight': 'bold'}
27+
return None
28+
29+
30+
def create_icon_axes(fig, ax_position, lw_bars, lw_grid, lw_border, rgrid):
31+
"""
32+
Create a polar axes containing the matplotlib radar plot.
33+
34+
Parameters
35+
----------
36+
fig : matplotlib.figure.Figure
37+
The figure to draw into.
38+
ax_position : (float, float, float, float)
39+
The position of the created Axes in figure coordinates as
40+
(x, y, width, height).
41+
lw_bars : float
42+
The linewidth of the bars.
43+
lw_grid : float
44+
The linewidth of the grid.
45+
lw_border : float
46+
The linewidth of the Axes border.
47+
rgrid : array-like
48+
Positions of the radial grid.
49+
50+
Returns
51+
-------
52+
ax : matplotlib.axes.Axes
53+
The created Axes.
54+
"""
55+
with plt.rc_context({'axes.edgecolor': MPL_BLUE,
56+
'axes.linewidth': lw_border}):
57+
ax = fig.add_axes(ax_position, projection='polar')
58+
ax.set_axisbelow(True)
59+
60+
N = 7
61+
arc = 2. * np.pi
62+
theta = np.arange(0.0, arc, arc / N)
63+
radii = np.array([2, 6, 8, 7, 4, 5, 8])
64+
width = np.pi / 4 * np.array([0.4, 0.4, 0.6, 0.8, 0.2, 0.5, 0.3])
65+
bars = ax.bar(theta, radii, width=width, bottom=0.0, align='edge',
66+
edgecolor='0.1', lw=lw_bars)
67+
for r, bar in zip(radii, bars):
68+
color = *cm.jet(r / 10.)[:3], 0.6 # color from jet with alpha=0.6
69+
bar.set_facecolor(color)
70+
71+
ax.tick_params(labelbottom=False, labeltop=False,
72+
labelleft=False, labelright=False)
73+
74+
ax.grid(lw=lw_grid, ls=':', color='0.7')
75+
ax.set_rmax(9)
76+
ax.set_yticks(rgrid)
77+
78+
# the actual visible background - extends a bit beyond the axis
79+
ax.add_patch(Rectangle((0, 0), arc, 9.58,
80+
facecolor='white', zorder=0,
81+
clip_on=False, in_layout=False))
82+
return ax
83+
84+
85+
def make_logo(pixels, lw_bars, lw_grid, lw_border, rgrid, with_text=False):
86+
dpi = 100
87+
height = pixels / dpi
88+
figsize = (5 * height, height) if with_text else (height, height)
89+
fig = plt.figure(figsize=figsize, dpi=dpi)
90+
fig.patch.set_alpha(0)
91+
92+
if with_text:
93+
fig.text(0.5, 0.5, 'matplotlib', fontsize=pixels * 0.8,
94+
color=MPL_BLUE, ha='center', va='center',
95+
fontdict=get_fontdict(), zorder=-10)
96+
97+
ax_position = (.535, .1, .17, .8) if with_text else (.03, .03, .94, .94)
98+
ax = create_icon_axes(fig, ax_position, lw_bars, lw_grid, lw_border, rgrid)
99+
100+
return fig, ax
101+
102+
103+
params_large = {
104+
'pixels': 300,
105+
'lw_bars': 2,
106+
'lw_grid': 0.8,
107+
'lw_border': 3,
108+
'rgrid': [1, 3, 5, 7],
109+
}
110+
111+
fig, ax = make_logo(**params_large, with_text=True)
112+
plt.savefig('logo_large_text.png')
113+
114+
fig, ax = make_logo(**params_large)
115+
plt.savefig('logo_large.png')
116+
117+
##############################################################################
118+
119+
params_medium = {
120+
'pixels': 64,
121+
'lw_bars': 0.5,
122+
'lw_grid': 0.5,
123+
'lw_border': 1,
124+
'rgrid': [3, 6],
125+
}
126+
127+
make_logo(**params_medium)
128+
plt.savefig('logo_medium.png')
129+
130+
##############################################################################
131+
132+
params_small = {
133+
'pixels': 32,
134+
'lw_bars': 0.3,
135+
'lw_grid': 0.3,
136+
'lw_border': 0.3,
137+
'rgrid': [5],
138+
}
139+
140+
make_logo(**params_small)
141+
plt.savefig('logo_small.png')
142+
143+
##############################################################################
144+
145+
params_xsmall = {
146+
'pixels': 16,
147+
'lw_bars': 0.1,
148+
'lw_grid': 0.3,
149+
'lw_border': 0.3,
150+
'rgrid': [],
151+
}
152+
153+
make_logo(**params_xsmall)
154+
plt.savefig('logo_xsmall.png')
155+
156+
##############################################################################
157+
#
158+
# Old stuff
159+
# """""""""
160+
11161
import numpy as np
12162
import matplotlib as mpl
13163
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)