Skip to main content
 首页 » 编程设计

python之Plotly Python之标签值格式化

2024年09月07日7bluestorm

如何设置标签值的格式,使其显示实际的小数而不是自动设置为微单位?

例如我希望它显示为 0.0001 而不是 100µ,绿色标签中没有自动格式化。

12/2/18 更新:

这是一些示例数据(macd_output 变量):

                         macd  macdsignal      macdhist 
datetime                                          
2018-02-12 19:44:00 -0.002401   -0.002316 -8.433693e-05 
2018-02-12 19:45:00 -0.002353   -0.002325 -2.734004e-05 
2018-02-12 19:46:00 -0.002107   -0.002271  1.641191e-04 
2018-02-12 19:47:00 -0.002040   -0.002213  1.732582e-04 
2018-02-12 19:48:00 -0.001814   -0.002113  2.990831e-04 
2018-02-12 19:49:00 -0.001934   -0.002068  1.344275e-04 
2018-02-12 19:50:00 -0.001814   -0.002005  1.910672e-04 
2018-02-12 19:51:00 -0.002187   -0.002050 -1.370391e-04 
2018-02-12 19:52:00 -0.002438   -0.002147 -2.903001e-04 
2018-02-12 19:53:00 -0.003135   -0.002394 -7.411783e-04 
2018-02-12 19:54:00 -0.003450   -0.002658 -7.918092e-04 
2018-02-12 19:55:00 -0.003242   -0.002804 -4.379292e-04 
2018-02-12 19:56:00 -0.003066   -0.002870 -1.962050e-04 
2018-02-12 19:57:00 -0.003105   -0.002929 -1.769048e-04 
2018-02-12 19:58:00 -0.002929   -0.002929 -6.099654e-07 

这是生成绘图的 python 代码部分:

... 
macd_output = MACD(ohlcv,fastperiod=params['macd_parameters']['fast'], slowperiod=params['macd_parameters']['slow'], signalperiod=params['macd_parameters']['signal']) 
 
print macd_output 
 
macdhist = go.Bar( 
    x=macd_output.index, 
    y=macd_output.macdhist 
) 
 
macd = go.Scatter( 
    x=macd_output.index, 
    y=macd_output.macd 
) 
 
macdsignal = go.Scatter( 
    x=macd_output.index, 
    y=macd_output.macdsignal 
) 
 
fig = tools.make_subplots(rows=1, cols=1) 
 
fig.append_trace(macdhist, 1, 1) 
fig.append_trace(macd, 1, 1) 
fig.append_trace(macdsignal, 1, 1) 
 
 
update_dict = dict( 
    xaxis = dict( 
        rangeslider = dict( 
            visible = False 
        ), 
        showgrid=True, 
    ) 
) 
 
print fig['layout'].update(update_dict) 
 
 
plotly.offline.iplot(fig) 

另一个更新:

感谢安妮的回答。这对我有用:

fig['layout']['yaxis1'].update(tickformat="0.5r") 

请您参考如下方法:

我认为你需要使用 d3 格式的标志:

d3.format("r")(0.0001) 

r - decimal notation, rounded to significant digits.

您可以阅读更多相关信息 here :

tickformat (string) default: ""
Sets the tick label formatting rule using d3 formatting mini-languages which are very similar to those in Python. For numbers, see: https://github.com/d3/d3-format/blob/master/README.md#locale_format