Patch glyph source problems

I am trying to plot patches using source, but I am running into issues. I have gotten this to work in (from what I can tell) almost identical ways, but I cannot for the life of me figure out what I am doing wrong here.

Here is a minimal example:

import pandas as pd
from bokeh.io import show
from bokeh.plotting import figure
from bokeh.models import HoverTool

url='https://raw.githubusercontent.com/mmcguffi/random_test_cases/master/test.csv'
df=pd.read_csv(url,index_col=0)
df['x'] = pd.eval(df['x']) #convert to list
df['y'] = pd.eval(df['y']) #convert to list

hover = HoverTool(names=["1"])
TOOLTIPS='<font size="3"><b>@Feature</b> — @Type</font> <br> @Description'
plotSize=.35
plotDimen=800
p = figure(plot_height=plotDimen,plot_width=plotDimen, title="", toolbar_location=None, match_aspect=True,sizing_mode='scale_width',
               tools=[hover,], tooltips=TOOLTIPS, x_range=(-plotSize, plotSize), y_range=(-plotSize, plotSize))

for index in df.index:
    p.patch(x='x', y='y', fill_color='fill_color',line_color='line_color',name="1",line_width=2, source=df.loc[[index]])
show(p)

It throws an error about colors, but even if you manually change that, it still does not display the plot. I really dont know what’s going on here any help or a pointer to some clear resources would be very much appreciated!

Hi mmcguffi,

I think what you want may be p.patches– check out this example:
http://docs.bokeh.org/en/latest/docs/gallery/texas.html

I tried it in your code by replacing your for loop with the following. (You’ll also have to add ColumnDataSource to your import from bokeh.models.)

source = ColumnDataSource(df)
p.patches('x', 'y', fill_color='fill_color', line_color='line_color', name="1", line_width=2, source=source)

End result looked like this:

Neat! Hope it’s close to what you’re going for. :slight_smile:

2 Likes

Wow this solves everything! Thank you so much @carolyn. Im not sure why the for loop approach worked with annular_wedge glyph, but in any case I appreciate the elegance of this approach and the reduced complexity.

1 Like

FYI most of Bokeh’s glyphs are “vectorized” (patch is one of the few exceptions). There are always special cases and exceptions, but in general it will always be more efficient to load all the data up front to one vectorized glyph function call, rather than looping and calling the glyph function over and over with individual data items.

1 Like

In a similar vein as the above problem I had, is there a reason the text_align parameter on the text glyph cannot input data from a source?

p.text(x="Lx1", y="Ly1",name="1",x_offset=0,y_offset=0, text_align="annoPos", text='Feature', level="annotation", source=source)

All of the other parameters work as expected, but I get this error:

More or less, it would take some extra work, and no one has ever asked for it.

Ah I see. I made a workaround by just filtering the DF and applying the text glyph twice. Thanks!

1 Like