我想添加一个仅在进度屏幕上显示的条形图。我是Kivy和Python的新手。我将matplotlib用于条形图。我想将Bar.py代码集成到我的main.py代码和progress.kv文件中。我对如何在Progress.kv文件中添加它感到困惑。
Bar.py
import matplotlib.pyplot as plt
x = [2,4,6,8,10]
y = [6,7,8,2,4]
x2 = [1,3,5,7,9]
y2 = [7,8,2,4,2]
plt.bar(x,y, label='Bars1', color='blue')
plt.bar(x2,y2, label='Bars2', color='c')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting Graph\nCheck It out')
plt.legend()
plt.show()
Main.py
from kivy.app import App
#kivy.require("1.10.0")
from kivy.uix.screenmanager import ScreenManager, Screen,
SlideTransition
from kivy.properties import ObjectProperty
from kivy.properties import ListProperty
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.graphics import *
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.config import Config
from kivy.lang import Builder
import numpy as np
from math import sin
import matplotlib.pyplot as plt
from kivy.garden.matplotlib.backend_kivyagg import
FigureCanvasKivyAgg
import matplotlib
from kivy.garden.matplotlib import FigureCanvasKivyAgg
#Screens created in kv file. Declare all screens for app.
class Login(Screen):
pass
class Connected(Screen):
pass
class Patient(Screen):
pass
class Progress(Screen):
pass
class Goals(Screen):
pass
class Training(Screen):
pass
class Range(Screen):
pass
class Trial(Screen):
pass
class Symmetry(Screen):
pass
class Coordination(Screen):
pass
class Help(Screen):
pass
class Manager(ScreenManager):
pass
class ScreenApp(App):
def build(self):
m = Manager(transition=SlideTransition())
return m
if __name__ == '__main__':
#sets pixels to pixels on Raspberry Pi tablet
Config.set('graphics', 'fullscreen', '0')
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '480')
ScreenApp().run()
Progress.kv
import matplotlib.pyplot as plt
<Progress>:
canvas.before:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
Button:
text:'Patient Menu'
font_size: 20
bold: True
size_hint: None, None
size: 300, 50
pos_hint: {'center_x': .5, 'center_y': .1 }
color: 1, 1, 1, 1
on_press: root.manager.current = 'Patient'
答案 0 :(得分:0)
<Progress>:
id: bar
添加到 BoxLayout 小部件中,以便我们可以在Python代码中引用它。id
连接到ObjectProperty bar: bar
Kv language » self.ids vs ObjectProperty
注意
尽管self.ids方法非常简洁,但通常是 被视为使用ObjectProperty的“最佳做法”。这创建了一个 直接引用,提供更快的访问权限,并且更明确。
from kivy.properties import ObjectProperty
bar = ObjectProperty(None)
的班级添加Progress()
on_pre_enter()
方法实施到类Progress()
中要声明属性,必须在类级别声明它们。的 然后,类将进行工作以实例化真实属性。 您的对象已创建。这些属性不是属性:它们是 根据您的属性创建事件的机制。
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
import matplotlib.pyplot as plt
x = [2,4,6,8,10]
y = [6,7,8,2,4]
x2 = [1,3,5,7,9]
y2 = [7,8,2,4,2]
plt.bar(x,y, label='Bars1', color='blue')
plt.bar(x2,y2, label='Bars2', color='c')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting Graph\nCheck It out')
plt.legend()
class Patient(Screen):
pass
class Progress(Screen):
bar = ObjectProperty(None)
def on_pre_enter(self, *args):
self.bar.add_widget(FigureCanvasKivyAgg(plt.gcf()))
class Manager(ScreenManager):
pass
class ProgressApp(App):
title = "Kivy Garden Matplolib & plt"
def build(self):
return Manager()
if __name__ == "__main__":
ProgressApp().run()
#:kivy 1.11.0
#:import SlideTransition kivy.uix.screenmanager.SlideTransition
<Manager>:
transition: SlideTransition()
Patient:
name: 'Patient'
Progress:
name: 'Progress'
<Patient>:
BoxLayout:
orientation: 'vertical'
Label:
text: 'Patient Screen'
font_size: '30sp'
Button:
size_hint: 1, 0.1
text: 'Goto Progress Screen'
on_release:
root.manager.current = 'Progress'
<Progress>:
bar: bar
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
id: bar
Button:
text:'Patient Menu'
font_size: 20
bold: True
size_hint: None, None
size: 300, 50
pos_hint: {'center_x': .5, 'center_y': .1 }
color: 1, 1, 1, 1
on_press: root.manager.current = 'Patient'
答案 1 :(得分:0)
条形图的Progress.kv文件
#:import MeshLinePlot kivy.garden.graph.MeshLinePlot
#:import get_color_from_hex kivy.utils
<Progress>:
BoxLayout:
orientation: 'vertical'
padding: [10,20,10,20]
spacing: 20
Graph:
height:1000
width:1000
id: graph_test
xlabel:'Algorithms'
ylabel:'Accuracy'
x_ticks_minor:5
x_tics_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:3
ymin:0
ymax:100
pos: 0, root.height
size: root.width, root.height
进步的Python代码
import itertools
from kivy.app import App
from kivy.utils import get_color_from_hex as rgb
from kivy.uix.screenmanager import Screen, SlideTransition
from kivy.properties import StringProperty,ObjectProperty
from math import sin
from kivy.garden.graph import Graph, MeshLinePlot, BarPlot
colors = itertools.cycle([rgb('7dac9f'), rgb('dc7062'), rgb('66a8d4'), rgb('e5b060')])
class Progress(Screen):
def assign(self):
super(Progress,self).__init__()
self.sensorEnabled = True
self.graph= self.ids.graph_test
self.plot = []
self.plot.append(BarPlot(color=next(colors),bar_spacing=.72)) #x - Red
self.plot.append(BarPlot(color=next(colors),bar_spacing=.72)) # Y - Green
self.plot.append(BarPlot(color=next(colors),bar_spacing=.72)) # Z - Blue
self.reset_plots(self.graph)
for plot in self.plot:
self.graph.add_plot(plot)
def reset_plots(self,graph):
for plot in self.plot:
plot.bind_to_graph(graph)
plot.points = []
self.counter = 1