如何根据同一轴上的列表值绘制多条线,在 pysimplegui 上,在 python
How to plot multiple lines according to values of lists on the same axis, on pysimplegui, on python
我有一个项目,我在其中找到用户的心率,并且必须将其绘制在图表上,以对照我根据另一个心率找到的目标区域。
我从 pysimplegui 演示中获得了用于在折线图上绘制值列表的代码,但是我很难同时绘制三个不同的列表。
我需要绘制的列表如下:
列表HR
PlotMax(直线)
PlotMin(直线)
import PySimpleGUI as sg
from PySimpleGUI.PySimpleGUI import execute_py_file
import json
import datetime
plotMin = [123]*60
plotMax = [200]*60
listBPM = [65,67,70,72,80,85,90,95,110,115,130]
BAR_WIDTH = 10
# BAR_WIDTH = 5
BAR_SPACING = 16
# BAR_SPACING = 6
EDGE_OFFSET = 3
GRAPH_SIZE = (550,500)
DATA_SIZE = (500,200)
graph = sg.Graph(GRAPH_SIZE, (0,-30), DATA_SIZE, background_color='white',)
sg.theme('DarkAmber') # Add a touch of color
layout = [[sg.Text('chart demo')],
[graph],
[sg.Button('OK')]]
window = sg.Window('Window Title', layout)
before_value = 0
while True:
event, values = window.Read()
graph.Erase()
if event is None:
break
for i in range(0,len(listBPM)-1): # have to find out how to plot three different lines
graph_value = listBPM[i+1] #variable not changing...
if i > 0:
graph.DrawLine(((i-1) * BAR_SPACING + EDGE_OFFSET+ BAR_WIDTH/2 , before_value) , (i * BAR_SPACING + EDGE_OFFSET+ BAR_WIDTH/2 , graph_value ),color='blue', width=1 )
graph.DrawText(text=graph_value, location=(i*BAR_SPACING+EDGE_OFFSET+2, graph_value+10))
graph.DrawPoint((i * BAR_SPACING + EDGE_OFFSET+ BAR_WIDTH/2 ,graph_value), size=3 ,color='blue',)
before_value = graph_value
window.Close()
画个图,可能有点担心,
- 通过转换函数生成所有点
- 找到 x 和 y 的范围以确认图形的视图
- 要绘制连线或轨迹,您可以调用
sg.Graph
的方法draw_lines
演示代码
import PySimpleGUI as sg
def new_point(i, y):
return (i * BAR_SPACING + EDGE_OFFSET + BAR_WIDTH/2, y)
listBPM = [65,67,70,72,80,85,90,95,110,115,130]
plotMin = [123]*len(listBPM)
plotMax = [200]*len(listBPM)
BAR_WIDTH = 10
BAR_SPACING = 16
EDGE_OFFSET = 3
GRAPH_SIZE = (550,500)
# Generate all data points
lines = [
[new_point(i, y) for i, y in enumerate(listBPM)],
[new_point(i, y) for i, y in enumerate(plotMin)],
[new_point(i, y) for i, y in enumerate(plotMax)],
]
sg.theme('DarkAmber')
# Find the range for data
min_x, min_y, max_x, max_y = 10000, 10000, 0, 0
for line in lines:
for x, y in line:
min_x, min_y = min(min_x, x), min(min_y, y)
max_x, max_y = max(max_x, x), max(max_y, y)
graph = sg.Graph(
GRAPH_SIZE,
(min_x - EDGE_OFFSET - BAR_WIDTH/2, min_y - EDGE_OFFSET - BAR_WIDTH/2),
(max_x + EDGE_OFFSET + BAR_WIDTH/2, max_y + EDGE_OFFSET + BAR_WIDTH/2),
background_color='white')
layout = [
[sg.Text('chart demo'), sg.Button('OK')],
[graph],
]
window = sg.Window('Window Title', layout)
while True:
event, values = window.read()
if event is None:
break
elif event == 'OK':
graph.erase()
for line in lines:
graph.draw_lines(line, color='blue', width=1)
for x, y in lines[0]:
graph.draw_text(text=y, location=(x, y+10))
graph.draw_point((x, y), size=3 ,color='blue')
window.Close()
我有一个项目,我在其中找到用户的心率,并且必须将其绘制在图表上,以对照我根据另一个心率找到的目标区域。
我从 pysimplegui 演示中获得了用于在折线图上绘制值列表的代码,但是我很难同时绘制三个不同的列表。
我需要绘制的列表如下: 列表HR PlotMax(直线) PlotMin(直线)
import PySimpleGUI as sg
from PySimpleGUI.PySimpleGUI import execute_py_file
import json
import datetime
plotMin = [123]*60
plotMax = [200]*60
listBPM = [65,67,70,72,80,85,90,95,110,115,130]
BAR_WIDTH = 10
# BAR_WIDTH = 5
BAR_SPACING = 16
# BAR_SPACING = 6
EDGE_OFFSET = 3
GRAPH_SIZE = (550,500)
DATA_SIZE = (500,200)
graph = sg.Graph(GRAPH_SIZE, (0,-30), DATA_SIZE, background_color='white',)
sg.theme('DarkAmber') # Add a touch of color
layout = [[sg.Text('chart demo')],
[graph],
[sg.Button('OK')]]
window = sg.Window('Window Title', layout)
before_value = 0
while True:
event, values = window.Read()
graph.Erase()
if event is None:
break
for i in range(0,len(listBPM)-1): # have to find out how to plot three different lines
graph_value = listBPM[i+1] #variable not changing...
if i > 0:
graph.DrawLine(((i-1) * BAR_SPACING + EDGE_OFFSET+ BAR_WIDTH/2 , before_value) , (i * BAR_SPACING + EDGE_OFFSET+ BAR_WIDTH/2 , graph_value ),color='blue', width=1 )
graph.DrawText(text=graph_value, location=(i*BAR_SPACING+EDGE_OFFSET+2, graph_value+10))
graph.DrawPoint((i * BAR_SPACING + EDGE_OFFSET+ BAR_WIDTH/2 ,graph_value), size=3 ,color='blue',)
before_value = graph_value
window.Close()
画个图,可能有点担心,
- 通过转换函数生成所有点
- 找到 x 和 y 的范围以确认图形的视图
- 要绘制连线或轨迹,您可以调用
sg.Graph
的方法
draw_lines
演示代码
import PySimpleGUI as sg
def new_point(i, y):
return (i * BAR_SPACING + EDGE_OFFSET + BAR_WIDTH/2, y)
listBPM = [65,67,70,72,80,85,90,95,110,115,130]
plotMin = [123]*len(listBPM)
plotMax = [200]*len(listBPM)
BAR_WIDTH = 10
BAR_SPACING = 16
EDGE_OFFSET = 3
GRAPH_SIZE = (550,500)
# Generate all data points
lines = [
[new_point(i, y) for i, y in enumerate(listBPM)],
[new_point(i, y) for i, y in enumerate(plotMin)],
[new_point(i, y) for i, y in enumerate(plotMax)],
]
sg.theme('DarkAmber')
# Find the range for data
min_x, min_y, max_x, max_y = 10000, 10000, 0, 0
for line in lines:
for x, y in line:
min_x, min_y = min(min_x, x), min(min_y, y)
max_x, max_y = max(max_x, x), max(max_y, y)
graph = sg.Graph(
GRAPH_SIZE,
(min_x - EDGE_OFFSET - BAR_WIDTH/2, min_y - EDGE_OFFSET - BAR_WIDTH/2),
(max_x + EDGE_OFFSET + BAR_WIDTH/2, max_y + EDGE_OFFSET + BAR_WIDTH/2),
background_color='white')
layout = [
[sg.Text('chart demo'), sg.Button('OK')],
[graph],
]
window = sg.Window('Window Title', layout)
while True:
event, values = window.read()
if event is None:
break
elif event == 'OK':
graph.erase()
for line in lines:
graph.draw_lines(line, color='blue', width=1)
for x, y in lines[0]:
graph.draw_text(text=y, location=(x, y+10))
graph.draw_point((x, y), size=3 ,color='blue')
window.Close()