|
| 1 | +print('importing numpy') |
| 2 | +import numpy as np |
| 3 | +print('imported numpy') |
| 4 | + |
| 5 | +print('importing matplotlib') |
| 6 | + |
| 7 | +import matplotlib |
| 8 | +print('imported matplotlib') |
| 9 | + |
| 10 | +print('importing pyplot') |
| 11 | + |
| 12 | +from matplotlib import pyplot as plt |
| 13 | + |
| 14 | +print('imported pyplot') |
| 15 | + |
| 16 | +fig, ax = plt.subplots() |
| 17 | + |
| 18 | +print('created fig and ax') |
| 19 | + |
| 20 | +ax.plot(np.random.random(50)) |
| 21 | + |
| 22 | +print('plotted something') |
| 23 | + |
| 24 | +ax.set_xlabel('test label') |
| 25 | + |
| 26 | +print('set a label') |
| 27 | + |
| 28 | +fig.set_size_inches((5, 4)) |
| 29 | +fig.savefig('test.png') |
| 30 | + |
| 31 | +print('saved fig') |
| 32 | + |
| 33 | +from kivy.app import App |
| 34 | +from kivy.uix.image import Image |
| 35 | +from kivy.lang import Builder |
| 36 | + |
| 37 | +class MatplotlibApp(App): |
| 38 | + def build(self): |
| 39 | + root = Builder.load_string(""" |
| 40 | +BoxLayout: |
| 41 | + orientation: 'vertical' |
| 42 | + Image: |
| 43 | + id: the_image |
| 44 | + source: 'test.png' |
| 45 | + allow_stretch: True |
| 46 | + Button: |
| 47 | + size_hint_y: None |
| 48 | + height: dp(40) |
| 49 | + text: 'new plot' |
| 50 | + on_release: app.generate_new_plot() |
| 51 | + """) |
| 52 | + return root |
| 53 | + |
| 54 | + def generate_new_plot(self): |
| 55 | + fig, ax = plt.subplots() |
| 56 | + ax.set_xlabel('test xlabel') |
| 57 | + ax.set_ylabel('test ylabel') |
| 58 | + ax.plot(np.random.random(50)) |
| 59 | + ax.plot(np.sin(np.linspace(0, 3*np.pi, 30))) |
| 60 | + |
| 61 | + ax.legend(['random numbers', 'sin']) |
| 62 | + |
| 63 | + fig.set_size_inches((5, 4)) |
| 64 | + fig.tight_layout() |
| 65 | + |
| 66 | + fig.savefig('test.png', dpi=150) |
| 67 | + |
| 68 | + self.root.ids.the_image.reload() |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +MatplotlibApp().run() |
| 74 | +runTouchApp(Image(source='test.png', allow_stretch=True)) |
0 commit comments