Search code examples
pythonplotlydecimalstring-formatting

How to specify data type in plotly table?


I created a table using plotly, which contains mixed types of values eg, text, number and percentage. Table which I created look like this: enter image description here

as you can see some of the numbers has very large decimal places or some numbers (like PLF column) are not in percentage form. How can I specify the format for these numbers in plotly? my attempt:

import plotly.graph_objs as go
fig = go.Figure(data=[go.Table(header=dict(values=['<b>Source</b>', '<b>Installed Capacity (MW)</b>',
                                                       '<b>Optimum Capacities(MW)</b>', '<b>Generation Units (MWh)</b>',
                                                       '<b>PLF (%)</b>', '<b>Generation Share (%)</b>',
                                                       '<b>Annual Generation (MU)</b>']),
                                   cells=dict(values=[list(installCapacities.keys()), list(installCapacities.values()),
                                                      list(optimumCapacities.values()), list(generationUnits.values()),
                                                      list(plf.values()), list(generationShare.values()),
                                                      list(annualGeneration.values())],
                                              align='center', format=['text', 'number', 'number', 'number', 'percentage',
                                                                      'percentage', 'number']))
                          ])

values are from different dictionaries and their calculation is very complex that's why I didn't mentioned that here, I just want to how can I specify their data type like text, number or percentage?


Solution

  • According to the documentation, you specify the format in D3 3.x API.. For example, the string .2% would format your number as a percentage with two digits after the decimal.

    Here is an example:

    import plotly.graph_objects as go
    
    fig = go.Figure(data=[go.Table(
        header=dict(values=["perc","number","text"]),
        cells=dict(values=[["0.111111","0.2333","0.92"],["123","456","789"],["1234text","1234","text"]],
        align="center", format=[".2%","g",""]))]
    )
    fig.show()
    

    enter image description here