Skip to content

Instantly share code, notes, and snippets.

@matham
Created March 2, 2015 22:25
Show Gist options
  • Save matham/e8418bd7c8fecd393379 to your computer and use it in GitHub Desktop.
Save matham/e8418bd7c8fecd393379 to your computer and use it in GitHub Desktop.
subplots
from kivy.app import runTouchApp
from kivy.garden.graph import Graph
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty, ListProperty, DictProperty
from kivy.clock import Clock
class GraphContainer(GridLayout):
finish_redraw_trigger = None
def __init__(self, **kwargs):
super(GraphContainer, self).__init__(**kwargs)
self.cols = 1
self.finish_redraw_trigger = Clock.create_trigger(
self._finish_plot_redraw)
def _finish_plot_redraw(self, *largs):
graphs = self.children
x1 = max([g.plot_area_size[0] for g in graphs if g.plot_area_size is not None])
x2 = min([g.plot_area_size[2] for g in graphs if g.plot_area_size is not None])
for graph in graphs:
if graph.plot_area_size is None:
continue
_, y1, _, y2 = graph.plot_area_size
graph._finish_redraw_size((x1, y1, x2, y2))
class SubplottedGraph(Graph):
subplots_container = ObjectProperty(None)
plot_area_size = ObjectProperty(None)
def _redraw_size(self, *args):
self.plot_area_size = self._update_labels()
self.subplots_container.finish_redraw_trigger()
def _finish_redraw_size(self, size):
self._plot_area.pos = (size[0], size[1])
self._plot_area.size = (size[2] - size[0], size[3] - size[1])
self._fbo.size = self.size
self._fbo_rect.texture = self._fbo.texture
self._fbo_rect.size = self.size
self._fbo_rect.pos = self.pos
self._background_rect.size = self.size
self._update_ticks(size)
self._update_plots(size)
if __name__ == '__main__':
from kivy.garden.graph import MeshLinePlot
from math import sin
container = GraphContainer()
graph = SubplottedGraph(
subplots_container=container,
xlabel='X', ylabel='Y', x_ticks_minor=5, x_ticks_major=25,
y_ticks_major=1, y_grid_label=True, x_grid_label=True, padding=5,
x_grid=True, y_grid=True, xmin=-0, xmax=100, ymin=-1, ymax=1)
plot = MeshLinePlot(color=[1, 0, 0, 1])
plot.points = [(x, sin(x / 10.)) for x in range(0, 101)]
graph.add_plot(plot)
container.add_widget(graph)
graph = SubplottedGraph(
subplots_container=container,
ylabel='Y', x_ticks_minor=5, x_ticks_major=25,
y_ticks_major=10, y_grid_label=True, padding=5,
x_grid=True, y_grid=True, xmin=-0, xmax=100, ymin=-10, ymax=10)
plot = MeshLinePlot(color=[0, 1, 0, 1])
plot.points = [(x, 10 * sin((x + 50) / 10.)) for x in range(0, 101)]
graph.add_plot(plot)
container.add_widget(graph, 1)
runTouchApp(container)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment