Using texttemplate to format a floating point number without the leading 0

I’m using texttemplate to format numbers on a bar graph. The numbers look like 0.5932 or 0.9492. I want to limit to 3 decimal places, but I also want to remove the leading 0’s, so on the graph, i want to show .593 and .949.

I’m currently using:

fig.updatetraces(texttemplate=’%{value:.3f}’)

This reduces the printing to 3 decimal places, but does not remove the leading 0 before the decimal.

Is there a way to do this?

1 Like

I needed something similar for a heatmap I generated using plotly imshow(). Unfortunately, I do not think there exists a straightforward method to do this in the texttemplate format. I ended up using text annotations.

Working Example:

m, n = (3, 4)
arr = np.random.rand(m, n)
fig = px.imshow(arr, x=list(map(str, range(n))), y = list(map(str, range(m))), origin="lower")
for row in range(m):
    for col in range(n):
        fig.add_annotation(
            x=col,
            y=row,
            showarrow=False,
            text=f"{arr[row, col]:.3f}".lstrip("0"), # Use lstrip() to remove the leading zero
            font=dict(color="white" if arr[row, col] < 0.5 else "black"),
        )
fig.update_layout(width=600, height=500)
fig.show()

Output:
newplot

Thank you ! This is helpful.