【问题标题】:Plotly: How to format numeric yaxis and hover text?Plotly:如何格式化数字 yaxis 和悬停文本?
【发布时间】:2018-06-25 03:12:25
【问题描述】:

我有一个带有数字 y 轴的 python 绘图图,它会自动将 y 轴数字格式化为“k”比例,并且当悬停该点时,会显示像 `115.277258k 这样的值。

如何将比例更改为显示 100,000 而不是 100k 并将悬停文本格式化为显示 115,2772.58 而不是 115.277258k

plotly graph example:

目前正在使用循环绘制多个子图。

for i in range(1, allcities.size + 1):
    for col in ['total_inflow', 'total_outflow', 'total_withdraw', 'total_account']:
        plot_data = hist[hist['city_id'] == allcities[i - 1]]
        plot_data = plot_data.merge(datelist, on='balance_date', how='right')
        fig.append_trace({'x': plot_data['balance_date'],
                          'y': round(plot_data[col], 2),
                          'type': 'scatter',
                          'mode': 'lines', 
                          'line': dict(color=line_colors[col]),
                          'showlegend': False,
                          'name': line_legends[col]
                          }, i + 1, 1)

谢谢!

【问题讨论】:

    标签: python plotly


    【解决方案1】:

    您可以像这样更改go.Scatter() 的悬停模板:

    hovertemplate = 'y:%{y:20,.2f}'
    

    您可以像这样使用update_layout(yaxis=dict()) 编辑刻度标签:

    yaxis=dict(tickformat="20,.2f")
    

    剧情:

    完整代码:

    import plotly.graph_objects as go
    import numpy as np
    
    x = np.arange(10)
    
    fig = go.Figure(data=go.Scatter(x=x**6, y=x**4,
                                    mode='lines+markers',
                                    hovertemplate = '<i>y</i>:%{y:20,.2f}'+ '<br><b>x</b>: %{x}<br>',
    
    fig.update_layout(yaxis=dict(tickformat="20,.2f"),
                      xaxis=dict(tickformat="20,.2f", tickangle = 0))
    
    
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 2018-08-30
      • 2021-04-04
      • 2021-04-13
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      • 1970-01-01
      • 2021-04-19
      相关资源
      最近更新 更多