Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly: How to display Axis ticks in hexadecimal?

Tags:

python

plotly

In plotly, is there a way to display a given axis' ticks in hexadecimal format rather than as a large scientific number? This is useful to plot address traces

like image 809
Diastolic Duke Avatar asked Dec 22 '25 18:12

Diastolic Duke


1 Answers

You can do this with [str(hex(elem)) for elem in [1000000, ..., 5000000]

Plot:

enter image description here

Some details to be aware of:

To my knowledge you'll have to use a list of type string to obtain the desired behavior. Values such as [1000000, 2000000, 3000000, 4000000, 5000000] will by default be displayed as:

enter image description here

And that will happen even for [str(hex(elem)) for elem in xVals]. So you'll have to explicitly instruct plotly to display the ticks as strings with:

fig.update_xaxes(tickmode = 'array',
                 tickvals = xHex,
                 ticktext= xHex)

Complete code:

# imports
import plotly.express as px
import plotly.graph_objs as go
import pandas as pd
import numpy as np

import plotly.express as px

xVals = [1000000, 2000000, 3000000, 4000000, 5000000]

xHex = [str(hex(elem)) for elem in xVals]

fig = px.scatter(x=xHex, y=[0, 1, 4, 9, 16])
fig.show()

fig.update_xaxes(tickmode = 'array',
                 tickvals = xHex,
                 ticktext= xHex)
like image 80
vestland Avatar answered Dec 24 '25 08:12

vestland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!