Skip to content

Commit 2a4ad7c

Browse files
Add files via upload
1 parent 92d2505 commit 2a4ad7c

7 files changed

+424
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import os
2+
import sys
3+
import matplotlib
4+
matplotlib.use('WXAgg')
5+
from matplotlib.figure import Figure
6+
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
7+
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar2Wx
8+
from matplotlib.mathtext import MathTextParser
9+
from numpy import arange, sin, pi, cos, log
10+
import wx
11+
12+
class CanvasFrame(wx.Frame):
13+
def __init__(self, parent, title):
14+
wx.Frame.__init__(self, parent, -1, title, size=(550, 350))
15+
self.figure = Figure()
16+
self.axes = self.figure.add_subplot(111)
17+
self.canvas = FigureCanvas(self, -1, self.figure)
18+
self.change_plot(0)
19+
self.sizer = wx.BoxSizer(wx.VERTICAL)
20+
self.add_buttonbar()
21+
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
22+
self.add_toolbar()
23+
self.SetSizer(self.sizer)
24+
self.Fit()
25+
26+
def OnChangePlot(self, event):
27+
self.change_plot(event.GetId() - 1000)
28+
29+
def change_plot(self, plot_number):
30+
t = arange(1.0, 3.0, 0.01)
31+
s = functionsplot_number
32+
self.axes.clear()
33+
self.axes.plot(t, s)
34+
self.canvas.draw()
35+
36+
class MyCanvasPanel(wx.Panel):
37+
def __init__(self, parent):
38+
wx.Panel.__init__(self, parent)
39+
self.figure = Figure()
40+
self.axes = self.figure.add_subplot(111)
41+
self.canvas = FigureCanvas(self, -1, self.figure)
42+
self.toolbar = NavigationToolbar2Wx(self.canvas)
43+
self.toolbar.Realize()
44+
45+
def DoLayout(self):
46+
sizer = wx.BoxSizer(wx.VERTICAL)
47+
sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
48+
sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
49+
self.SetSizer(sizer)
50+
self.Fit()
51+
52+
def Draw(self):
53+
t = arange(0.0, 3.0, 0.01)
54+
s = sin(2 * pi * t)
55+
self.axes.plot(t, s)
56+
57+
class MyFrame(wx.Frame):
58+
def __init__(self, title):
59+
wx.Frame.__init__(self, None, -1, title, size=(620, 620))
60+
self.panel = MyCanvasPanel(self)
61+
self.panel.Draw()
62+
63+
class MyApp(wx.App):
64+
def OnInit(self):
65+
frame = MyFrame("Sample one")
66+
self.SetTopWindow(frame)
67+
frame.Show(True)
68+
return True
69+
70+
def main():
71+
app = MyApp(False)
72+
app.MainLoop()
73+
74+
if __name__ == "__main__":
75+
main()
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
"""
2+
===========
3+
MathText WX
4+
===========
5+
6+
Demonstrates how to convert mathtext to a wx.Bitmap for display in various
7+
controls on wxPython.
8+
"""
9+
10+
import matplotlib
11+
matplotlib.use("WxAgg")
12+
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
13+
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
14+
from matplotlib.figure import Figure
15+
import numpy as np
16+
17+
import wx
18+
19+
IS_GTK = 'wxGTK' in wx.PlatformInfo
20+
IS_WIN = 'wxMSW' in wx.PlatformInfo
21+
22+
############################################################
23+
# This is where the "magic" happens.
24+
from matplotlib.mathtext import MathTextParser
25+
mathtext_parser = MathTextParser("Bitmap")
26+
27+
28+
def mathtext_to_wxbitmap(s):
29+
rgba, depth = mathtext_parser.to_rgba(s, dpi=150, fontsize=10)
30+
return wx.Bitmap.FromBufferRGBA(rgba.shape[1], rgba.shape[0], rgba)
31+
############################################################
32+
33+
functions = [
34+
(r'$\sin(2 \pi x)$', lambda x: np.sin(2*np.pi*x)),
35+
(r'$\frac{4}{3}\pi x^3$', lambda x: (4.0/3.0)*np.pi*x**3),
36+
(r'$\cos(2 \pi x)$', lambda x: np.cos(2*np.pi*x)),
37+
(r'$\log(x)$', lambda x: np.log(x))
38+
]
39+
40+
41+
class CanvasFrame(wx.Frame):
42+
def __init__(self, parent, title):
43+
wx.Frame.__init__(self, parent, -1, title, size=(550, 350))
44+
45+
self.figure = Figure()
46+
self.axes = self.figure.add_subplot(111)
47+
48+
self.canvas = FigureCanvas(self, -1, self.figure)
49+
50+
self.change_plot(0)
51+
52+
self.sizer = wx.BoxSizer(wx.VERTICAL)
53+
self.add_buttonbar()
54+
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
55+
self.add_toolbar() # comment this out for no toolbar
56+
57+
menuBar = wx.MenuBar()
58+
59+
# File Menu
60+
menu = wx.Menu()
61+
m_exit = menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample")
62+
menuBar.Append(menu, "&File")
63+
self.Bind(wx.EVT_MENU, self.OnClose, m_exit)
64+
65+
if IS_GTK or IS_WIN:
66+
# Equation Menu
67+
menu = wx.Menu()
68+
for i, (mt, func) in enumerate(functions):
69+
bm = mathtext_to_wxbitmap(mt)
70+
item = wx.MenuItem(menu, 1000 + i, " ")
71+
item.SetBitmap(bm)
72+
menu.Append(item)
73+
self.Bind(wx.EVT_MENU, self.OnChangePlot, item)
74+
menuBar.Append(menu, "&Functions")
75+
76+
self.SetMenuBar(menuBar)
77+
78+
self.SetSizer(self.sizer)
79+
self.Fit()
80+
81+
def add_buttonbar(self):
82+
self.button_bar = wx.Panel(self)
83+
self.button_bar_sizer = wx.BoxSizer(wx.HORIZONTAL)
84+
self.sizer.Add(self.button_bar, 0, wx.LEFT | wx.TOP | wx.GROW)
85+
86+
for i, (mt, func) in enumerate(functions):
87+
bm = mathtext_to_wxbitmap(mt)
88+
button = wx.BitmapButton(self.button_bar, 1000 + i, bm)
89+
self.button_bar_sizer.Add(button, 1, wx.GROW)
90+
self.Bind(wx.EVT_BUTTON, self.OnChangePlot, button)
91+
92+
self.button_bar.SetSizer(self.button_bar_sizer)
93+
94+
def add_toolbar(self):
95+
"""Copied verbatim from embedding_wx2.py"""
96+
self.toolbar = NavigationToolbar2Wx(self.canvas)
97+
self.toolbar.Realize()
98+
# By adding toolbar in sizer, we are able to put it at the bottom
99+
# of the frame - so appearance is closer to GTK version.
100+
self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
101+
# update the axes menu on the toolbar
102+
self.toolbar.update()
103+
104+
def OnChangePlot(self, event):
105+
self.change_plot(event.GetId() - 1000)
106+
107+
def change_plot(self, plot_number):
108+
t = np.arange(1.0, 3.0, 0.01)
109+
s = functions[plot_number][1](t)
110+
self.axes.clear()
111+
self.axes.plot(t, s)
112+
self.canvas.draw()
113+
114+
def OnClose(self, event):
115+
self.Destroy()
116+
117+
118+
class MyApp(wx.App):
119+
def OnInit(self):
120+
frame = CanvasFrame(None, "wxPython mathtext demo app")
121+
self.SetTopWindow(frame)
122+
frame.Show(True)
123+
return True
124+
125+
app = MyApp()
126+
app.MainLoop()
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import wx
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
5+
from matplotlib.figure import Figure
6+
7+
class MyFrame(wx.Frame):
8+
def __init__(self, parent, title):
9+
super(MyFrame, self).__init__(parent, title=title, size=(600, 400))
10+
11+
# Create a panel
12+
self.panel = wx.Panel(self)
13+
14+
# Create a Matplotlib figure
15+
self.figure = Figure()
16+
self.axes = self.figure.add_subplot(111)
17+
18+
# Generate some data
19+
x = np.linspace(0, 10, 100)
20+
y = np.sin(x)
21+
22+
# Plot the data
23+
self.axes.plot(x, y)
24+
25+
# Create a FigureCanvas
26+
self.canvas = FigureCanvas(self.panel, -1, self.figure)
27+
28+
# Create a vertical box sizer to manage layout
29+
self.sizer = wx.BoxSizer(wx.VERTICAL)
30+
self.sizer.Add(self.canvas, 1, wx.EXPAND | wx.ALL, 5)
31+
32+
# Set sizer for the panel
33+
self.panel.SetSizer(self.sizer)
34+
35+
# Add the panel to the frame
36+
self.sizer.Fit(self)
37+
self.Centre()
38+
39+
class MyApp(wx.App):
40+
def OnInit(self):
41+
frame = MyFrame(None, title='Matplotlib with wxPython')
42+
frame.Show(True)
43+
return True
44+
45+
if __name__ == '__main__':
46+
app = MyApp(False)
47+
app.MainLoop()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import matplotlib
2+
matplotlib.use('Qt5Agg') # Use Qt5Agg backend
3+
import sys
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
7+
from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas
8+
9+
class MainWindow(QMainWindow):
10+
def __init__(self):
11+
super().__init__()
12+
self.setWindowTitle("Matplotlib with PySide")
13+
self.setGeometry(100, 100, 800, 600)
14+
15+
# Create a central widget
16+
central_widget = QWidget()
17+
self.setCentralWidget(central_widget)
18+
19+
# Create a layout for the central widget
20+
layout = QVBoxLayout()
21+
central_widget.setLayout(layout)
22+
23+
# Create a Matplotlib figure and canvas
24+
self.figure = plt.figure(figsize=(5, 4), dpi=100)
25+
self.canvas = FigureCanvas(self.figure)
26+
layout.addWidget(self.canvas)
27+
28+
# Plot initial data
29+
self.plot_data()
30+
31+
def plot_data(self):
32+
# Generate some sample data
33+
x = np.linspace(0, 10, 100)
34+
y = np.sin(x)
35+
36+
# Clear previous plot
37+
self.figure.clear()
38+
39+
# Plot data using Matplotlib
40+
ax = self.figure.add_subplot(111)
41+
ax.plot(x, y)
42+
ax.set_xlabel('X-axis')
43+
ax.set_ylabel('Y-axis')
44+
ax.set_title('Matplotlib Plot')
45+
46+
# Update canvas
47+
self.canvas.draw()
48+
49+
def main():
50+
app = QApplication(sys.argv)
51+
window = MainWindow()
52+
window.show()
53+
sys.exit(app.exec_())
54+
55+
if __name__ == "__main__":
56+
main()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import matplotlib
2+
matplotlib.use('Qt5Agg')
3+
import PySimpleGUI as sg
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
7+
8+
# Generate some sample data
9+
x_values = np.linspace(0, 10, 100)
10+
y_values = np.sin(x_values)
11+
12+
# Create a Matplotlib figure and plot
13+
fig, ax = plt.subplots(figsize=(6, 4))
14+
ax.plot(x_values, y_values, label="sin(x)")
15+
ax.set_xlabel("X")
16+
ax.set_ylabel("Y")
17+
ax.set_title("Matplotlib Plot in PySimpleGUI")
18+
19+
# Create the PySimpleGUI layout
20+
layout = [
21+
[sg.Canvas(key="-CANVAS-")],
22+
[sg.Button("Exit")]
23+
]
24+
25+
# Create the PySimpleGUI window
26+
window = sg.Window("Matplotlib Plot Example", layout, finalize=True)
27+
28+
# Embed the Matplotlib plot in the PySimpleGUI canvas
29+
canvas_elem = window["-CANVAS-"]
30+
canvas = FigureCanvasTkAgg(fig, master=canvas_elem.Widget)
31+
canvas.draw()
32+
canvas.get_tk_widget().pack(side="top", fill="both", expand=1)
33+
34+
# Event loop
35+
while True:
36+
event, values = window.read()
37+
if event == sg.WIN_CLOSED or event == "Exit":
38+
break
39+
40+
window.close()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import matplotlib
2+
matplotlib.use('Qt5Agg')
3+
import PySimpleGUI as sg
4+
import matplotlib.pyplot as plt
5+
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
6+
7+
def draw_plot(window):
8+
# Generate some sample data
9+
x = [1, 2, 3, 4, 5]
10+
y = [10, 5, 15, 7, 10]
11+
12+
# Create a Matplotlib figure and plot
13+
fig, ax = plt.subplots()
14+
ax.plot(x, y, marker='o')
15+
ax.set_xlabel('X-axis')
16+
ax.set_ylabel('Y-axis')
17+
ax.set_title('Matplotlib Plot')
18+
19+
# Convert Matplotlib figure to Tkinter canvas
20+
canvas = FigureCanvasTkAgg(fig, master=window['-CANVAS-'].TKCanvas)
21+
22+
# Update the PySimpleGUI window with the Matplotlib canvas
23+
canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
24+
canvas.draw()
25+
26+
while True:
27+
event, values = window.read()
28+
if event == sg.WINDOW_CLOSED or event == 'Close':
29+
break
30+
31+
plt.close(fig)
32+
33+
def main():
34+
# Define PySimpleGUI layout
35+
layout = [[sg.Canvas(key='-CANVAS-')],
36+
[sg.Button('Close')]]
37+
38+
# Create PySimpleGUI window
39+
window = sg.Window('Matplotlib with PySimpleGUI', layout, finalize=True, resizable=True)
40+
41+
# Call draw_plot function with the PySimpleGUI window
42+
draw_plot(window)
43+
44+
# Close the PySimpleGUI window
45+
window.close()
46+
47+
if __name__ == '__main__':
48+
main()

0 commit comments

Comments
 (0)