mp-007 / kivy_matplotlib_widget

A fast matplotlib rendering for Kivy based on Kivy_matplotlib project and kivy scatter. Matplotlib used 'agg' backend
MIT License
35 stars 7 forks source link

Specifying line color crashes `LegendRv` #15

Closed kubapilch closed 1 year ago

kubapilch commented 1 year ago

If we want to specify a color of the line inside GraphGenerator like so

self.ax1.plot(x, y,label='line 1', color='red')

LegendRv crashes the program with following exception: ValueError: invalid literal for int() with base 16: 'r'. The error happens in this line: https://github.com/mp-007/kivy_matplotlib_widget/blob/5a9a55d8e23e7f70c62a709b43b3a359037b911b/kivy_matplotlib_widget/uix/legend_widget.py#L196 When kivy.utils function get_color_from_hex receives red instead of a hex value. It works perfectly fine without the LegendRv

Minimal code to reproduce the error:

from kivy.utils import platform

#avoid conflict between mouse provider and touch (very important with touch device)
#no need for android platform
if platform != 'android':
    from kivy.config import Config
    Config.set('input', 'mouse', 'mouse,disable_on_activity')

from kivy.lang import Builder
from kivy.app import App
from graph_generator import GraphGenerator
import kivy_matplotlib_widget

import matplotlib as mpl
import matplotlib.pyplot as plt
from kivy.metrics import dp

#optimized draw on Agg backend
mpl.rcParams['path.simplify'] = True
mpl.rcParams['path.simplify_threshold'] = 1.0
mpl.rcParams['agg.path.chunksize'] = 1000

#define some matplotlib figure parameters
mpl.rcParams['font.family'] = 'Verdana'
mpl.rcParams['axes.spines.top'] = False
mpl.rcParams['axes.spines.right'] = False
mpl.rcParams['axes.linewidth'] = 1.0

font_size_axis_title=dp(13)
font_size_axis_tick=dp(12)        

class GraphGenerator(object):
    """class that generate Matplotlib graph."""

    def __init__(self):
        """Create empty structure plot. 

        """       
        super().__init__()

        self.fig, self.ax1 = plt.subplots(1, 1)

        from random import randint

        x=[randint(0, 9) for p in range(0, 10)]
        x.sort()
        y=[randint(0, 9) for p in range(0, 10)]
        self.ax1.plot(x, y,label='line 1', color='red')

        self.xmin,self.xmax = self.ax1.get_xlim()
        self.ymin,self.ymax = self.ax1.get_ylim()

        self.fig.subplots_adjust(left=0.13,top=0.96,right=0.93,bottom=0.2)

        self.ax1.set_xlim(self.xmin, self.xmax)
        self.ax1.set_ylim(self.ymin, self.ymax)   
        self.ax1.set_xlabel("axis_x",fontsize=font_size_axis_title)
        self.ax1.set_ylabel("axis_y",fontsize=font_size_axis_title)#

KV = '''

Screen
    figure_wgt:figure_wgt
    legend_wgt:legend_wgt
    BoxLayout:

        BoxLayout:
            MatplotFigure:
                id:figure_wgt
            LegendRv:
                id:legend_wgt
                figure_wgt:figure_wgt
                size_hint_x:0.3
'''

class Test(App):
    lines = []

    def build(self):  
        self.screen=Builder.load_string(KV)
        return self.screen

    def on_start(self, *args):
        mygraph = GraphGenerator()

        self.screen.figure_wgt.figure = mygraph.fig
        self.screen.figure_wgt.axes = mygraph.ax1
        self.screen.figure_wgt.xmin = mygraph.xmin
        self.screen.figure_wgt.xmax = mygraph.xmax
        self.screen.figure_wgt.ymin = mygraph.ymin
        self.screen.figure_wgt.ymax = mygraph.ymax

        #register lines instance if need to be update
        for line in self.screen.figure_wgt.axes.lines:
            self.lines.append(line)

        self.screen.legend_wgt.set_data(self.lines)

Test().run()
mp-007 commented 1 year ago

Ok I will fix that. For the moment you can set the red hex color

self.ax1.plot(x, y,label='line 1', color='#FF0000') #red color
kubapilch commented 1 year ago

Ok I will fix that. For the moment you can set the red hex color

self.ax1.plot(x, y,label='line 1', color='#FF0000') #red color

That's exactly how I do it now. I posted an issue to make you aware of it, maybe you don't want to allow it but handle the error differently.

mp-007 commented 1 year ago

you can also used this function from matplotlib https://matplotlib.org/stable/api/_as_gen/matplotlib.colors.to_hex.html