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

Skip to content

Commit c7367b0

Browse files
committed
Render all the fonts in each mathtext expression to a single image buffer
(memory and time savings). Add support for getting raw image data for mathtext expressions. Add mathtext_wx.py example showing how to put mathtext expressions into controls. svn path=/trunk/matplotlib/; revision=3765
1 parent d0a4cd0 commit c7367b0

1 file changed

Lines changed: 124 additions & 0 deletions

File tree

examples/mathtext_wx.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
"""
2+
Demonstrates how to convert mathtext to a wx.Bitmap for display in various
3+
controls on wxPython.
4+
"""
5+
6+
import matplotlib
7+
matplotlib.use("WxAgg")
8+
from matplotlib.numerix import arange, sin, pi, cos, log
9+
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
10+
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
11+
from matplotlib.figure import Figure
12+
13+
import wx
14+
15+
IS_GTK = 'wxGTK' in wx.PlatformInfo
16+
IS_WIN = 'wxMSW' in wx.PlatformInfo
17+
IS_MAC = 'wxMac' in wx.PlatformInfo
18+
19+
############################################################
20+
# This is where the "magic" happens.
21+
from matplotlib.mathtext import MathTextParser
22+
mathtext_parser = MathTextParser("Bitmap")
23+
def mathtext_to_wxbitmap(s):
24+
ftimage = mathtext_parser.parse(s, 150)
25+
return wx.BitmapFromBufferRGBA(
26+
ftimage.get_width(), ftimage.get_height(),
27+
ftimage.as_rgba_str())
28+
############################################################
29+
30+
functions = [
31+
(r'$\sin(2 \pi x)$' , lambda x: sin(2*pi*x)),
32+
(r'$\frac{4}{3}\pi x^3$' , lambda x: (4.0 / 3.0) * pi * x**3),
33+
(r'$\cos(2 \pi x)$' , lambda x: cos(2*pi*x)),
34+
(r'$\log x$' , lambda x: log(x))
35+
]
36+
37+
class CanvasFrame(wx.Frame):
38+
def __init__(self, parent, title):
39+
wx.Frame.__init__(self, parent, -1, title, size=(550, 350))
40+
self.SetBackgroundColour(wx.NamedColor("WHITE"))
41+
42+
self.figure = Figure()
43+
self.axes = self.figure.add_subplot(111)
44+
self.change_plot(0)
45+
46+
self.canvas = FigureCanvas(self, -1, self.figure)
47+
48+
self.sizer = wx.BoxSizer(wx.VERTICAL)
49+
self.add_buttonbar()
50+
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
51+
self.add_toolbar() # comment this out for no toolbar
52+
53+
menuBar = wx.MenuBar()
54+
55+
# File Menu
56+
menu = wx.Menu()
57+
menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample")
58+
menuBar.Append(menu, "&File")
59+
60+
if IS_GTK or IS_WIN:
61+
# Equation Menu
62+
menu = wx.Menu()
63+
for i, (mt, func) in enumerate(functions):
64+
bm = mathtext_to_wxbitmap(mt)
65+
item = wx.MenuItem(menu, 1000 + i, "")
66+
item.SetBitmap(bm)
67+
menu.AppendItem(item)
68+
self.Bind(wx.EVT_MENU, self.OnChangePlot, item)
69+
menuBar.Append(menu, "&Functions")
70+
71+
self.SetMenuBar(menuBar)
72+
73+
self.SetSizer(self.sizer)
74+
self.Fit()
75+
76+
def add_buttonbar(self):
77+
self.button_bar = wx.Panel(self)
78+
self.button_bar_sizer = wx.BoxSizer(wx.HORIZONTAL)
79+
self.sizer.Add(self.button_bar, 0, wx.LEFT | wx.TOP | wx.GROW)
80+
81+
for i, (mt, func) in enumerate(functions):
82+
bm = mathtext_to_wxbitmap(mt)
83+
button = wx.BitmapButton(self.button_bar, 1000 + i, bm)
84+
self.button_bar_sizer.Add(button, 1, wx.GROW)
85+
self.Bind(wx.EVT_BUTTON, self.OnChangePlot, button)
86+
87+
self.button_bar.SetSizer(self.button_bar_sizer)
88+
89+
def add_toolbar(self):
90+
"""Copied verbatim from embedding_wx2.py"""
91+
self.toolbar = NavigationToolbar2Wx(self.canvas)
92+
self.toolbar.Realize()
93+
if IS_MAC:
94+
self.SetToolBar(self.toolbar)
95+
else:
96+
tw, th = self.toolbar.GetSizeTuple()
97+
fw, fh = self.canvas.GetSizeTuple()
98+
self.toolbar.SetSize(wx.Size(fw, th))
99+
self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
100+
self.toolbar.update()
101+
102+
def OnPaint(self, event):
103+
self.canvas.draw()
104+
105+
def OnChangePlot(self, event):
106+
self.change_plot(event.GetId() - 1000)
107+
108+
def change_plot(self, plot_number):
109+
t = arange(1.0,3.0,0.01)
110+
s = functions[plot_number][1](t)
111+
self.axes.clear()
112+
self.axes.plot(t, s)
113+
self.Refresh()
114+
115+
class MyApp(wx.App):
116+
def OnInit(self):
117+
frame = CanvasFrame(None, "wxPython mathtext demo app")
118+
self.SetTopWindow(frame)
119+
frame.Show(True)
120+
return True
121+
122+
app = MyApp()
123+
app.MainLoop()
124+

0 commit comments

Comments
 (0)