如何强制Plotly对小数使用指数?

时间:2019-06-25 15:19:07

标签: python plotly

如何强制Plotly始终使用指数(科学计数法)? 它适用于大数字,但是当轴上的数字较小时,科学计数法将变为关闭。

下面的示例在运行此代码后以科学计数法表示x轴,但不以y轴表示。

import plotly as py
import plotly.graph_objs as go
import numpy as np

N = 1000
random_x = np.random.randn(N)*100000
random_y = np.random.randn(N)

figure = {
    'data': [
        go.Scatter(
            x=random_x,
            y=random_y,
            mode='markers',
            opacity=0.7,
            marker={
                'size': 5,
                'line': {'width': 0.5, 'color': 'white'}
            },
            name='abc'
        )
    ],
    'layout': go.Layout(
        xaxis={'title': 'x', 'showexponent': 'all', 'exponentformat': 'E', 'rangemode': 'tozero',
               'ticks': 'outside'},
        yaxis={'title': 'y', 'showexponent': 'all', 'exponentformat': 'E', 'rangemode': 'tozero',
               'ticks': 'outside'},
        hovermode='closest'
    )
}

py.offline.plot(figure, filename='filename.html', auto_open=True)

enter image description here

1 个答案:

答案 0 :(得分:0)

我将使用'tickformat'选项而不是'exponantformat'

在您的示例中,这对我有用:

yaxis={'title': 'y', 'tickformat':'e', 'rangemode': 'tozero',
           'ticks': 'outside'}

您还可以指定要使用'tickformat': '.2e'显示的精度