Does Jitter not work with Categorical/Factor Plots?

I am struggling to get the Jitter transform to behave properly with categorical plots. Was it not intended to work with this type?

import numpy as np
import pandas as pd
from bokeh.models import Jitter, ColumnDataSource, FactorRange
from bokeh.plotting import figure, show, output_file
from bokeh.layouts import row, column, widgetbox

np.random.seed()
letters = np.random.choice(list('abcde'), 40)
numbers = np.random.uniform(1,10, 40).round(1)
jitter = 0.3

df = pd.DataFrame(zip(letters,numbers), columns=['letters', 'numbers'])
cds = ColumnDataSource(df)

# I prefer to build categorical plots by instantiating a figure with an empty factor range.
# Figure 3 should demonstrate this is not contributing to the issue.
x_factor_range = FactorRange(factors=[])

fig1 = figure(title="Categorical with No Jitter", x_range=x_factor_range)
fig1.x_range.factors = list('abcde')

<details class='elided'>
<summary title='Show trimmed content'>&#183;&#183;&#183;</summary>

#
fig2 = figure(title="Categorical with Jitter", x_range=x_factor_range)
fig2.x_range.factors = list('abcde')

fig3 = figure(title="Categorical with Jitter", x_range=list('abcde'))

# Specifying with DataSpec and no jitter.
fig1.circle(
    x={'field': 'letters'},
    y='numbers',
    source=cds,
    line_color='black',
    line_alpha=1.0,
    size=9
)

# Trying to jitter.
fig2.circle(
    x={'field': 'letters', 'transform': Jitter(width=jitter)},
    y='numbers',
    source=cds,
    line_color='black',
    line_alpha=1.0,
    size=9
)

# Try to jitter using a figure that was instantiated differently.
fig3.circle(
    x={'field': 'letters', 'transform': Jitter(width=jitter)},
    y='numbers',
    source=cds,
    line_color='black',
    line_alpha=1.0,
    size=9
)

output_file("jitter.html")
show(row(fig1, fig2, fig3))

It's certainly intended to some day, but I cannot say that it does, yet. There is some work coming up to improve categoricals and to support nested coordinate systems. Jitter working will fall out of that naturally, if it doesn't already.

If it doesn't work now, an albeit clunky workaround could be: convert all your factors to integers, use a regular axis with fixed ticker, and custom tick formatter to print the factor names instead of the integer coordinates.

Thanks,

Bryan

ยทยทยท

On Oct 7, 2016, at 5:08 PM, wcopeland <[email protected]> wrote:

I am struggling to get the Jitter transform to behave properly with categorical plots. Was it not intended to work with this type?

import numpy as np
import pandas as pd
from bokeh.models import Jitter, ColumnDataSource, FactorRange
from bokeh.plotting import figure, show, output_file
from bokeh.layouts import row, column, widgetbox

np.random.seed()
letters = np.random.choice(list('abcde'), 40)
numbers = np.random.uniform(1,10, 40).round(1)
jitter = 0.3

df = pd.DataFrame(zip(letters,numbers), columns=['letters', 'numbers'])
cds = ColumnDataSource(df)

# I prefer to build categorical plots by instantiating a figure with an empty factor range.
# Figure 3 should demonstrate this is not contributing to the issue.
x_factor_range = FactorRange(factors=)

fig1 = figure(title="Categorical with No Jitter", x_range=x_factor_range)
fig1.x_range.factors = list('abcde')

#
fig2 = figure(title="Categorical with Jitter", x_range=x_factor_range)
fig2.x_range.factors = list('abcde')

fig3 = figure(title="Categorical with Jitter", x_range=list('abcde'))

# Specifying with DataSpec and no jitter.
fig1.circle(
    x={'field': 'letters'},
    y='numbers',
    source=cds,
    line_color='black',
    line_alpha=1.0,
    size=9
)

# Trying to jitter.
fig2.circle(
    x={'field': 'letters', 'transform': Jitter(width=jitter)},
    y='numbers',
    source=cds,
    line_color='black',
    line_alpha=1.0,
    size=9
)

# Try to jitter using a figure that was instantiated differently.
fig3.circle(
    x={'field': 'letters', 'transform': Jitter(width=jitter)},
    y='numbers',
    source=cds,
    line_color='black',
    line_alpha=1.0,
    size=9
)

output_file("jitter.html")
show(row(fig1, fig2, fig3))

--
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/6438b147-2bc4-488a-bb83-0009d2b6c93b%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.