Cannot set the f.y_range value after I initialize a Figure object

The code below does not work.
p.y_range = FactorRange(factors=cats)
cannot set the y-axis range.

However, if I use:
p = figure( y_range=FactorRange(factors=list(cats)), width=900, x_range=(-5, 105), toolbar_location=None)
it works!

I need to set the y-range after the figure initialisation though, for the purposes of my application. Any thoughts?

Thank you in advance

import colorcet as cc
from numpy import linspace
from scipy.stats.kde import gaussian_kde
from scipy import stats

from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
from bokeh.sampledata.perceptions import probly
from bokeh.models import Div, ColumnDataSource, FactorRange

def ridge(category, data, scale=20):
    return list(zip([category]*len(data), scale*data))

cats = list(reversed(probly.keys()))
palette = [cc.rainbow[i*15] for i in range(17)]
x = linspace(-20,110, 500)

source = ColumnDataSource(data=dict(x=x))

p = figure( width=900, x_range=(-5, 105), toolbar_location=None)
p.y_range = FactorRange(factors=cats)

for cat in reversed(cats):    
    pdf = gaussian_kde(list(probly[cat]))
    y= pdf(x)
    y = ridge(cat, pdf(x))[cat] = y
    p.patch('x', cat, source=source, alpha=0.6, line_color="black",)

@ThemisKoutsellis The error we see is:

ERROR:bokeh.core.validation.check:E-1009 (INCOMPATIBLE_SCALE_AND_RANGE): A Scale is incompatible with one or more ranges on the same plot dimension: incompatibility on y-dimension: FactorRange(id='1299', ...)/LinearScale(id='1275', ...) [Figure(id='1268', ...)]

This sound like the default y_scale = LinearScale() sn’t compatible with y_range = FactorRange that we set later.

Maybe you can initialize with FactorRage and just update the factors later?

p = figure(width=900, x_range=(-5, 105), y_range=FactorRange(), toolbar_location=None)

p.y_range.factors = cats

Indeed. That worked! Thanks! :slight_smile:


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.