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

Skip to content

Commit bbb2592

Browse files
committed
updated users guide
svn path=/trunk/matplotlib/; revision=584
1 parent 9a8f491 commit bbb2592

File tree

6 files changed

+116
-12
lines changed

6 files changed

+116
-12
lines changed

CXX/Objects.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2268,7 +2268,7 @@ namespace Py
22682268
}
22692269

22702270
const_iterator (const MapBase<T>* m, List k, List::iterator p )
2271-
: map(s), keys(k), pos(p)
2271+
: map(m), keys(k), pos(p)
22722272
{}
22732273

22742274
const_iterator(const const_iterator& other)

examples/mathtext_demo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
ylabel(r'$\Delta_{i+1}^j$', fontsize=20)
2020
tex = r'$\cal{R}\prod_{i=\alpha_{i+1}}^\infty a_i\rm{sin}(2 \pi f x_i)$'
2121
text(1, 1.6, tex, fontsize=20)
22-
title(r'$\Delta_i^j \hspace{0.4} \rm{versus} \hspace{0.4} \Delta_{i+1}^j$', fontsize=20)
22+
23+
#title(r'$\Delta_i^j \hspace{0.4} \rm{versus} \hspace{0.4} \Delta_{i+1}^j$', fontsize=20)
2324
savefig('mathtext_demo')
2425

2526
show()

lib/matplotlib/backends/backend_svg.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
from matplotlib._matlab_helpers import Gcf
1414
from matplotlib import rcParams
1515

16+
try: from matplotlib.mathtext import math_parse_s_ft2font_svg
17+
except ImportError:
18+
print >>sys.stderr, 'backend_svg could not import mathtext (build with ft2font)'
19+
useMathText = False
20+
else: useMathText = True
21+
1622
import sys,os,math
1723

1824
def error_msg_svg(msg, *args):
@@ -103,7 +109,10 @@ def get_text_width_height(self, s, prop, ismath):
103109
get the width and height in display coords of the string s
104110
with FontPropertry prop
105111
"""
106-
if ismath: s = self.strip_math(s)
112+
if ismath and useMathText:
113+
width, height, glyphs = math_parse_s_ft2font_svg(
114+
s, 72, prop.get_size_in_points())
115+
return width, height
107116
font = self._get_font(prop)
108117
font.set_text(s, 0.0)
109118
w, h = font.get_width_height()
@@ -172,11 +181,38 @@ def draw_point(self, gc, x, y):
172181
"""
173182
self.draw_arc(gc, gc.get_rgb(), x, y, 1, 0, 0, 0) # result seems to have a hole in it...
174183

184+
def draw_mathtext(self, gc, x, y, s, prop, angle):
185+
"""
186+
Draw math text using matplotlib.mathtext
187+
"""
188+
fontsize = prop.get_size_in_points()
189+
width, height, svg_glyphs = math_parse_s_ft2font_svg(s, 72, fontsize)
190+
color = rgb2hex(gc.get_rgb())
191+
192+
svg = ""
193+
self.open_group("mathtext")
194+
for fontname, fontsize, num, ox, oy, metrics in svg_glyphs:
195+
thetext=unichr(num)
196+
thetext.encode('utf-8')
197+
style = 'font-size: %f; font-family: %s; stroke-width: 0.5; stroke: %s; fill: %s;'%(fontsize, fontname, color, color)
198+
if angle!=0:
199+
transform = 'transform="translate(%f,%f) rotate(%1.1f) translate(%f,%f)"' % (x,y,-angle,-x,-y) # Inkscape doesn't support rotate(angle x y)
200+
else: transform = ''
201+
newx, newy = x+ox, y-oy
202+
svg += """\
203+
<text style="%(style)s" x="%(newx)f" y="%(newy)f" %(transform)s>%(thetext)s</text>
204+
""" % locals()
205+
206+
self._draw_rawsvg(svg.encode('utf-8'))
207+
self.close_group("mathtext")
208+
175209
def draw_text(self, gc, x, y, s, prop, angle, ismath):
176210
"""
177211
draw text
178212
"""
179-
if ismath: s = self.strip_math(s)
213+
if ismath and useMathText:
214+
return self.draw_mathtext(gc, x, y, s, prop, angle)
215+
180216
font = self._get_font(prop)
181217

182218
fontsize = prop.get_size_in_points()

lib/matplotlib/legend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def _get_handles(self, handles, texts):
179179
legline.set_markersize(0.6*legline.get_markersize())
180180
legline.set_data_clipping(False)
181181
ret.append(legline)
182-
elif isinstance(handle, Rectangle):
182+
elif isinstance(handle, Patch):
183183

184184
p = Rectangle(xy=(min(self._xdata), y-3/4*HEIGHT),
185185
width = self.HANDLELEN, height=HEIGHT/2,

lib/matplotlib/mathtext.py

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,18 @@ class BakomaTrueTypeFonts(Fonts):
232232
None : 'cmmi10',
233233
}
234234

