溫馨提示×

Bokeh中怎么自定義軸標簽格式

小億
91
2024-05-20 20:13:32
欄目: 編程語言

要在Bokeh中自定義軸標簽格式,可以使用FuncTickFormatter類來定義要顯示的標簽格式。下面是一個示例代碼:

from bokeh.models import FuncTickFormatter

# 定義自定義的軸標簽格式函數(shù)
def custom_format_ticks(tick):
    return f"${tick:.2f}"

# 創(chuàng)建一個x軸的FuncTickFormatter對象
x_axis_formatter = FuncTickFormatter(code="""
    return custom_format_ticks(tick);
""")

# 創(chuàng)建一個y軸的FuncTickFormatter對象
y_axis_formatter = FuncTickFormatter(code="""
    return custom_format_ticks(tick);
""")

# 設置x軸和y軸的軸標簽格式
plot.xaxis.formatter = x_axis_formatter
plot.yaxis.formatter = y_axis_formatter

在上面的示例代碼中,我們定義了一個名為custom_format_ticks的函數(shù)來定義要顯示的格式。然后,我們創(chuàng)建了一個FuncTickFormatter對象,并將其應用于x軸和y軸。最后,我們將自定義的格式函數(shù)應用于軸標簽。

0