简体   繁体   中英

Plotly: How to format numeric yaxis and hover text?

I have a python plotly graph with numeric yaxis, it automatically formats the yaxis numbers to 'k' scale and and when hovering the point, shows values like `115.277258k.

How can I change the scale to display 100,000 instead of 100k and also format the hovering text to show 115,2772.58 instead of 115.277258k ?

plotly graph example:

在此处输入图片说明

Currently plotting multiple subplots using a loop.

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)

Thanks!

You can change the hovertemplate for go.Scatter() like this:

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

And you can edit the tick labels using update_layout(yaxis=dict()) like this:

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

Plot:

在此处输入图片说明

Complete code:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM