You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly Python图表Y轴负数值显示乱码问题求助

Plotly条形图Y轴负数值乱码问题(Databricks环境)

问题场景

在Databricks环境中使用Plotly的make_subplots绘制包含正负值的净收益条形图时,Y轴负数值显示为乱码,尝试过update_layout等方法仍无法解决。

复现代码

import pandas as pd
import plotly.subplots as sp

net_gains = pd.DataFrame()
net_gains["general_net_gain"] = [-2,2,-1,2]

fig = sp.make_subplots(rows=1, cols=1)
fig.add_bar(x=net_gains.index, y=net_gains["general_net_gain"], row=1, col=1)
fig.update_layout(height=400, width=500, showlegend=True)
fig.show()

异常可视化效果

可视化输出

解决方法

1. 强制指定Y轴刻度字体与格式

直接通过设置支持负号的字体和明确刻度格式修复乱码:

fig.update_yaxes(
    tickformat='.0f',  # 可按需调整为小数格式,例如'.1f'
    tickfont=dict(family='Arial, Helvetica, sans-serif')
)

2. 升级依赖或环境

  • 升级Plotly版本:
    %pip install --upgrade plotly
    
  • 切换至更高版本的Databricks Runtime,新版本通常已修复渲染层的兼容性问题。

3. 替代方案:使用Databricks原生可视化

若Plotly的问题难以快速解决,可直接用Databricks内置的display函数生成正常图表:

display(net_gains.plot(kind='bar', figsize=(5, 4)))

内容的提问来源于stack exchange,提问作者marksman123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 11:10:18