235-
def __init__(self):
235+
def __init__(self, useSVG=False):
236236
self.glyphd = {}
237237
self.fonts = dict(
238238
[ (name, FT2Font(os.path.join(self.basepath, name) + '.ttf'))
239239
for name in self.fnames])
240240

241241
for font in self.fonts.values():
242242
font.clear()
243+
if useSVG:
244+
self.svg_glyphs=[] # a list of "glyphs" we need to render this thing in SVG
245+
else: pass
246+
self.usingSVG = useSVG
243247

244248
def get_metrics(self, font, sym, fontsize, dpi):
245249
cmfont, metrics, glyph, offset = \
@@ -299,9 +303,22 @@ def render(self, ox, oy, font, sym, fontsize, dpi):
299303
cmfont, metrics, glyph, offset = \
300304
self._get_info(font, sym, fontsize, dpi)
301305

302-
cmfont.draw_glyph_to_bitmap(
303-
int(ox), int(self.height - oy - metrics.ymax), glyph)
304-
306+
if not self.usingSVG:
307+
cmfont.draw_glyph_to_bitmap(
308+
int(ox), int(self.height - oy - metrics.ymax), glyph)
309+
else:
310+
oy += offset - 512/2048.*10.
311+
basename = self.fontmap[font]
312+
if latex_to_bakoma.has_key(sym):
313+
basename, num = latex_to_bakoma[sym]
314+
num = self.fonts[basename].get_charmap()[num]
315+
elif len(sym) == 1:
316+
num = ord(sym)
317+
else:
318+
num = 0
319+
print >>sys.stderr, 'unrecognized symbol "%s"' % sym
320+
self.svg_glyphs.append((basename, fontsize, num, ox, oy, metrics))
321+
305322

306323
class BakomaPSFonts(Fonts):
307324
"""
@@ -960,6 +977,57 @@ def math_parse_s_ft2font(s, dpi, fontsize, angle=0):
960977

961978
math_parse_s_ft2font.cache = {}
962979

980+
def math_parse_s_ft2font_svg(s, dpi, fontsize, angle=0):
981+
"""
982+
Parse the math expression s, return the (bbox, fonts) tuple needed
983+
to render it.
984+
985+
fontsize must be in points
986+
987+
return is width, height, fonts
988+
"""
989+
990+
major, minor1, minor2, tmp, tmp = sys.version_info
991+
if major==2 and minor1==2:
992+
print >> sys.stderr, 'mathtext broken on python2.2. We hope to get this fixed soon'
993+
sys.exit()
994+
cacheKey = (s, dpi, fontsize, angle)
995+
s = s[1:-1] # strip the $ from front and back
996+
if math_parse_s_ft2font_svg.cache.has_key(cacheKey):
997+
w, h, svg_glyphs = math_parse_s_ft2font_svg.cache[cacheKey]
998+
return w, h, svg_glyphs
999+
1000+
bakomaFonts = BakomaTrueTypeFonts(useSVG=True)
1001+
Element.fonts = bakomaFonts
1002+
handler.clear()
1003+
expression.parseString( s )
1004+
1005+
handler.expr.set_size_info(fontsize, dpi)
1006+
1007+
# set the origin once to allow w, h compution
1008+
handler.expr.set_origin(0, 0)
1009+
xmin = min([e.xmin() for e in handler.symbols])
1010+
xmax = max([e.xmax() for e in handler.symbols])
1011+
ymin = min([e.ymin() for e in handler.symbols])
1012+
ymax = max([e.ymax() for e in handler.symbols])
1013+
1014+
# now set the true origin - doesn't affect with and height
1015+
w, h = xmax-xmin, ymax-ymin
1016+
# a small pad for the canvas size
1017+
w += 2
1018+
h += 2
1019+
1020+
handler.expr.set_origin(0, h-ymax)
1021+
1022+
Element.fonts.set_canvas_size(w,h)
1023+
handler.expr.render()
1024+
handler.clear()
1025+
1026+
math_parse_s_ft2font_svg.cache[cacheKey] = w, h, bakomaFonts.svg_glyphs
1027+
return w, h, bakomaFonts.svg_glyphs
1028+
1029+
math_parse_s_ft2font_svg.cache = {}
1030+
9631031

9641032
def math_parse_s_ps(s, dpi, fontsize):
9651033
"""

lib/matplotlib/ticker.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
locating and formatting. Although the locators know nothing about
77
major or minor ticks, they are used by the Axis class to support major
88
and minor tick locating and formatting. Generic tick locators and
9-
formatters are provided, as well as domain specific custom locators an
10-
formatters.
9+
formatters are provided, as well as domain specific custom ones..
1110
1211
1312
Tick locating
1413
-------------
1514
1615
The Locator class is the base class for all tick locators. The
1716
locators handle autoscaling of the view limits based on the data
18-
limits, and choosing the tick locations. The most generally useful
17+
limits, and the choosing of tick locations. The most generally useful
1918
tick locator is MultipleLocator. You initialize this with a base, eg
2019
10, and it picks axis limits and ticks that are multiples of your
2120
base. The class AutoLocator contains a MultipleLocator instance, and

0 commit comments

Comments
 (0)