Adding variable names to lists

0 votes
asked Sep 12, 2017 by thatch

So, I'm dynamically creating a bokeh plot and need to create a tuple like this

('Matbock', [Matbockl, Matbockc]) 

The items in the list portion need to be variable names (no quotes) I'm currently able to create the tuple as all strings, ('Matbock', ['Matbockl', 'Matbockc']) but that doesn't work within the Bokeh legend code. I am dynamically creating elements for the chart and I need to reference those lines and circles (Matbockl and Matbockc) within the legend list of tuples and I'm at a bit of an impass as to how to do that.

Basically, the Bokeh part of it isn't important, just the ability to create a tuple with variable names instead of strings is what I'm after. These then get ammended into a list of tuples for use within the legend declaration of Bokeh.

[('Matbock', [Matbockl, Matbockc]), ('Atlas', [Atlasl, Atlasc])]

Any help would be appreciated.

To clarify further, or perhaps to simplify the question, the instantiation of 'Matbockl' and 'Matbockc' aren't the issue. I have done it both as dictionary references and as dynamic variables (yes I know, but I was trying to figure out how to get the name passed) and the problem always comes back to how to pass the information into the Bokeh legend.

At the end of the day, I am trying to pass my chart items into this...

legend = Legend(items=[
    ("iLitter",   [ll1, lc1]),
    ("Zagnut the destroyer",   [ll1, lc1]),
    ("Woot",   [ll1, lc1])
], location=(5, -40))

I am attempting to build the 'item' list you see above. I couldn't figure out how to build that items list as it is displayed above. Nothing I tried would get me this exact construct. Most commonly I'd have quotes around my variable references "Il1", which cause the chart to fail.

1 Answer

0 votes
answered Sep 12, 2017 by lyend-victorian

So if I understand the question you're trying to make a list and fill it with tuples dynamically with functions.

here's an example

a = 'name'
b = ['my', 'list']
>>> ('name', ('my', 'list'))

or if you specifically want a list in the tuple

tuple([a, b])
>>> ('name', ['my', 'list'])
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter