Search code examples
pythonuser-interfacetaipy

Adding Horizontal or Vertical Lines in a Scatterplot using Taipy


I was creating a scatterplot using Taipy and wanted to know the best way to include horizontal or vertical lines on the plot. In Plotly, there is a convenient function called fig.add_vline() to add vertical lines to a scatterplot. However, I need help finding a direct equivalent function in Taipy to achieve the same result.

Expected Behavior:

It would want to know if adding horizontal or vertical lines to a scatterplot is possible. This feature would allow users to easily visualize specific reference lines or thresholds in their scatterplots, aiding in data analysis and interpretation.


Solution

  • Creating a column with constant values is the most straightforward way. You can also do it by customizing the layout of the chart.

    Here is the code for your question:

    from taipy import Gui
    
    data = {
      "x": [2, 3.5, 6],
      "y": [1, 1.5, 1],
    }
    
    layout = {
      "shapes": [
        #line vertical
        {
          "type": 'line',
          "x0": 1, "y0": 0, "x1": 1, "y1": 2,
          "line": {
            "color": 'rgb(55, 128, 191)',
          }
        },
        #Line Horizontal
        {
          "type": 'line',
          "x0": 2, "y0": 2, "x1": 5, "y1": 2,
          "line": {
            "color": 'rgb(50, 171, 96)',
            "dash": 'dashdot'
          }
        },
        #Line Diagonal
        {
          "type": 'line',
          "x0": 4, "y0": 0, "x1": 6, "y1": 2,
          "line": {
            "color": 'rgb(128, 0, 128)',
            "dash": 'dot'
          }
        }
      ]
    }
    
    
    md = "<|{data}|chart|x=x|y=y|layout={layout}|>"
    
    Gui(md).run()
    

    enter image description here