初学Bokeh:使用对数坐标轴【20】跬步

发布时间 2023-11-03 11:56:59作者: ohfaint

使用对数坐标轴

如果需要使用对数坐标轴。可以使用如下设置:y_axis_type="log" 即可以切换到对数轴:

# 引入库
from bokeh.plotting import figure, show

# prepare some data
# 定义显示数据
x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y0 = [i**2 for i in x]
y1 = [10**i for i in x]
y2 = [10**(i**2) for i in x]

# create a new plot with a logarithmic axis type
# 创建绘图
p = figure(
    title="Logarithmic axis example",   # 标题
    sizing_mode="stretch_width",    # 自动横向拉伸
    height=300, # 高度
    max_width=500,  # 最大宽度
    y_axis_type="log",  # y周坐标类型:log
    y_range=[0.001, 10 ** 11],  # y轴范围
    x_axis_label="sections",    # x轴标签
    y_axis_label="particles",   # y轴标签
)

# add some renderers
p.line(x, x, legend_label="y=x")    # 创建line对象
p.circle(x, x, legend_label="y=x", fill_color="white", size=8)  # 创建circle对象
p.line(x, y0, legend_label="y=x^2", line_width=3)   # 创建line对象
p.line(x, y1, legend_label="y=10^x", line_color="red")  # 创建line对象
p.circle(x, y1, legend_label="y=10^x", fill_color="red", line_color="red", size=6)  # 创建circle对象
p.line(x, y2, legend_label="y=10^x^2", line_color="orange", line_dash="4 4")    # 创建line对象

# show the results
# 显示图
show(p)

fig20-1

可见图的y轴当前为已经改为对数坐标。