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

Skip to content

Commit db884dd

Browse files
committed
cleaned legend demo example
svn path=/trunk/matplotlib/; revision=5705
1 parent 2158126 commit db884dd

4 files changed

Lines changed: 71 additions & 29 deletions

File tree

examples/api/legend_demo.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
a = np.arange(0,3,.02)
5+
b = np.arange(0,3,.02)
6+
c = np.exp(a)
7+
d = c[::-1]
8+
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()
39+
40+
41+

examples/pylab_examples/hexbin_demo.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"""
77

88
import numpy as np
9+
import matplotlib.cm as cm
910
import matplotlib.pyplot as plt
11+
1012
n = 100000
1113
x = np.random.standard_normal(n)
1214
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
@@ -17,14 +19,14 @@
1719

1820
plt.subplots_adjust(hspace=0.5)
1921
plt.subplot(121)
20-
plt.hexbin(x,y)
22+
plt.hexbin(x,y, cmap=cm.jet)
2123
plt.axis([xmin, xmax, ymin, ymax])
2224
plt.title("Hexagon binning")
2325
cb = plt.colorbar()
2426
cb.set_label('counts')
2527

2628
plt.subplot(122)
27-
plt.hexbin(x,y,bins='log')
29+
plt.hexbin(x,y,bins='log', cmap=cm.jet)
2830
plt.axis([xmin, xmax, ymin, ymax])
2931
plt.title("With a log color scale")
3032
cb = plt.colorbar()

examples/pylab_examples/legend_demo.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,43 @@
44
#See http://matplotlib.sf.net/examples/legend_demo2.py for an example
55
#controlling which lines the legend uses and the order
66

7-
8-
from pylab import *
9-
10-
a = arange(0,3,.02)
11-
b = arange(0,3,.02)
12-
c=exp(a)
13-
d=c.tolist()
14-
d.reverse()
15-
d = array(d)
16-
17-
ax = subplot(111)
18-
plot(a,c,'k--',a,d,'k:',a,c+d,'k')
19-
legend(('Model length', 'Data length', 'Total message length'),
20-
'upper center', shadow=True)
21-
ax.set_ylim([-1,20])
22-
ax.grid(0)
23-
xlabel('Model complexity --->')
24-
ylabel('Message length --->')
25-
title('Minimum Message Length')
26-
setp(gca(), 'yticklabels', [])
27-
setp(gca(), 'xticklabels', [])
7+
import numpy as np
8+
import matplotlib.pyplot as plt
9+
10+
a = np.arange(0,3,.02)
11+
b = np.arange(0,3,.02)
12+
c = np.exp(a)
13+
d = c[::-1]
14+
15+
ax = plt.subplot(111)
16+
plt.plot(a,c,'k--',a,d,'k:',a,c+d,'k')
17+
plt.legend(('Model length', 'Data length', 'Total message length'),
18+
'upper center', shadow=True)
19+
plt.ylim([-1,20])
20+
plt.grid(False)
21+
plt.xlabel('Model complexity --->')
22+
plt.ylabel('Message length --->')
23+
plt.title('Minimum Message Length')
24+
25+
plt.setp(plt.gca(), 'yticklabels', [])
26+
plt.setp(plt.gca(), 'xticklabels', [])
2827

2928
# set some legend properties. All the code below is optional. The
3029
# defaults are usually sensible but if you need more control, this
3130
# shows you how
32-
leg = gca().get_legend()
31+
leg = plt.gca().get_legend()
3332
ltext = leg.get_texts() # all the text.Text instance in the legend
3433
llines = leg.get_lines() # all the lines.Line2D instance in the legend
3534
frame = leg.get_frame() # the patch.Rectangle instance surrounding the legend
3635

3736
# see text.Text, lines.Line2D, and patches.Rectangle for more info on
3837
# the settable properties of lines, text, and rectangles
3938
frame.set_facecolor('0.80') # set the frame face color to light gray
40-
setp(ltext, fontsize='small') # the legend text fontsize
41-
setp(llines, linewidth=1.5) # the legend linewidth
39+
plt.setp(ltext, fontsize='small') # the legend text fontsize
40+
plt.setp(llines, linewidth=1.5) # the legend linewidth
4241
#leg.draw_frame(False) # don't draw the legend frame
43-
#savefig('legend_demo')
44-
show()
42+
#plt.savefig('legend_demo')
43+
plt.show()
4544

4645

4746

lib/matplotlib/axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3592,7 +3592,7 @@ def legend(self, *args, **kwargs):
35923592
35933593
**Example:**
35943594
3595-
.. plot:: legend_demo.py
3595+
.. plot:: ../mpl_examples/api/legend_demo.py
35963596
"""
35973597

35983598
def get_handles():

0 commit comments

Comments
 (0)