Search code examples
pythonplotlyplotly-python

$ tick formatter in Plotly that respects negative values


Per the answer to Add Currency symbol in the y axis scale using plotly method in python, you can add a currency to a Python Plotly tick formatter via:

fig.update_layout(yaxis_tickprefix = '$', yaxis_tickformat = ',.')

However, this shows negative numbers as $-100 instead of -$100.

How can I format ticks to include $ and respect negative numbers?


Solution

  • If you set the dollar sign in the tick format instead of the prefix, you will get the intended format.

    import plotly.graph_objects as go
    import numpy as np
    
    fig = go.Figure(go.Scatter(
        x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
        y = np.random.randint(-20,20,12)
    ))
    fig.update_layout(yaxis_tickformat='$,')#yaxis_tickprefix='$',
    
    fig.show()
    

    enter image description here