Skip to content

Commit 92d2505

Browse files
Add files via upload
1 parent a4a1637 commit 92d2505

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sys
2+
import numpy as np
3+
import matplotlib
4+
matplotlib.use('Qt5Agg')
5+
import matplotlib.pyplot as plt
6+
from kivy.app import App
7+
from kivy.uix.boxlayout import BoxLayout
8+
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
9+
10+
class MyKivyApp(App):
11+
def build(self):
12+
layout = BoxLayout(orientation='vertical')
13+
14+
# Example data
15+
x = np.linspace(0, 2 * np.pi, 100)
16+
y = np.sin(x)
17+
18+
# Create a Matplotlib figure
19+
fig, ax = plt.subplots()
20+
ax.plot(x, y, label='Sine Wave')
21+
ax.set_xlabel('X-axis')
22+
ax.set_ylabel('Y-axis')
23+
ax.set_title('Matplotlib Plot in Kivy')
24+
ax.grid(True)
25+
ax.legend()
26+
27+
# Add the Matplotlib canvas to the Kivy layout
28+
canvas = FigureCanvasKivyAgg(fig)
29+
layout.add_widget(canvas)
30+
31+
return layout
32+
33+
if __name__ == '__main__':
34+
MyKivyApp().run()
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import matplotlib
2+
matplotlib.use('Qt5Agg')
3+
import matplotlib.pyplot as plt
4+
from kivy.app import App
5+
import numpy as np
6+
from kivy.lang import Builder
7+
from kivy_matplotlib import MatplotFigure, MatplotNavToolbar
8+
9+
kv = """
10+
BoxLayout:
11+
orientation: 'vertical'
12+
13+
MatplotFigure:
14+
id: figure_wgt
15+
size_hint: 1, 0.7
16+
17+
MatplotNavToolbar:
18+
id: navbar_wgt
19+
size_hint: 1, 0.3
20+
figure_widget: figure_wgt
21+
"""
22+
23+
class TestApp(App):
24+
title = "Test Matplotlib"
25+
26+
def build(self):
27+
# Matplotlib stuff, figure and plot
28+
fig = plt.figure(figsize=(2, 2))
29+
t = np.arange(0.0, 100.0, 0.01)
30+
s = np.sin(0.08 * np.pi * t)
31+
axes = fig.gca()
32+
axes.plot(t, s)
33+
axes.set_xlim(0, 50)
34+
axes.grid(True)
35+
36+
# Kivy stuff
37+
root = Builder.load_string(kv)
38+
figure_wgt = root.ids['figure_wgt'] # MatplotFigure
39+
figure_wgt.figure = fig
40+
41+
return root
42+
43+
if __name__ == '__main__':
44+
TestApp().run()

0 commit comments

Comments
 (0)