Skip to content

Commit 84188bf

Browse files
committed
Tweak aesthetics of plots in screenshots page.
These changes also cleanup the code a bit, as described by MEP 12.
1 parent 5438051 commit 84188bf

File tree

7 files changed

+179
-272
lines changed

7 files changed

+179
-272
lines changed

doc/pyplots/tex_demo.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#!/usr/bin/env python
21
"""
2+
Demo of TeX rendering.
3+
34
You can use TeX to render all of your matplotlib text if the rc
45
parameter text.usetex is set. This works currently on the agg and ps
56
backends, and requires that you have tex and the other dependencies
@@ -10,26 +11,25 @@
1011
~/.tex.cache
1112
1213
"""
13-
from matplotlib import rc
14-
from numpy import arange, cos, pi
15-
from matplotlib.pyplot import figure, axes, plot, xlabel, ylabel, title, \
16-
grid, savefig, show
14+
import numpy as np
15+
import matplotlib.pyplot as plt
1716

1817

19-
rc('text', usetex=True)
20-
rc('font', family='serif')
21-
figure(1, figsize=(6,4))
22-
ax = axes([0.1, 0.1, 0.8, 0.7])
23-
t = arange(0.0, 1.0+0.01, 0.01)
24-
s = cos(2*2*pi*t)+2
25-
plot(t, s)
18+
# Example data
19+
t = np.arange(0.0, 1.0 + 0.01, 0.01)
20+
s = np.cos(4 * np.pi * t) + 2
2621

27-
xlabel(r'\textbf{time (s)}')
28-
ylabel(r'\textit{voltage (mV)}',fontsize=16)
29-
title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
30-
fontsize=16, color='r')
31-
grid(True)
32-
savefig('tex_demo')
22+
plt.rc('text', usetex=True)
23+
plt.rc('font', family='serif')
24+
plt.plot(t, s)
3325

26+
plt.xlabel(r'\textbf{time} (s)')
27+
plt.ylabel(r'\textit{voltage} (mV)',fontsize=16)
28+
plt.title(r"\TeX\ is Number "
29+
r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
30+
fontsize=16, color='gray')
31+
# Make room for the ridiculously large title.
32+
plt.subplots_adjust(top=0.8)
3433

35-
show()
34+
plt.savefig('tex_demo')
35+
plt.show()

examples/api/legend_demo.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
1+
"""
2+
Demo of the legend function with a few features.
3+
4+
In addition to the basic legend, this demo shows a few optional features:
5+
6+
* Custom legend placement.
7+
* A keyword argument to a drop-shadow.
8+
* Setting the background color.
9+
* Setting the font size.
10+
* Setting the line width.
11+
"""
112
import numpy as np
213
import matplotlib.pyplot as plt
314

4-
a = np.arange(0,3,.02)
5-
b = np.arange(0,3,.02)
15+
16+
# Example data
17+
a = np.arange(0,3, .02)
18+
b = np.arange(0,3, .02)
619
c = np.exp(a)
720
d = c[::-1]
821

9-
fig = plt.figure()
10-
ax = fig.add_subplot(111)
11-
ax.plot(a,c,'k--',a,d,'k:',a,c+d,'k')
12-
leg = ax.legend(('Model length', 'Data length', 'Total message length'),
13-
'upper center', shadow=True)
14-
ax.set_ylim([-1,20])
15-
ax.grid(False)
16-
ax.set_xlabel('Model complexity --->')
17-
ax.set_ylabel('Message length --->')
18-
ax.set_title('Minimum Message Length')
19-
20-
ax.set_yticklabels([])
21-
ax.set_xticklabels([])
22-
23-
# set some legend properties. All the code below is optional. The
24-
# defaults are usually sensible but if you need more control, this
25-
# shows you how
26-
27-
# the matplotlib.patches.Rectangle instance surrounding the legend
28-
frame = leg.get_frame()
29-
frame.set_facecolor('0.80') # set the frame face color to light gray
30-
31-
# matplotlib.text.Text instances
32-
for t in leg.get_texts():
33-
t.set_fontsize('small') # the legend text fontsize
34-
35-
# matplotlib.lines.Line2D instances
36-
for l in leg.get_lines():
37-
l.set_linewidth(1.5) # the legend line width
38-
plt.show()
22+
# Create plots with pre-defined labels.
23+
# Alternatively, you can pass labels explicitly when calling `legend`.
24+
fig, ax = plt.subplots()
25+
ax.plot(a, c, 'k--', label='Model length')
26+
ax.plot(a, d, 'k:', label='Data length')
27+
ax.plot(a, c+d, 'k', label='Total message length')
28+
29+
# Now add the legend with some customizations.
30+
legend = ax.legend(loc='upper center', shadow=True)
3931

