問題描述
固定散景散點圖中的軸間距(刻度) (Fixing axis spacing (ticks) in Bokeh scatter plots)
我正在使用散景圖生成散點圖,每個 X 值具有不同的 Y 值。當 Bokeh 生成繪圖時,它會根據繪製的值的數量自動填充 x 軸間距。無論單個數據點的數量如何,我都希望 x 軸上的所有值均勻分佈。我已經研究過手動設置刻度,但看起來我必須使用這種方法自己設置間距(即指定確切的位置)。我希望它能夠像繪製奇異 x,y 值對時那樣自動均勻地設置間距。可以這樣做嗎?
這是一個顯示行為的示例。
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)
參考解法
方法 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.