Plotting financial data priced in 32nds

Hi I am trying to plot some financial US Treasury bond data correctly in plotly and not sure what the best approach is (Tick size - Wikipedia). This data is priced in 32nds so if the decimal price is 110.50, the correct way to display it is 110-16. I have functions which will convert from decimal to tic 32nd (as a string β€œ110-16” or as a decimal 110.16) and I would like to display this as a line graph where the axis (and hovers) are correctly displayed.

For instance if I have the following series

x = [1,2,3,4,5]
y1 =  [100,100.0625,100.25,100.03125,100.96875,101.00,100.00] 

in tic this is equal to

y2 = ["100-00", "100-08", "100-03",  "100-01", "100-31","101-00","100-00"]

or an alternative notation using decimal convention is

y3 = [100.00.100.08,100.03,100.01,100.32,101.00,100.00]

if I create a series

import plotly.express as px
import pandas as pd
 
df = pd.Series(data=y,index=x)
px.line(df)

if I use y=y1 it obviously works but does not display the tic

if I use y=y3 it sort of works but ideally I would like to replace the decimal point β€œ.” with a β€œ-”. In addition there would be large jumps between 101.31 and 102.00 which is just 1/32.

if I use y=y2 then is obviously doesn’t work since they are assumed as strings. I thought about using y1 and then replacing the decimal values with tic values on the y axis but not sure if a) this is a good method and or b) there is a better way of going this.

Any ideas / advice would be great.

Thanks

Hi @Beebeed
:wave: welcome to the community.

You could try to update the layout β†’ yaxis like this:

import plotly.express as px
import pandas as pd

x = [1,2,3,4,5,6,7]
y1 = [100,100.0625,100.25,100.03125,100.96875,101.00,100.00]
y2 = ["100-00", "100-08", "100-03","100-01", "100-31","101-00","100-00"]
y3 = [100.00,100.08,100.03,100.01,100.32,101.00,100.00]

df = pd.Series(data=y3, index=x)
fig = px.line(df)
fig.update_layout(
    yaxis = dict(
        tickmode = 'array',
        tickvals = y3,
        ticktext = y2
    )
)
fig.show()

Thanks Adam,

Getting there and I did try that… the issue here is that all the tick labels are displayed. For instance if I generate some fake longer data then you can see the issues

import plotly.express as px
import pandas as pd
import numpy as np

x = np.arange(0,1000)
y32 = np.random.randint(0,31,1000)
y0 = np.random.randint(100,110,1000)
y = y0 + y32/32

yp = []
for p in list(zip(y0,y32)):
  yp.append(f"{p[0]}-{p[1]}")



df = pd.Series(data=y, index=x)
fig = px.line(df)
fig.update_layout(
    yaxis = dict(
        tickmode = 'array',
        tickvals = y,
        ticktext = yp
    )
)
fig.show()

in this case you get output similar to this where you can see all that every ytick label is dislayed.

I often use slicing as a method to thin out the scale, is that appropriate for your purposes?

fig.update_layout(
    yaxis = dict(
        tickmode = 'array',
        tickvals = y[::40],
        ticktext = yp[::40]
    )
)
fig.update_layout(autosize=True, height=400,)

Alternatively, you can create the items you wish to display.

fig.update_layout(
    yaxis = dict(
        tickmode = 'array',
        tickvals = np.arange(100,111,1),
        ticktext = ['{}-32'.format(x) for x in np.arange(100,111,1)]
    )
)