32+
# The frame is matplotlib.patches.Rectangle instance surrounding the legend.
33+
frame = legend.get_frame()
34+
frame.set_facecolor('0.90')
4035

36+
# Set the fontsize
37+
for label in legend.get_texts():
38+
label.set_fontsize('large')
4139

40+
for label in legend.get_lines():
41+
label.set_linewidth(1.5) # the legend line width
42+
plt.show()
Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
1-
2-
#!/usr/bin/env python
1+
"""
2+
Bar chart demo with pairs of bars grouped for easy comparison.
3+
"""
34
import numpy as np
45
import matplotlib.pyplot as plt
56

6-
N = 5
7-
menMeans = (20, 35, 30, 35, 27)
8-
menStd = (2, 3, 4, 1, 2)
97

10-
ind = np.arange(N) # the x locations for the groups
11-
width = 0.35 # the width of the bars
8+
n_groups = 5
129

10+
means_men = (20, 35, 30, 35, 27)
11+
std_men = (2, 3, 4, 1, 2)
1312

14-
plt.subplot(111)
15-
rects1 = plt.bar(ind, menMeans, width,
16-
color='r',
17-
yerr=menStd,
18-
error_kw=dict(elinewidth=6, ecolor='pink'))
13+
means_women = (25, 32, 34, 20, 25)
14+
std_women = (3, 5, 2, 3, 3)
1915

20-
womenMeans = (25, 32, 34, 20, 25)
21-
womenStd = (3, 5, 2, 3, 3)
22-
rects2 = plt.bar(ind+width, womenMeans, width,
23-
color='y',
24-
yerr=womenStd,
25-
error_kw=dict(elinewidth=6, ecolor='yellow'))
16+
index = np.arange(n_groups)
17+
bar_width = 0.35
2618

27-
# add some
28-
plt.ylabel('Scores')
29-
plt.title('Scores by group and gender')
30-
plt.xticks(ind+width, ('G1', 'G2', 'G3', 'G4', 'G5') )
19+
opacity = 0.4
20+
error_config = {'ecolor': '0.3'}
3121

32-
plt.legend( (rects1[0], rects2[0]), ('Men', 'Women') )
22+
rects1 = plt.bar(index, means_men, bar_width,
23+
alpha=opacity,
24+
color='b',
25+
yerr=std_men,
26+
error_kw=error_config,
27+
label='Men')
3328

34-
def autolabel(rects):
35-
# attach some text labels
36-
for rect in rects:
37-
height = rect.get_height()
38-
plt.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
39-
ha='center', va='bottom')
29+
rects2 = plt.bar(index + bar_width, means_women, bar_width,
30+
alpha=opacity,
31+
color='r',
32+
yerr=std_women,
33+
error_kw=error_config,
34+
label='Women')
4035

41-
autolabel(rects1)
42-
autolabel(rects2)
36+
plt.xlabel('Group')
37+
plt.ylabel('Scores')
38+
plt.title('Scores by group and gender')
39+
plt.xticks(index + bar_width, ('A', 'B', 'C', 'D', 'E'))
40+
plt.legend()
4341

42+
plt.tight_layout()
4443
plt.show()
Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,42 @@
1-
#!/usr/bin/env python
2-
# Thanks to Charles Twardy for this example
3-
#
4-
#See http://matplotlib.org/examples/pylab_examples/legend_demo2.html
5-
# for an example controlling which lines the legend uses and the order
6-
# they are drawn in.
1+
"""
2+
Demo of the legend function with a few features.
73
4+
In addition to the basic legend, this demo shows a few optional features:
5+
6+
* Custom legend placement.
7+
* A keyword argument to a drop-shadow.
8+
* Setting the background color.
9+
* Setting the font size.
10+
* Setting the line width.
11+
"""
812
import numpy as np
913
import matplotlib.pyplot as plt
1014

11-
a = np.arange(0,3,.02)
12-
b = np.arange(0,3,.02)
15+
16+
# Example data
17+
a = np.arange(0,3, .02)
18+
b = np.arange(0,3, .02)
1319
c = np.exp(a)
1420
d = c[::-1]
1521

