updating plot line with different number of columns

Hi,

You are already adding a Legend and configuring its items manually, you will have to clear out Legend.items of the existing legend that you added, and re-configure .items with the new contents you want to see.

Thanks,

Bryan

···

On Mar 7, 2019, at 12:31 PM, Goran Goki <[email protected]> wrote:

Hi,

I have dataframe

s4= {'females': {'2012': 76.9, '2007': 75.9, '2005': 75.9, '2014': 77.5, '2004': 75.8, '2000': 75.2, '2013': 77.5, '2006': 76.2, '2002': 75.6, '2008': 76.5, '2010': 77.2, '2001': 76.1, '2011': 77.2, '2003': 75.7, '2009': 76.7, '2015': 77.4, '2016': 77.5}, 'males': {'2012': 73.0, '2007': 71.8, '2005': 71.6, '2014': 73.5, '2004': 71.5, '2000': 70.8, '2013': 73.4, '2006': 71.7, '2002': 70.6, '2008': 72.4, '2010': 72.9, '2001': 70.9, '2011': 73.1, '2003': 70.9, '2009': 72.3, '2015': 73.5, '2016': 73.4}, 'total': {'2012': 74.9, '2007': 73.8, '2005': 73.7, '2014': 75.5, '2004': 73.6, '2000': 73.0, '2013': 75.5, '2006': 73.9, '2002': 73.0, '2008': 74.4, '2010': 75.0, '2001': 73.4, '2011': 75.1, '2003': 73.2, '2009': 74.4, '2015': 75.5, '2016': 75.4}}

with following columns: Years, females, males and total
.
When updating on click button event I get new dataframe
s4= {'value': {Timestamp('2017-01-01 00:00:00'): nan, Timestamp('2015-01-01 00:00:00'): 41.6, Timestamp('2001-01-01 00:00:00'): nan, Timestamp('2002-01-01 00:00:00'): nan, Timestamp('2011-01-01 00:00:00'): 50.4, Timestamp('2004-01-01 00:00:00'): nan, Timestamp('2014-01-01 00:00:00'): 43.3, Timestamp('2005-01-01 00:00:00'): nan, Timestamp('2009-01-01 00:00:00'): nan, Timestamp('2010-01-01 00:00:00'): 47.2, Timestamp('2007-01-01 00:00:00'): nan, Timestamp('2016-01-01 00:00:00'): 41.1, Timestamp('2008-01-01 00:00:00'): nan, Timestamp('2003-01-01 00:00:00'): nan, Timestamp('2013-01-01 00:00:00'): 48.1, Timestamp('2000-01-01 00:00:00'): nan, Timestamp('2012-01-01 00:00:00'): 50.3, Timestamp('2006-01-01 00:00:00'): nan}}

with following columns: Years, value.

The question is how to show in first time plot with 3 lines (from dataframe df1) and appropriate legend and after updating to show in the same figure plot with one line (dataframe df2) and appropriate legend.
I tried to define one column data source with same structure (columns: Year, Value, var1, var2, var3, var4 ) but I have a problem to showin the plot figure because in both cases plot shows aditional line with values 0. (see im1 and im 2 in attachment) For im1 columndatasource looks like {'var4': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'Value': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'var1': [75.2, 76.1, 75.6, 75.7, 75.8, 75.9, 76.2, 75.9, 76.5, 76.7, 77.2, 77.2, 76.9, 77.5, 77.5, 77.4, 77.5], 'var2': [70.8, 70.9, 70.6, 70.9, 71.5, 71.6, 71.7, 71.8, 72.4, 72.3, 72.9, 73.1, 73.0, 73.4, 73.5, 73.5, 73.4], 'Year': ['2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016'], 'var3': [73.0, 73.4, 73.0, 73.2, 73.6, 73.7, 73.9, 73.8, 74.4, 74.4, 75.0, 75.1, 74.9, 75.5, 75.5, 75.5, 75.4]} and for im2 Column data source looks like {'Value': [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 47.2, 50.4, 50.3, 48.1, 43.3, 41.6, 41.1, nan], 'var4': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'Year': [Timestamp('2000-01-01 00:00:00'), Timestamp('2001-01-01 00:00:00'), Timestamp('2002-01-01 00:00:00'), Timestamp('2003-01-01 00:00:00'), Timestamp('2004-01-01 00:00:00'), Timestamp('2005-01-01 00:00:00'), Timestamp('2006-01-01 00:00:00'), Timestamp('2007-01-01 00:00:00'), Timestamp('2008-01-01 00:00:00'), Timestamp('2009-01-01 00:00:00'), Timestamp('2010-01-01 00:00:00'), Timestamp('2011-01-01 00:00:00'), Timestamp('2012-01-01 00:00:00'), Timestamp('2013-01-01 00:00:00'), Timestamp('2014-01-01 00:00:00'), Timestamp('2015-01-01 00:00:00'), Timestamp('2016-01-01 00:00:00'), Timestamp('2017-01-01 00:00:00')], 'var1': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'var3': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'var2': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}

Below is part of the code I use for generating plot line with appropriate legend.
Please help
Thanks,
Goran

