Image with CDS from DataFrame leads to BokehUserWarning

Hello,

I am having difficulties to create an image plot with a column data source from a DataFrame. This results in the following error message:

BokehUserWarning: ColumnDataSource’s columns must be of the same length

``

My test code:

from bokeh.client import push_session
from bokeh.models import ColumnDataSource
from bokeh.plotting import curdoc, figure
import numpy as np
from pandas import DataFrame

Starting bokeh server from here

import subprocess
import time
args = [‘python’, ‘-m’, ‘bokeh’, ‘serve’]
p = subprocess.Popen(args)
time.sleep(1) # time to wait might be longer on other PC

defining CDS

data1 = np.random.rand(5, 20)
df1 = DataFrame(data = data1)

this is working

source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=[data1]))

this is not working

source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=df1))

image

size = 300
ranges = (0,10)
image_plot = figure(title = ‘Image’, plot_width = size, plot_height = size,
x_range=ranges, y_range=ranges)
image_plot.image(image = ‘image’, source = source, x = ‘x’, y=‘y’,
dw=‘dw’, dh=‘dw’, palette=“Viridis256”)

show

session = push_session(curdoc())
session.show(image_plot)
session.loop_until_closed()

``

When I replace

source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=df1))

``

with
source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=[data1]))

``

the image is normally displayed.

What is the error in my attempt to use a DataFrame?

Thanks

Daniel

Hi,

As stated, all the columns of a CDS must be the same length. When you do

  source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=[data1]))

All the columns have length "1" and things work. When you do:

  source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=df1))

That is not the case. The image column is now probably a 2d array with some longer length. This is not permissible, the first version is the correct usage.

Another way of looking at it: Perhaps also worth mentioning that like all the other glyph method, the "image" glyph method is for displaying *multiple* images at once. The column for "image" should a be a list of images. If you want to show one image, then "image" column should be a list of length "1" -- containing a single image inside.

Thanks

Bryan

···

On Apr 14, 2017, at 02:18, 'Daniel Krause' via Bokeh Discussion - Public <[email protected]> wrote:

Hello,

I am having difficulties to create an image plot with a column data source from a DataFrame. This results in the following error message:

BokehUserWarning: ColumnDataSource's columns must be of the same length

My test code:

from bokeh.client import push_session
from bokeh.models import ColumnDataSource
from bokeh.plotting import curdoc, figure
import numpy as np
from pandas import DataFrame

# Starting bokeh server from here
import subprocess
import time
args = ['python', '-m', 'bokeh', 'serve']
p = subprocess.Popen(args)
time.sleep(1) # time to wait might be longer on other PC

# defining CDS
data1 = np.random.rand(5, 20)
df1 = DataFrame(data = data1)

# this is working
# source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=[data1]))

# this is not working
  
# image
size = 300
ranges = (0,10)
image_plot = figure(title = 'Image', plot_width = size, plot_height = size,
                    x_range=ranges, y_range=ranges)
image_plot.image(image = 'image', source = source, x = 'x', y='y',
                 dw='dw', dh='dw', palette="Viridis256")

# show
session = push_session(curdoc())
session.show(image_plot)
session.loop_until_closed()

When I replace
source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=df1))

with
source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=[data1]))

the image is normally displayed.

What is the error in my attempt to use a DataFrame?

Thanks
Daniel

--
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/170704a9-7856-4c2b-bf2c-257d5065e36b%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Thanks Bryan for the explanation.
Many other plotting glyphs are excepting data frames, so it took me a rather long time to figure out that an array is working, and a data frame is not.

I would feel more comfortable if the API for a CDS would be consistend for all glyphs, what do you think of the following idea (for the image glyphs):

Instead of raising the bokeh user warning, the data frame could be implicitly changed to an numpy array like

df_as_numpy_ndarray = [df.as_matrix()]

``

Daniel

···

Am Freitag, 14. April 2017 09:18:51 UTC+2 schrieb Daniel Krause:

Hello,