16-
ax = plt.subplot(111)
17-
plt.plot(a,c,'k--',a,d,'k:',a,c+d,'k')
18-
plt.legend(('Model length', 'Data length', 'Total message length'),
19-
'upper center', shadow=True, fancybox=True)
20-
plt.ylim([-1,20])
21-
plt.grid(False)
22-
plt.xlabel('Model complexity --->')
23-
plt.ylabel('Message length --->')
24-
plt.title('Minimum Message Length')
25-
26-
plt.setp(plt.gca(), 'yticklabels', [])
27-
plt.setp(plt.gca(), 'xticklabels', [])
28-
29-
# set some legend properties. All the code below is optional. The
30-
# defaults are usually sensible but if you need more control, this
31-
# shows you how
32-
leg = plt.gca().get_legend()
33-
ltext = leg.get_texts() # all the text.Text instance in the legend
34-
llines = leg.get_lines() # all the lines.Line2D instance in the legend
35-
frame = leg.get_frame() # the patch.Rectangle instance surrounding the legend
36-
37-
# see text.Text, lines.Line2D, and patches.Rectangle for more info on
38-
# the settable properties of lines, text, and rectangles
39-
frame.set_facecolor('0.80') # set the frame face color to light gray
40-
plt.setp(ltext, fontsize='small') # the legend text fontsize
41-
plt.setp(llines, linewidth=1.5) # the legend linewidth
42-
#leg.draw_frame(False) # don't draw the legend frame
43-
plt.show()
22+
# Create plots with pre-defined labels.
23+
# Alternatively, you can pass labels explicitly when calling `legend`.
24+
fig, ax = plt.subplots()
25+
ax.plot(a, c, 'k--', label='Model length')
26+
ax.plot(a, d, 'k:', label='Data length')
27+
ax.plot(a, c+d, 'k', label='Total message length')
4428

29+
# Now add the legend with some customizations.
30+
legend = ax.legend(loc='upper center', shadow=True)
4531

32+
# The frame is matplotlib.patches.Rectangle instance surrounding the legend.
33+
frame = legend.get_frame()
34+
frame.set_facecolor('0.90')
4635

36+
# Set the fontsize
37+
for label in legend.get_texts():
38+
label.set_fontsize('large')
39+
40+
for label in legend.get_lines():
41+
label.set_linewidth(1.5) # the legend line width
42+
plt.show()
Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,17 @@
1-
#!/usr/bin/env python
2-
#
3-
# matplotlib now has a PolarAxes class and a polar function in the
4-
# matplotlib interface. This is considered alpha and the interface
5-
# may change as we work out how polar axes should best be integrated
6-
#
7-
# The only function that has been tested on polar axes is "plot" (the
8-
# pylab interface function "polar" calls ax.plot where ax is a
9-
# PolarAxes) -- other axes plotting functions may work on PolarAxes
10-
# but haven't been tested and may need tweaking.
11-
#
12-
# you can get a PolarSubplot instance by doing, for example
13-
#
14-
# subplot(211, polar=True)
15-
#
16-
# or a PolarAxes instance by doing
17-
# axes([left, bottom, width, height], polar=True)
18-
#
19-
# The view limits (eg xlim and ylim) apply to the lower left and upper
20-
# right of the rectangular box that surrounds to polar axes. e.g., if
21-
# you have
22-
#
23-
# r = arange(0,1,0.01)
24-
# theta = 2*pi*r
25-
#
26-
# the lower left corner is 5/4pi, sqrt(2) and the
27-
# upper right corner is 1/4pi, sqrt(2)
28-
#
29-
# you could change the radial bounding box (zoom out) by setting the
30-
# ylim (radial coordinate is the second argument to the plot command,
31-
# as in MATLAB, though this is not advised currently because it is not
32-
# clear to me how the axes should behave in the change of view limits.
33-
# Please advise me if you have opinions. Likewise, the pan/zoom
34-
# controls probably do not do what you think they do and are better
35-
# left alone on polar axes. Perhaps I will disable them for polar
36-
# axes unless we come up with a meaningful, useful and functional
37-
# implementation for them.
38-
#
39-
# See the pylab rgrids and thetagrids functions for
40-
# information on how to customize the grid locations and labels
41-
import matplotlib
1+
"""
2+
Demo of a line plot on a polar axis.
3+
"""
424
import numpy as np
43-
from matplotlib.pyplot import figure, show, rc, grid
5+
import matplotlib.pyplot as plt
446

45-
# radar green, solid grid lines
46-
rc('grid', color='#316931', linewidth=1, linestyle='-')
47-
rc('xtick', labelsize=15)
48-
rc('ytick', labelsize=15)
49-
50-
# force square figure and square axes looks better for polar, IMO
51-
width, height = matplotlib.rcParams['figure.figsize']
52-
size = min(width, height)
53-
# make a square figure
54-
fig = figure(figsize=(size, size))
55-
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True, axisbg='#d5de9c')
567

578
r = np.arange(0, 3.0, 0.01)
58-
theta = 2*np.pi*r
59-
ax.plot(theta, r, color='#ee8d18', lw=3)
9+
theta = 2 * np.pi * r
10+
11+
ax = plt.subplot(111, polar=True)
12+
ax.plot(theta, r, color='r', linewidth=3)
6013
ax.set_rmax(2.0)
61-
grid(True)
14+
ax.grid(True)
6215

63-
ax.set_title("And there was much rejoicing!", fontsize=20)
64-
show()
16+
ax.set_title("A line plot on a polar axis", va='bottom')
17+
plt.show()

0 commit comments

Comments
 (0)