Formatting number of Matplotlib ticks (thousands). TypeError: '<' not supported between instances of 'str' and 'int'
Formatting number of Matplotlib ticks (thousands). TypeError: '<' not supported between instances of 'str' and 'int'
我正在构建一个应用程序,显示销售员在不同时间段内取得的销售额。有 4 个具有不同时间段(日、周、月、年)的复选框。单击任何时间段后,将显示 matplotlib 时间线图。
当我试图将 y 刻度格式化为数千时,我遇到了困难。我正在使用 format(value, ',')
命令。但是我一直收到 TypeError: '<' not supported between instances of 'str' and 'int'
。经过研究,我发现建议是将值明确声明为 int。我已经以多种方式尝试过,但没有成功。例如,如果我添加一个 int() 以从字符串转换为 int: int(format(divider * 1, ','))
我得到错误 ValueError: invalid literal for int() with base 10: '150,000'
最小可重现示例的代码如下:
Python代码:
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import ButtonBehavior
from kivy.uix.label import Label
import matplotlib
import matplotlib.pyplot as plt
from datetime import datetime
matplotlib.use("module://kivy.garden.matplotlib.backend_kivy")
from kivy.garden.matplotlib import FigureCanvasKivyAgg
class LabelButton(ButtonBehavior, Label):
pass
class MainMenuWindow(Screen):
pass
class DashboardWindow(Screen):
# Valores Iniciales
""" PENDIENTE: HACER QUE LOS VALORES INICIALES VARÍEN EN FUNCIÓN DEL VENDEDOR Y SUS NÚMEROS """
meta_mensual = 300000
venta_diaria_ = 2500
venta_semanal_ = 50000
venta_mensual_ = 230000
venta_anual_ = 2500000
menu = None
number_items_mdlist = 0
minutes_items_mdlist = 0
duracion_actividades_list = []
picture_filepath_list = []
image_status_list = []
periodo_selec = ''
# Ventas
def diario_label_press(self, value):
if not self.ids.periodo_diario_.active:
self.ids.periodo_diario_.active = True
else:
self.ids.periodo_diario_.active = False
def semanal_label_press(self, value):
if not self.ids.periodo_semanal_.active:
self.ids.periodo_semanal_.active = True
else:
self.ids.periodo_semanal_.active = False
def mensual_label_press(self, value):
if not self.ids.periodo_mensual_.active:
self.ids.periodo_mensual_.active = True
else:
self.ids.periodo_mensual_.active = False
def anual_label_press(self, value):
if not self.ids.periodo_anual_.active:
self.ids.periodo_anual_.active = True
else:
self.ids.periodo_anual_.active = False
def switch_checkbox(self):
if self.ids.periodo_diario_.active:
# Meta de Ventas
meta_diaria = int(self.meta_mensual) / 30
meta_diaria_format = '{:,}'.format(meta_diaria)
self.ids.meta_label.text = "$ " + str(meta_diaria_format) + "0"
# Monto de Venta
venta = int(self.venta_diaria_)
venta = '{:,}'.format(venta)
self.ids.monto_venta.text = "$ " + str(venta) + ".00"
# Porcentaje de Meta
porcentaje = float(self.venta_diaria_ / meta_diaria)
porcentaje_format = "{0:.1%}".format(porcentaje)
self.ids.porcentaje_meta.text = str(porcentaje_format)
self.periodo_selec = 'Diario'
elif self.ids.periodo_semanal_.active:
# Meta de Ventas
meta_semanal = int(self.meta_mensual) / 4
meta_semanal_format = '{:,}'.format(meta_semanal)
self.ids.meta_label.text = "$ " + str(meta_semanal_format) + "0"
# Monto de Venta
venta = int(self.venta_semanal_)
venta = '{:,}'.format(venta)
self.ids.monto_venta.text = "$ " + str(venta) + ".00"
# Porcentaje de Meta
porcentaje = float(self.venta_semanal_ / meta_semanal)
porcentaje_format = "{0:.1%}".format(porcentaje)
self.ids.porcentaje_meta.text = str(porcentaje_format)
self.periodo_selec = 'Semanal'
elif self.ids.periodo_mensual_.active:
# Meta de Ventas
meta_mensual = int(self.meta_mensual) * 1
meta_mensual_format = '{:,}'.format(self.meta_mensual)
self.ids.meta_label.text = "$ " + str(meta_mensual_format) + ".00"
# Monto de Venta
venta = int(self.venta_mensual_)
venta = '{:,}'.format(venta)
self.ids.monto_venta.text = "$ " + str(venta) + ".00"
# Porcentaje de Meta
porcentaje = float(self.venta_mensual_ / self.meta_mensual)
porcentaje_format = "{0:.1%}".format(porcentaje)
self.ids.porcentaje_meta.text = str(porcentaje_format)
self.periodo_selec = 'Mensual'
elif self.ids.periodo_anual_.active:
# Meta de Ventas
meta_anual = int(self.meta_mensual) * 12
meta_anual_format = '{:,}'.format(meta_anual)
self.ids.meta_label.text = "$ " + str(meta_anual_format) + ".00"
# Monto de Venta
venta = int(self.venta_anual_)
venta = '{:,}'.format(venta)
self.ids.monto_venta.text = "$ " + str(venta) + ".00"
# Porcentaje de Meta
porcentaje = float(self.venta_anual_ / meta_anual)
porcentaje_format = "{0:.1%}".format(porcentaje)
self.ids.porcentaje_meta.text = str(porcentaje_format)
self.periodo_selec = 'Anual'
elif not self.ids.periodo_diario_.active and not self.ids.periodo_semanal_.active and \
not self.ids.periodo_mensual_.active and not self.ids.periodo_anual_.active:
# Meta de Ventas
self.ids.meta_label.text = "Seleccione una opción"
self.ids.monto_venta.text = "$ 0.00"
self.ids.porcentaje_meta.text = "0.00 %"
self.actualizar_timeline_ventas(self.periodo_selec)
def actualizar_timeline_ventas(self, selec):
now = datetime.now()
dash = MDApp.get_running_app().root.get_screen('dash')
plt.clf()
plt.style.use('seaborn')
mes = ''
if now.month == 1:
mes = 'Enero'
elif now.month == 2:
mes = 'Febrero'
elif now.month == 3:
mes = 'Marzo'
elif now.month == 4:
mes = 'Abril'
elif now.month == 5:
mes = 'Mayo'
elif now.month == 6:
mes = 'Junio'
elif now.month == 7:
mes = 'Julio'
elif now.month == 8:
mes = 'Agosto'
elif now.month == 9:
mes = 'Septiembre'
elif now.month == 10:
mes = 'Octubre'
elif now.month == 11:
mes = 'Noviembre'
elif now.month == 12:
mes = 'Diciembre'
# Línea de tiempo del día
if self.ids.periodo_diario_.active:
dia_semana = ''
if now.weekday() == 0:
dia_semana = 'Lunes'
elif now.weekday() == 1:
dia_semana = 'Martes'
elif now.weekday() == 2:
dia_semana = 'Miércoles'
elif now.weekday() == 3:
dia_semana = 'Jueves'
elif now.weekday() == 4:
dia_semana = 'Viernes'
elif now.weekday() == 5:
dia_semana = 'Sábado'
elif now.weekday() == 6:
dia_semana = 'Domingo'
dash.ids.momento_seleccionado.text = f'Hoy, {dia_semana.lower()}, {now.day} de {mes.lower()} del {now.year}'
dash.ids.timeline_container.clear_widgets()
hora = ['9:00 a.m.', '10:00 a.m.', '11:00 a.m.', '12:00 p.m.', '1:00 p.m.', '2:00 p.m.', '3:00 p.m.',
'4:00 p.m.', '5:00 p.m.', '6:00 p.m.']
venta = [0, 1500, 10000, 17000, 2600, 0, 0, 2710, 7500, 0]
divider = max(venta) / 4
plt.yticks([0, divider * 1, divider * 2, divider * 3, divider * 4])
plt.plot(hora, venta, color='blue', marker='D')
plt.xticks(['9:00 a.m.', '12:00 p.m.', '3:00 p.m.', '6:00 p.m.'])
plt.grid(True)
dash.ids.timeline_container.add_widget(FigureCanvasKivyAgg(figure=plt.gcf(), size_hint=(0.95, 0.95),
pos_hint={'center_x': 0.5, 'top': 1}))
# Línea de tiempo Semanal
elif self.ids.periodo_semanal_.active:
dash.ids.momento_seleccionado.text = f'Semana {datetime.date(now).isocalendar()[1]} del {now.year}'
dash.ids.timeline_container.clear_widgets()
dia = ['Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab', 'Dom']
venta = [10000, 12000, 22000, 17000, 26000, 5000, 0]
divider = max(venta) / 4
plt.yticks([0, divider * 1, divider * 2, divider * 3, divider * 4])
plt.plot(dia, venta, color='yellow', marker='o')
plt.grid(True)
dash.ids.timeline_container.add_widget(FigureCanvasKivyAgg(figure=plt.gcf(), size_hint=(0.95, 0.95),
pos_hint={'center_x': 0.5, 'top': 1}))
# Línea de tiempo mensual
elif self.ids.periodo_mensual_.active:
dash.ids.momento_seleccionado.text = f'{mes} {now.year}'
dash.ids.timeline_container.clear_widgets()
now = datetime.now()
curr_month = now.now().month
if int(curr_month) == 1 or int(curr_month) == 3 or int(curr_month) == 5 or int(curr_month) == 7 \
or int(curr_month) == 8 or int(curr_month) == 10 or int(curr_month) == 12:
number_days = 31
elif int(curr_month) == 4 or int(curr_month) == 6 or int(curr_month) == 9 or int(curr_month) == 11:
number_days = 30
else:
number_days = 28
import random
dia = []
venta = []
day = 1
for i in range(number_days):
dia.append(day)
venta.append(random.randrange(150000, 750000))
day += 1
divider = max(venta) / 4
plt.yticks([0, divider * 1, divider * 2, divider * 3, divider * 4])
plt.plot(dia, venta, color='red', marker='s')
plt.grid(True)
dash.ids.timeline_container.add_widget(FigureCanvasKivyAgg(figure=plt.gcf(), size_hint=(0.95, 0.95),
pos_hint={'center_x': 0.5, 'top': 1}))
# Línea de tiempo Anual
elif self.ids.periodo_anual_.active:
dash.ids.momento_seleccionado.text = str(now.year)
dash.ids.timeline_container.clear_widgets()
mes = ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic']
venta = [100000, 120000, 130000, 170000, 260000, 220000, 243000, 271000, 274000, 320000, 430000, 600000]
divider = int(max(venta) / 4)
plt.yticks([int(0), format(divider * 1, ','), format(divider * 2, ','), format(divider * 3, ','),
format(divider * 4, ',')])
plt.plot(mes, venta, color='green', marker='^')
plt.grid(True)
dash.ids.timeline_container.add_widget(FigureCanvasKivyAgg(figure=plt.gcf(), size_hint=(0.95, 0.95),
pos_hint={'center_x': 0.5, 'top': 1}))
else:
dash.ids.momento_seleccionado.text = 'Líneas de tiempo'
dash.ids.timeline_container.clear_widgets()
dash.ids.timeline_container.add_widget(self.ids.label_timeline_vacio)
class WindowManager(ScreenManager):
pass
class Linegraph(MDApp):
def build(self):
self.theme_cls.primary_palette = "Teal"
return WindowManager()
if __name__ == "__main__":
Linegraph().run()
KV代码:
<WindowManager>:
id: screen_manager
DashboardWindow:
id: dash
name: 'dash'
<DashboardWindow>:
id: dash
name:'dash'
MDBoxLayout:
orientation: 'vertical'
valign: 'middle'
padding: '10dp'
MDLabel:
text: "Nombre del Vendedor"
size_hint_y: 0.15
font_size: (root.width**2 + root.height**2) / 12.75**4
valign: 'middle'
bold: True
MDBoxLayout:
orientation: 'horizontal'
spacing: '5dp'
size_hint_y: 0.15
MDLabel:
text: "Meta de venta:"
valign: 'middle'
font_size: (root.width**2 + root.height**2) / 14.25**4
MDLabel:
id: meta_label
text: 'Seleccione una opcion'
valign: 'middle'
font_size: (root.width**2 + root.height**2) / 14.25**4
bold: True
MDBoxLayout:
orientation: 'horizontal'
spacing: '5dp'
size_hint_y: 0.20
MDCheckbox:
group: 'periodo_venta'
id: periodo_diario_
size_hint: None, None
size: dp(50), dp(50)
pos_hint: {"x":0, "center_y":0.5}
on_active:
root.switch_checkbox()
LabelButton:
text: 'Diario'
color: 0, 0, 0, 1
halign: 'left'
valign: 'middle'
text_size: self.size
on_release: root.diario_label_press(root.ids.periodo_diario_.active)
MDCheckbox:
group: 'periodo_venta'
id: periodo_semanal_
size_hint: None, None
size: dp(50), dp(50)
pos_hint: {"x":0, "center_y":0.5}
on_active:
root.switch_checkbox()
LabelButton:
text: 'Semanal'
color: 0, 0, 0, 1
halign: 'left'
valign: 'middle'
text_size: self.size
on_release: root.semanal_label_press(root.ids.periodo_semanal_.active)
MDCheckbox:
group: 'periodo_venta'
id: periodo_mensual_
size_hint: None, None
size: dp(50), dp(50)
pos_hint: {"x":0, "center_y":0.5}
on_active:
root.switch_checkbox()
LabelButton:
text: 'Mensual'
color: 0, 0, 0, 1
halign: 'left'
valign: 'middle'
text_size: self.size
on_release: root.mensual_label_press(root.ids.periodo_mensual_.active)
MDCheckbox:
group: 'periodo_venta'
id: periodo_anual_
size_hint: None, None
size: dp(50), dp(50)
pos_hint: {"x":0, "center_y":0.5}
on_active:
root.switch_checkbox()
LabelButton:
text: 'Anual'
color: 0, 0, 0, 1
halign: 'left'
valign: 'middle'
text_size: self.size
on_release: root.anual_label_press(root.ids.periodo_anual_.active)
MDBoxLayout:
orientation: 'horizontal'
padding: 0, '10dp', 0, 0
spacing: '20dp'
size_hint: 1, 0.25
MDCard:
orientation: 'vertical'
padding: '10dp'
size_hint: 0.4, 1
radius: [16, ]
md_bg_color: [1, 1, 1, 0.85]
MDLabel:
id: monto_venta
text: "$ 0.00"
theme_text_color: "Custom"
text_color: 0, 0, 0, 1
font_style: 'H1'
halign: 'center'
font_size: (root.width**2 + root.height**2) / 13**4
MDLabel:
text: "Monto Vendido"
theme_text_color: "Custom"
text_color: 0, 0, 0, 1
font_style: 'Subtitle2'
halign: 'center'
MDCard:
size_hint: 0.4, 1
orientation: 'vertical'
padding: '10dp'
radius: [16, ]
md_bg_color: [1, 1, 1, 0.85]
MDLabel:
id: porcentaje_meta
text: '0.00 %'
theme_text_color: "Custom"
text_color: 0, 0, 0, 1
font_style: 'H1'
halign: 'center'
font_size: (root.width**2 + root.height**2) / 13**4
MDLabel:
text: "% de meta alcanzado"
theme_text_color: "Custom"
text_color: 0, 0, 0, 1
font_style: 'Subtitle2'
halign: 'center'
MDBoxLayout:
orientation: 'horizontal'
spacing: '10dp'
# padding: "10dp", 0,"10dp", "10dp"
size_hint_y: 0.45
MDCard:
size_hint: 1, 0.9
orientation: 'vertical'
radius: [16, ]
MDBoxLayout:
size_hint: 1, 0.175
radius: [16, 16, 0, 0]
md_bg_color: 0.114, 0.212, 0.235, 1
MDLabel:
id: momento_seleccionado
text: 'Lineas de tiempo'
halign: 'center'
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
MDSeparator:
height: "2dp"
color: 0.95, 0.81, 0.25, 1
MDBoxLayout:
id: timeline_container
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
radius: [0, 0, 16, 16]
MDLabel:
id: label_timeline_vacio
text: 'Seleccionar temporalidad para visualizar ventas del periodo'
halign: 'center'
如您所见,唯一尝试进行千位格式设置的复选框是 'Year'。因此,这将是唯一会导致应用程序崩溃的按钮。请注意,我使用 divider 变量的原因是为了获得 5 个均匀分布的刻度。关于如何正确实现刻度格式的任何建议?
作为问题的第二部分,我将非常感谢有关如何添加在单击图表的不同点时显示的弹出标签的建议。我已经找到了,但只在地图的 Folium 包上。 matplotlib有没有类似的解决方案?
非常感谢。
我相信你的问题出在这条线上:
plt.yticks([int(0), format(divider * 1, ','), format(divider * 2, ','), format(divider * 3, ','), format(divider * 4, ',')])
yticks()
方法接受零个、一个或两个参数。如果你只提供一个参数,就像上面一行那样,那么它必须是一个数字数组,而不是字符串。如果要指定实际标签,则必须提供两个参数 - 数字列表和另一个字符串列表。所以,我相信上面一行应该是:
plt.yticks([0, divider * 1, divider * 2, divider * 3, divider * 4], ['0', format(divider * 1, ','), format(divider * 2, ','), format(divider * 3, ','), format(divider * 4, ',')])
我正在构建一个应用程序,显示销售员在不同时间段内取得的销售额。有 4 个具有不同时间段(日、周、月、年)的复选框。单击任何时间段后,将显示 matplotlib 时间线图。
当我试图将 y 刻度格式化为数千时,我遇到了困难。我正在使用 format(value, ',')
命令。但是我一直收到 TypeError: '<' not supported between instances of 'str' and 'int'
。经过研究,我发现建议是将值明确声明为 int。我已经以多种方式尝试过,但没有成功。例如,如果我添加一个 int() 以从字符串转换为 int: int(format(divider * 1, ','))
我得到错误 ValueError: invalid literal for int() with base 10: '150,000'
最小可重现示例的代码如下:
Python代码:
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import ButtonBehavior
from kivy.uix.label import Label
import matplotlib
import matplotlib.pyplot as plt
from datetime import datetime
matplotlib.use("module://kivy.garden.matplotlib.backend_kivy")
from kivy.garden.matplotlib import FigureCanvasKivyAgg
class LabelButton(ButtonBehavior, Label):
pass
class MainMenuWindow(Screen):
pass
class DashboardWindow(Screen):
# Valores Iniciales
""" PENDIENTE: HACER QUE LOS VALORES INICIALES VARÍEN EN FUNCIÓN DEL VENDEDOR Y SUS NÚMEROS """
meta_mensual = 300000
venta_diaria_ = 2500
venta_semanal_ = 50000
venta_mensual_ = 230000
venta_anual_ = 2500000
menu = None
number_items_mdlist = 0
minutes_items_mdlist = 0
duracion_actividades_list = []
picture_filepath_list = []
image_status_list = []
periodo_selec = ''
# Ventas
def diario_label_press(self, value):
if not self.ids.periodo_diario_.active:
self.ids.periodo_diario_.active = True
else:
self.ids.periodo_diario_.active = False
def semanal_label_press(self, value):
if not self.ids.periodo_semanal_.active:
self.ids.periodo_semanal_.active = True
else:
self.ids.periodo_semanal_.active = False
def mensual_label_press(self, value):
if not self.ids.periodo_mensual_.active:
self.ids.periodo_mensual_.active = True
else:
self.ids.periodo_mensual_.active = False
def anual_label_press(self, value):
if not self.ids.periodo_anual_.active:
self.ids.periodo_anual_.active = True
else:
self.ids.periodo_anual_.active = False
def switch_checkbox(self):
if self.ids.periodo_diario_.active:
# Meta de Ventas
meta_diaria = int(self.meta_mensual) / 30
meta_diaria_format = '{:,}'.format(meta_diaria)
self.ids.meta_label.text = "$ " + str(meta_diaria_format) + "0"
# Monto de Venta
venta = int(self.venta_diaria_)
venta = '{:,}'.format(venta)
self.ids.monto_venta.text = "$ " + str(venta) + ".00"
# Porcentaje de Meta
porcentaje = float(self.venta_diaria_ / meta_diaria)
porcentaje_format = "{0:.1%}".format(porcentaje)
self.ids.porcentaje_meta.text = str(porcentaje_format)
self.periodo_selec = 'Diario'
elif self.ids.periodo_semanal_.active:
# Meta de Ventas
meta_semanal = int(self.meta_mensual) / 4
meta_semanal_format = '{:,}'.format(meta_semanal)
self.ids.meta_label.text = "$ " + str(meta_semanal_format) + "0"
# Monto de Venta
venta = int(self.venta_semanal_)
venta = '{:,}'.format(venta)
self.ids.monto_venta.text = "$ " + str(venta) + ".00"
# Porcentaje de Meta
porcentaje = float(self.venta_semanal_ / meta_semanal)
porcentaje_format = "{0:.1%}".format(porcentaje)
self.ids.porcentaje_meta.text = str(porcentaje_format)
self.periodo_selec = 'Semanal'
elif self.ids.periodo_mensual_.active:
# Meta de Ventas
meta_mensual = int(self.meta_mensual) * 1
meta_mensual_format = '{:,}'.format(self.meta_mensual)
self.ids.meta_label.text = "$ " + str(meta_mensual_format) + ".00"
# Monto de Venta
venta = int(self.venta_mensual_)
venta = '{:,}'.format(venta)
self.ids.monto_venta.text = "$ " + str(venta) + ".00"
# Porcentaje de Meta
porcentaje = float(self.venta_mensual_ / self.meta_mensual)
porcentaje_format = "{0:.1%}".format(porcentaje)
self.ids.porcentaje_meta.text = str(porcentaje_format)
self.periodo_selec = 'Mensual'
elif self.ids.periodo_anual_.active:
# Meta de Ventas
meta_anual = int(self.meta_mensual) * 12
meta_anual_format = '{:,}'.format(meta_anual)
self.ids.meta_label.text = "$ " + str(meta_anual_format) + ".00"
# Monto de Venta
venta = int(self.venta_anual_)
venta = '{:,}'.format(venta)
self.ids.monto_venta.text = "$ " + str(venta) + ".00"
# Porcentaje de Meta
porcentaje = float(self.venta_anual_ / meta_anual)
porcentaje_format = "{0:.1%}".format(porcentaje)
self.ids.porcentaje_meta.text = str(porcentaje_format)
self.periodo_selec = 'Anual'
elif not self.ids.periodo_diario_.active and not self.ids.periodo_semanal_.active and \
not self.ids.periodo_mensual_.active and not self.ids.periodo_anual_.active:
# Meta de Ventas
self.ids.meta_label.text = "Seleccione una opción"
self.ids.monto_venta.text = "$ 0.00"
self.ids.porcentaje_meta.text = "0.00 %"
self.actualizar_timeline_ventas(self.periodo_selec)
def actualizar_timeline_ventas(self, selec):
now = datetime.now()
dash = MDApp.get_running_app().root.get_screen('dash')
plt.clf()
plt.style.use('seaborn')
mes = ''
if now.month == 1:
mes = 'Enero'
elif now.month == 2:
mes = 'Febrero'
elif now.month == 3:
mes = 'Marzo'
elif now.month == 4:
mes = 'Abril'
elif now.month == 5:
mes = 'Mayo'
elif now.month == 6:
mes = 'Junio'
elif now.month == 7:
mes = 'Julio'
elif now.month == 8:
mes = 'Agosto'
elif now.month == 9:
mes = 'Septiembre'
elif now.month == 10:
mes = 'Octubre'
elif now.month == 11:
mes = 'Noviembre'
elif now.month == 12:
mes = 'Diciembre'
# Línea de tiempo del día
if self.ids.periodo_diario_.active:
dia_semana = ''
if now.weekday() == 0:
dia_semana = 'Lunes'
elif now.weekday() == 1:
dia_semana = 'Martes'
elif now.weekday() == 2:
dia_semana = 'Miércoles'
elif now.weekday() == 3:
dia_semana = 'Jueves'
elif now.weekday() == 4:
dia_semana = 'Viernes'
elif now.weekday() == 5:
dia_semana = 'Sábado'
elif now.weekday() == 6:
dia_semana = 'Domingo'
dash.ids.momento_seleccionado.text = f'Hoy, {dia_semana.lower()}, {now.day} de {mes.lower()} del {now.year}'
dash.ids.timeline_container.clear_widgets()
hora = ['9:00 a.m.', '10:00 a.m.', '11:00 a.m.', '12:00 p.m.', '1:00 p.m.', '2:00 p.m.', '3:00 p.m.',
'4:00 p.m.', '5:00 p.m.', '6:00 p.m.']
venta = [0, 1500, 10000, 17000, 2600, 0, 0, 2710, 7500, 0]
divider = max(venta) / 4
plt.yticks([0, divider * 1, divider * 2, divider * 3, divider * 4])
plt.plot(hora, venta, color='blue', marker='D')
plt.xticks(['9:00 a.m.', '12:00 p.m.', '3:00 p.m.', '6:00 p.m.'])
plt.grid(True)
dash.ids.timeline_container.add_widget(FigureCanvasKivyAgg(figure=plt.gcf(), size_hint=(0.95, 0.95),
pos_hint={'center_x': 0.5, 'top': 1}))
# Línea de tiempo Semanal
elif self.ids.periodo_semanal_.active:
dash.ids.momento_seleccionado.text = f'Semana {datetime.date(now).isocalendar()[1]} del {now.year}'
dash.ids.timeline_container.clear_widgets()
dia = ['Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab', 'Dom']
venta = [10000, 12000, 22000, 17000, 26000, 5000, 0]
divider = max(venta) / 4
plt.yticks([0, divider * 1, divider * 2, divider * 3, divider * 4])
plt.plot(dia, venta, color='yellow', marker='o')
plt.grid(True)
dash.ids.timeline_container.add_widget(FigureCanvasKivyAgg(figure=plt.gcf(), size_hint=(0.95, 0.95),
pos_hint={'center_x': 0.5, 'top': 1}))
# Línea de tiempo mensual
elif self.ids.periodo_mensual_.active:
dash.ids.momento_seleccionado.text = f'{mes} {now.year}'
dash.ids.timeline_container.clear_widgets()
now = datetime.now()
curr_month = now.now().month
if int(curr_month) == 1 or int(curr_month) == 3 or int(curr_month) == 5 or int(curr_month) == 7 \
or int(curr_month) == 8 or int(curr_month) == 10 or int(curr_month) == 12:
number_days = 31
elif int(curr_month) == 4 or int(curr_month) == 6 or int(curr_month) == 9 or int(curr_month) == 11:
number_days = 30
else:
number_days = 28
import random
dia = []
venta = []
day = 1
for i in range(number_days):
dia.append(day)
venta.append(random.randrange(150000, 750000))
day += 1
divider = max(venta) / 4
plt.yticks([0, divider * 1, divider * 2, divider * 3, divider * 4])
plt.plot(dia, venta, color='red', marker='s')
plt.grid(True)
dash.ids.timeline_container.add_widget(FigureCanvasKivyAgg(figure=plt.gcf(), size_hint=(0.95, 0.95),
pos_hint={'center_x': 0.5, 'top': 1}))
# Línea de tiempo Anual
elif self.ids.periodo_anual_.active:
dash.ids.momento_seleccionado.text = str(now.year)
dash.ids.timeline_container.clear_widgets()
mes = ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic']
venta = [100000, 120000, 130000, 170000, 260000, 220000, 243000, 271000, 274000, 320000, 430000, 600000]
divider = int(max(venta) / 4)
plt.yticks([int(0), format(divider * 1, ','), format(divider * 2, ','), format(divider * 3, ','),
format(divider * 4, ',')])
plt.plot(mes, venta, color='green', marker='^')
plt.grid(True)
dash.ids.timeline_container.add_widget(FigureCanvasKivyAgg(figure=plt.gcf(), size_hint=(0.95, 0.95),
pos_hint={'center_x': 0.5, 'top': 1}))
else:
dash.ids.momento_seleccionado.text = 'Líneas de tiempo'
dash.ids.timeline_container.clear_widgets()
dash.ids.timeline_container.add_widget(self.ids.label_timeline_vacio)
class WindowManager(ScreenManager):
pass
class Linegraph(MDApp):
def build(self):
self.theme_cls.primary_palette = "Teal"
return WindowManager()
if __name__ == "__main__":
Linegraph().run()
KV代码:
<WindowManager>:
id: screen_manager
DashboardWindow:
id: dash
name: 'dash'
<DashboardWindow>:
id: dash
name:'dash'
MDBoxLayout:
orientation: 'vertical'
valign: 'middle'
padding: '10dp'
MDLabel:
text: "Nombre del Vendedor"
size_hint_y: 0.15
font_size: (root.width**2 + root.height**2) / 12.75**4
valign: 'middle'
bold: True
MDBoxLayout:
orientation: 'horizontal'
spacing: '5dp'
size_hint_y: 0.15
MDLabel:
text: "Meta de venta:"
valign: 'middle'
font_size: (root.width**2 + root.height**2) / 14.25**4
MDLabel:
id: meta_label
text: 'Seleccione una opcion'
valign: 'middle'
font_size: (root.width**2 + root.height**2) / 14.25**4
bold: True
MDBoxLayout:
orientation: 'horizontal'
spacing: '5dp'
size_hint_y: 0.20
MDCheckbox:
group: 'periodo_venta'
id: periodo_diario_
size_hint: None, None
size: dp(50), dp(50)
pos_hint: {"x":0, "center_y":0.5}
on_active:
root.switch_checkbox()
LabelButton:
text: 'Diario'
color: 0, 0, 0, 1
halign: 'left'
valign: 'middle'
text_size: self.size
on_release: root.diario_label_press(root.ids.periodo_diario_.active)
MDCheckbox:
group: 'periodo_venta'
id: periodo_semanal_
size_hint: None, None
size: dp(50), dp(50)
pos_hint: {"x":0, "center_y":0.5}
on_active:
root.switch_checkbox()
LabelButton:
text: 'Semanal'
color: 0, 0, 0, 1
halign: 'left'
valign: 'middle'
text_size: self.size
on_release: root.semanal_label_press(root.ids.periodo_semanal_.active)
MDCheckbox:
group: 'periodo_venta'
id: periodo_mensual_
size_hint: None, None
size: dp(50), dp(50)
pos_hint: {"x":0, "center_y":0.5}
on_active:
root.switch_checkbox()
LabelButton:
text: 'Mensual'
color: 0, 0, 0, 1
halign: 'left'
valign: 'middle'
text_size: self.size
on_release: root.mensual_label_press(root.ids.periodo_mensual_.active)
MDCheckbox:
group: 'periodo_venta'
id: periodo_anual_
size_hint: None, None
size: dp(50), dp(50)
pos_hint: {"x":0, "center_y":0.5}
on_active:
root.switch_checkbox()
LabelButton:
text: 'Anual'
color: 0, 0, 0, 1
halign: 'left'
valign: 'middle'
text_size: self.size
on_release: root.anual_label_press(root.ids.periodo_anual_.active)
MDBoxLayout:
orientation: 'horizontal'
padding: 0, '10dp', 0, 0
spacing: '20dp'
size_hint: 1, 0.25
MDCard:
orientation: 'vertical'
padding: '10dp'
size_hint: 0.4, 1
radius: [16, ]
md_bg_color: [1, 1, 1, 0.85]
MDLabel:
id: monto_venta
text: "$ 0.00"
theme_text_color: "Custom"
text_color: 0, 0, 0, 1
font_style: 'H1'
halign: 'center'
font_size: (root.width**2 + root.height**2) / 13**4
MDLabel:
text: "Monto Vendido"
theme_text_color: "Custom"
text_color: 0, 0, 0, 1
font_style: 'Subtitle2'
halign: 'center'
MDCard:
size_hint: 0.4, 1
orientation: 'vertical'
padding: '10dp'
radius: [16, ]
md_bg_color: [1, 1, 1, 0.85]
MDLabel:
id: porcentaje_meta
text: '0.00 %'
theme_text_color: "Custom"
text_color: 0, 0, 0, 1
font_style: 'H1'
halign: 'center'
font_size: (root.width**2 + root.height**2) / 13**4
MDLabel:
text: "% de meta alcanzado"
theme_text_color: "Custom"
text_color: 0, 0, 0, 1
font_style: 'Subtitle2'
halign: 'center'
MDBoxLayout:
orientation: 'horizontal'
spacing: '10dp'
# padding: "10dp", 0,"10dp", "10dp"
size_hint_y: 0.45
MDCard:
size_hint: 1, 0.9
orientation: 'vertical'
radius: [16, ]
MDBoxLayout:
size_hint: 1, 0.175
radius: [16, 16, 0, 0]
md_bg_color: 0.114, 0.212, 0.235, 1
MDLabel:
id: momento_seleccionado
text: 'Lineas de tiempo'
halign: 'center'
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
MDSeparator:
height: "2dp"
color: 0.95, 0.81, 0.25, 1
MDBoxLayout:
id: timeline_container
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
radius: [0, 0, 16, 16]
MDLabel:
id: label_timeline_vacio
text: 'Seleccionar temporalidad para visualizar ventas del periodo'
halign: 'center'
如您所见,唯一尝试进行千位格式设置的复选框是 'Year'。因此,这将是唯一会导致应用程序崩溃的按钮。请注意,我使用 divider 变量的原因是为了获得 5 个均匀分布的刻度。关于如何正确实现刻度格式的任何建议?
作为问题的第二部分,我将非常感谢有关如何添加在单击图表的不同点时显示的弹出标签的建议。我已经找到了,但只在地图的 Folium 包上。 matplotlib有没有类似的解决方案?
非常感谢。
我相信你的问题出在这条线上:
plt.yticks([int(0), format(divider * 1, ','), format(divider * 2, ','), format(divider * 3, ','), format(divider * 4, ',')])
yticks()
方法接受零个、一个或两个参数。如果你只提供一个参数,就像上面一行那样,那么它必须是一个数字数组,而不是字符串。如果要指定实际标签,则必须提供两个参数 - 数字列表和另一个字符串列表。所以,我相信上面一行应该是:
plt.yticks([0, divider * 1, divider * 2, divider * 3, divider * 4], ['0', format(divider * 1, ','), format(divider * 2, ','), format(divider * 3, ','), format(divider * 4, ',')])