I am having difficulties to create an image plot with a column data source from a DataFrame. This results in the following error message:

BokehUserWarning: ColumnDataSource’s columns must be of the same length

``

My test code:

from bokeh.client import push_session
from bokeh.models import ColumnDataSource
from bokeh.plotting import curdoc, figure
import numpy as np
from pandas import DataFrame

Starting bokeh server from here

import subprocess
import time
args = [‘python’, ‘-m’, ‘bokeh’, ‘serve’]
p = subprocess.Popen(args)
time.sleep(1) # time to wait might be longer on other PC

defining CDS

data1 = np.random.rand(5, 20)
df1 = DataFrame(data = data1)

this is working

source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=[data1]))

this is not working

source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=df1))

image

size = 300
ranges = (0,10)
image_plot = figure(title = ‘Image’, plot_width = size, plot_height = size,
x_range=ranges, y_range=ranges)
image_plot.image(image = ‘image’, source = source, x = ‘x’, y=‘y’,
dw=‘dw’, dh=‘dw’, palette=“Viridis256”)

show

session = push_session(curdoc())
session.show(image_plot)
session.loop_until_closed()

``

When I replace

source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=df1))

``

with
source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=[data1]))

``

the image is normally displayed.

What is the error in my attempt to use a DataFrame?

Thanks

Daniel

Hi,

The current CDS is rigorously consistent: it is always a mapping of names to columns, and all the columns always have the same length. The column elements may be numbers (common for most glyphs) or can happen to be 2d arrays (for images, etc.). The columns may be python lists or tuples, or NumPy arrays, or Pandas series. But none of that affects the statement above: CDS is a mapping of columns, each column of the same length.

Offhand I don't automatically converting data frames in the way you describe is a good idea. It's not clear to me that it would necessarily always (or even often) be the intended outcome, in which case silently performing such a conversion would lead to subtle and hard to diagnose mis-uses.

Thanks,

Bryan

···

On Apr 23, 2017, at 00:35, 'Daniel Krause' via Bokeh Discussion - Public <[email protected]> wrote:

Thanks Bryan for the explanation.
Many other plotting glyphs are excepting data frames, so it took me a rather long time to figure out that an array is working, and a data frame is not.

I would feel more comfortable if the API for a CDS would be consistend for all glyphs, what do you think of the following idea (for the image glyphs):
Instead of raising the bokeh user warning, the data frame could be implicitly changed to an numpy array like
df_as_numpy_ndarray = [df.as_matrix()]

Daniel

Am Freitag, 14. April 2017 09:18:51 UTC+2 schrieb Daniel Krause:
Hello,

I am having difficulties to create an image plot with a column data source from a DataFrame. This results in the following error message:

BokehUserWarning: ColumnDataSource's columns must be of the same length

My test code:

from bokeh.client import push_session
from bokeh.models import ColumnDataSource
from bokeh.plotting import curdoc, figure
import numpy as np
from pandas import DataFrame

# Starting bokeh server from here
import subprocess
import time
args = ['python', '-m', 'bokeh', 'serve']
p = subprocess.Popen(args)
time.sleep(1) # time to wait might be longer on other PC

# defining CDS
data1 = np.random.rand(5, 20)
df1 = DataFrame(data = data1)

# this is working
# source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=[data1]))

# this is not working
source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=df1))

# image
size = 300
ranges = (0,10)
image_plot = figure(title = 'Image', plot_width = size, plot_height = size,
                    x_range=ranges, y_range=ranges)
image_plot.image(image = 'image', source = source, x = 'x', y='y',
                 dw='dw', dh='dw', palette="Viridis256")

# show
session = push_session(curdoc())
session.show(image_plot)
session.loop_until_closed()

When I replace
source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=df1))

with
source = ColumnDataSource(dict(x=[0],y=[0],dw=[10],dh=[10],image=[data1]))

the image is normally displayed.

What is the error in my attempt to use a DataFrame?

Thanks
Daniel

--
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/9831a853-6819-4b43-b59b-bbbae76af336%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.