在kivy.garden.graph上绘制一个椭圆

时间:2019-01-13 10:37:32

标签: python kivy

我正在使用kivy.garden.graph绘制数据图。我也想添加一些点作为椭圆,但是我只能使用“画布”坐标而不是图形坐标来添加它们。如何使用图形坐标系绘制椭圆?

我找不到能给我足够信息的方法,使我能够进行转换以将“图形”坐标转换为“画布”坐标。如果不可能仅在“图形”坐标系中绘制椭圆,这也是一种解决方案。

graph_play.py

from kivy.garden.graph import MeshLinePlot
from kivy.graphics import Ellipse, Color
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from math import sin

class RootWidget(BoxLayout):
    def __init__(self, **kwargs):
        super(RootWidget, self).__init__()

    def add_plots(self):

        with self.ids.graph.canvas:
            Color(1, 1, 0)
            Ellipse(pos=(10, 10), size=(14, 14))

        for i in range(5):
            data_to_graph = [(x, sin(x)+ i) for x in range(0, 101)] #applies a DC offset to each trace to display multiple traces
            print(data_to_graph)
            self.plot = MeshLinePlot(color=[.5, .5, 1, .5])
            self.plot.points =  data_to_graph
            self.ids.graph.add_plot(self.plot)

class GraphDemo(App):
    def build(self):
        return Builder.load_file("mainWindow_play.kv")

if __name__ == "__main__":
    GraphDemo().run()

mainWindow_play.kv

#:import MeshLinePlot kivy.garden.graph.MeshLinePlot
RootWidget:
    BoxLayout:
        orientation: "vertical"
        BoxLayout:
            size_hint: [1, .8]
            Graph:
                id: graph
                xlabel: "metres"
                ylabel: "milliSeconds"
                y_ticks_major: 10
                x_ticks_major: 10
                y_grid_label: True
                x_grid_label: True
                padding: 5
                x_grid: True
                y_grid: True
                ymin: -10
                ymax: 30
                xmin: 0
                xmax: 100
                on_parent: root.add_plots()

0 个答案:

没有答案