简体   繁体   English

如何对齐数据点和刻度标签?

[英]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.tickerplot.yaxis.tickerplot.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.

相关问题 如何对齐 matplotlib 中的刻度标签? - How do you align tick labels in matplotlib? 当数字在 maptlotlib 中有上标时如何对齐刻度标签? - How to align tick labels when numbers have superscript in it in maptlotlib? matplotlib热图的中心对齐刻度标签 - Center-align tick labels of matplotlib heatmap 更改 y 轴刻度标签以最大程度地减少混乱,但不显示除数据集中观察到的数据点之外的数据点 - Altering y-axis tick labels to minimize clutter but NOT showing data points apart from what was observed in the dataset 修改绘图轴,使其刻度标签的顺序及其各自的点相应更改 - 无需修改数据本身 - Modify plot axis so the order of its tick labels and their respective points change accordingly - without modifying the data itself 如何将条形与 plt 或 pandas 直方图中的刻度标签对齐(绘制多列时) - How to align bars with tick labels in plt or pandas histogram (when plotting multiple columns) 如何使用 matplotlib 将主要和次要刻度标签精确对齐 - How to align major and minor tick labels exactly next to each other with matplotlib 如何在x轴上设置日期刻度标签,仅适用于matplotlib上的给定点 - How to set date tick labels on x axis, only for given points on matplotlib 如何旋转特定的刻度标签 - How to rotate specific tick labels 使用自定义刻度标签绘制矩阵数据 - Plot matrix data with custom tick labels
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM