Search code examples
chartsyaxistaipy

Need detailed example how to create line charts with two or more y axis?


I am looking for a detailed code example for a line chart with at least 2 y-axis that have different ranges.

I am aware of the example in the documentation here: https://docs.taipy.io/en/release-2.1/manuals/gui/viselements/charts/basics/#adding-a-trace

But following the given snipets it doesn't work for me. I am assuming that I interpret some of the given code segments not correct.

Any help that shows a complete code example would be helpful.


Solution

  • Here the code you want to look for. The second chart has two y axis.

    from taipy import Gui
    
    x_range = range(-10, 11)
    
    # The data set holds the _x_ series and two distinct series for _y_
    data = {
        "Date": x_range,
        "Starts": [x*x for x in x_range],
        "Duration": [(100-x*x)/50 for x in x_range]
    }
    
    layout = {
        "yaxis2": {
          # Second axis overlays with the first y axis
          "overlaying": "y",
          # Place the second axis on the right
          "side": "right",
          # and give it a title
          "title": "Second y axis"
        },
        "legend": {
          # Place the legend above chart
          "yanchor": "bottom"
        }
    }
    
    page = """
    # Basics - Multiple axis
    
    Shared axis:
    <|{data}|chart|x=Date|y[1]=Starts|y[2]=Duration|height=300px|>
    
    With two axis:
    <|{data}|chart|x=Date|y[1]=Starts|y[2]=Duration|yaxis[2]=y2|layout={layout}|height=300px|>
    """
    
    Gui(page).run()
    

    yaxis[2]=y2 refers to the second 'y' of the graph. This syntax somes from the Plotly yaxis anchor.