[英]How to align data points and tick labels?
I want to draw a grid with circles and label each row and column.我想用圆圈和 label 每行和列绘制一个网格。 While I can draw the data points, I fail to properly align the data points and their respective labels, so the plot looks like this:
虽然我可以绘制数据点,但我无法正确对齐数据点及其各自的标签,因此 plot 看起来像这样:
How can it be done correctly, so that the tick labels align with the datapoints?如何正确完成,以便刻度标签与数据点对齐?
The code:编码:
import numpy as np
import itertools
from bokeh.models import Circle, ColumnDataSource
from bokeh.plotting import figure, show
space = 0.5
row_ind = np.arange(0, 2, space)
col_ind = np.arange(0, 1.5, space)
data_points = list(itertools.product(row_ind, col_ind))
rows = [dp[0] for dp in data_points]
cols = [dp[1] for dp in data_points]
size = 10
sizes = [size] * len(data_points)
source = ColumnDataSource(dict(columns=cols, rows=rows, size=sizes))
plot = figure(
plot_width=400,
plot_height=300,
x_range=["1", "2", "3"],
y_range=list("ABCD")
)
plot.circle(
x="columns",
y="rows",
size="size",
line_color="black",
fill_color="white",
line_width=2,
source=source
)
show(plot)
You should make use of the dafault ranges from bokeh and change the ticks afterwards using plot.xaxis.ticker
, plot.yaxis.ticker
and plot.yaxis.major_label_overrides
.您应该使用散景中的默认范围,然后使用
plot.xaxis.ticker
、 plot.yaxis.ticker
和plot.yaxis.major_label_overrides
更改刻度。
Complete Example完整示例
import numpy as np
import itertools
from bokeh.models import Circle, ColumnDataSource
from bokeh.plotting import figure, show, output_notebook
output_notebook()
row_ind = np.arange(0, 2, 0.5)
col_ind = np.arange(0, 1.5, 0.5)
data_points = list(itertools.product(row_ind, col_ind))
rows = [dp[0] for dp in data_points]
cols = [dp[1] for dp in data_points]
sizes = [10] * len(data_points)
source = ColumnDataSource(dict(columns=cols, rows=rows, size=sizes))
plot = figure(
plot_width=400,
plot_height=300
)
plot.circle(
x="columns",
y="rows",
size="size",
line_color="black",
fill_color="white",
line_width=2,
source=source
)
plot.xaxis.ticker, plot.yaxis.ticker = col_ind, row_ind
plot.yaxis.major_label_overrides = dict(zip(
[int(i) if i.is_integer() else i for i in row_ind],
list('ABCD'))
)
show(plot)
Output Output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.