1

I'm generating scatter plots with Bokeh with differing numbers Y values for each X value. When Bokeh generates the plot, it automatically pads the x-axis spacing based on the number of values plotted. I would like for all values on the x-axis to be spaced evenly, regardless of the number of individual data points. I've looked into manually setting the ticks, but it looks like I have to set the spacing myself using this approach (ie. specify the exact positions). I would like for it to automatically set the spacing evenly as it does when plotting singular x,y value pairs. Can this be done?

Here is an example showing the behavior.

import pandas
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource

days =['Mon','Mon','Mon', 'Tues', 'Tues', 'Weds','Weds','Weds','Weds']
vals = [1,3,5,2,3,6,3,2,4]

df = pandas.DataFrame({'Day': days, 'Values':vals})

source = ColumnDataSource(df)

p = figure(x_range=df['Day'].tolist())
p.circle(x='Day', y='Values', source=source)
show(p)
JeremyD
  • 125
  • 1
  • 3
  • 8

1 Answers1

1

You are passing a list of strings as the range. This creates a categorical axis. However, the list of categories for the range is expected to be unique, with no duplicates. You are passing a list with duplicate values. This is actually invalid usage, and the result is undefined behavior. You should pass a unique list of categorical factors, in the order you want them to appear, for the range.

bigreddot
  • 33,642
  • 5
  • 69
  • 122
  • This is the only way I've been able to pass multiple x values for each y value. Is there another way to do this? I tried using a loop to generate individual glyphs and add them, but that leads to other issues with the categorical. What, then, is the proper way to generate a scatter plot with multiple y values for each x value in bokeh??? – JeremyD Jan 09 '19 at 16:02
  • Bingo! I just tried this: `xrng = list(set(days))` And passed it as the x range: `p = figure(x_range=xrng)` Thank you!! – JeremyD Jan 09 '19 at 16:07
  • Another followup: Using set won't preserve the order of the list. If the order of the list is important (it is in my case), the order can be preserved by using an ordereddict and outputting the keys as a list. – JeremyD Jan 09 '19 at 16:20
  • Categorical factors are arbitrary. Bokeh has no way of deducing what order a user might want them in. If you need them in a certain order, you simply have to put them in that order, by whatever means is convenient for your case, e.g. manually, by using a sort function, etc. – bigreddot Jan 09 '19 at 16:27