s4= {'females': {'2012': 76.9, '2007': 75.9, '2005': 75.9, '2014': 77.5, '2004': 75.8, '2000': 75.2, '2013': 77.5, '2006': 76.2, '2002': 75.6, '2008': 76.5, '2010': 77.2, '2001': 76.1, '2011': 77.2, '2003': 75.7, '2009': 76.7, '2015': 77.4, '2016': 77.5}, 'males': {'2012': 73.0, '2007': 71.8, '2005': 71.6, '2014': 73.5, '2004': 71.5, '2000': 70.8, '2013': 73.4, '2006': 71.7, '2002': 70.6, '2008': 72.4, '2010': 72.9, '2001': 70.9, '2011': 73.1, '2003': 70.9, '2009': 72.3, '2015': 73.5, '2016': 73.4}, 'total': {'2012': 74.9, '2007': 73.8, '2005': 73.7, '2014': 75.5, '2004': 73.6, '2000': 73.0, '2013': 75.5, '2006': 73.9, '2002': 73.0, '2008': 74.4, '2010': 75.0, '2001': 73.4, '2011': 75.1, '2003': 73.2, '2009': 74.4, '2015': 75.5, '2016': 75.4}}

if len(list(s4.columns)) == 1:
        source = ColumnDataSource(data=dict(Year=list(s4.index),
                                            Value=list(s4[list(s4.columns)[0]]),
                                            var1=[0]*len(list(s4.index)),
                                            var2=[0]*len(list(s4.index)),
                                            var3=[0]*len(list(s4.index)),
                                            var4=[0]*len(list(s4.index))))
        
    elif len(list(s4.columns)) == 2:
        source = ColumnDataSource(data=dict(Year=list(s4.index),
                                            Value=[0]*len(list(s4.index)),
                                            var1=list(s4[list(s4.columns)[0]]),
                                            var2=list(s4[list(s4.columns)[1]]),
                                            var3=[0]*len(list(s4.index)),
                                            var4=[0]*len(list(s4.index))))
    elif len(list(s4.columns)) == 3:
        source = ColumnDataSource(data=dict(Year=list(s4.index),
                                            Value=[0]*len(list(s4.index)),
                                            var1=list(s4[list(s4.columns)[0]]),
                                            var2=list(s4[list(s4.columns)[1]]),
                                            var3=list(s4[list(s4.columns)[2]]),
                                            var4=[0]*len(list(s4.index))))
    elif len(list(s4.columns)) == 4:
        source = ColumnDataSource(data=dict(Year=list(s4.index),
                                            Value=[0]*len(list(s4.index)),
                                            var1=list(s4[list(s4.columns)[0]]),
                                            var2=list(s4[list(s4.columns)[1]]),
                                            var3=list(s4[list(s4.columns)[2]]),
                                            var4=list(s4[list(s4.columns)[3]])))

def make_plotLine(source, title, subtitle, yunits):
    cols = list(s4.columns)
    b= ['0']*(4 - len(cols))
    cols.extend(b)

    if list(s4.columns) == 1:
        b= ['0']*(4 - len(cols))
        cols.extend(b)
    elif list(s4.columns) == 2:
        cols.insert(0, "0")
        b= ['0']*(4 - len(cols))
        cols.extend(b)

    elif list(s4.columns) == 3:
        cols.insert(0, "0")
        b= ['0']*(4 - len(cols))
        cols.extend(b)

    elif list(s4.columns) == 4:
        cols.insert(0, "0")
        b= ['0']*(4 - len(cols))
        cols.extend(b)
        
    color = ['red', 'blue', 'green', 'orange']
    colors = color[:len(s4.columns)]
    
    p2 = figure(x_axis_type="datetime", title=title, plot_width=800, plot_height=500)

    p21 = p2.line(x='Year', y='Value', source=source, line_width=4, alpha=0.5, color=color[0])
    p22 = p2.line(x='Year', y='var1', source=source, line_width=4, alpha=0.5, color=color[0])
    p23 = p2.line(x='Year', y='var2', source=source, line_width=4, alpha=0.5, color=color[1])
    p24 = p2.line(x='Year', y='var3', source=source, line_width=4, alpha=0.5, color=color[2])
    p25 = p2.line(x='Year', y='var4', source=source, line_width=4, alpha=0.5, color=color[3])

    if list(s4.columns) == 1:
        p21.visible = True
        p22.visible = False
        p23.visible = False
        p24.visible = False
        p25.visible = False

    elif list(s4.columns) == 2:
        p21.visible = False
        p22.visible = True
        p23.visible = True
        p24.visible = False
        p25.visible = False

    elif list(s4.columns) == 3:
        p21.visible = False
        p22.visible = True
        p23.visible = True
        p24.visible = True
        p25.visible = False

    elif list(s4.columns) == 4:
        p21.visible = False
        p22.visible = True
        p23.visible = True
        p24.visible = True
        p25.visible = True
        
    p2.xaxis.axis_label = None
    p2.yaxis.axis_label = yunits
    p2.x_range = DataRange1d(range_padding=0.1)
    p2.grid.grid_line_alpha = 0.3
    p2.xaxis.formatter=DatetimeTickFormatter(
        years=["%Y"]
        )
    p2.min_border_bottom = 0
    p2.yaxis.minor_tick_line_color = None
    
    p2.legend.location = "bottom_right"

    legend = Legend(items=[
        (cols[0] , [p22]),
        (cols[1] , [p23]),
        (cols[2] , [p24]),
        (cols[3] , [p25]),
        ], location=(200, -10))
    p2.add_layout(legend, 'below')

    p2.lod_timeout
    
    p2.legend.click_policy="hide"
    p2.legend.orientation = "horizontal"

    p2.title.text_font_size = '11pt'
    
    return p2

def rr(attributes):
    def python_callback(event):
   source.data.update(source1.data)
   plot_line = make_plotLine(source, title, subtitle, yunits)
    plot_line.title.text = title
    plot_bar.title.text = titlebar

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/0ffaa2fe-6e8a-49dc-a120-f11dc5d11db4%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.
<im2.png><im1.png>