0

I have a dataframe of XY coordinates which I'm plotting as Markers in a Scatter plot. I'd like to add_trace lines between specific XY pairs, not between every pair. For example, I'd like a line between Index 0 and Index 3 and another between Index 1 and Index 2. This means that just using a line plot won't work as I don't want to show all the connections. Is it possible to do it with a version of iloc or do I need to make my DataFrame in 'Wide-format' and have each XY pair as separate column pairs? I've read through this but I'm not sure it helps in my case. Adding specific lines to a Plotly Scatter3d() plot

enter image description here

import pandas as pd
import plotly.graph_objects as go

# sample data
d={'MeanE': {0: 22.448461538460553, 1: 34.78435897435799, 2: 25.94307692307667, 3: 51.688974358974164},
   'MeanN': {0: 110.71128205129256, 1: 107.71666666666428, 2: 140.6384615384711, 3: 134.58615384616363}}

# pandas dataframe
df=pd.DataFrame(d)

# set up plotly figure
fig = go.Figure()

fig.add_trace(go.Scatter(x=df['MeanE'],y=df['MeanN'],mode='markers')) 
       
fig.show()

enter image description here

UPDATE: Adding the accepted answer below to what I had already, I now get the following finished plot. enter image description here

1 Answer 1

1
  • taken approach of updating data frame rows that are the pairs of co-ordinates where you have defined
  • then add traces to figure to complete requirement as a list comprehension
import pandas as pd
import plotly.graph_objects as go

# sample data
d={'MeanE': {0: 22.448461538460553, 1: 34.78435897435799, 2: 25.94307692307667, 3: 51.688974358974164},
   'MeanN': {0: 110.71128205129256, 1: 107.71666666666428, 2: 140.6384615384711, 3: 134.58615384616363}}

# pandas dataframe
df=pd.DataFrame(d)

# set up plotly figure
fig = go.Figure()

fig.add_trace(go.Scatter(x=df['MeanE'],y=df['MeanN'],mode='markers')) 

# mark of pairs that will be lines
df.loc[[0, 3], "group"] = 1
df.loc[[1, 2], "group"] = 2
# add the lines to the figure
fig.add_traces(
    [
        go.Scatter(
            x=df.loc[df["group"].eq(g), "MeanE"],
            y=df.loc[df["group"].eq(g), "MeanN"],
            mode="lines",
        )
        for g in df["group"].unique()
    ]
)

fig.show()

enter image description here

alternate solution to enhanced requirement in comments

# mark of pairs that will be lines
lines = [[0, 3], [1, 2], [0,2],[1,3]]
# add the lines to the figure
fig.add_traces(
    [
        go.Scatter(
            x=df.loc[pair, "MeanE"],
            y=df.loc[pair, "MeanN"],
            mode="lines",
        )
        for pair in lines
    ]
)
Sign up to request clarification or add additional context in comments.

6 Comments

That works. If I want to link more, how do I do that? I naively tried this but I only get the last two lines plotted. df.loc[[0, 3], "group"] = 1 df.loc[[1, 2], "group"] = 2 df.loc[[0, 2], "group"] = 3 df.loc[[1, 3], "group"] = 4
your group 3 & 4 use same rows as 1&2 therefore the information on which lines to plot is overwritten. Is your question really you have an list of pairs of rows you want to plot lines for?
I've provided a very similar alternate solution for pairs of rows defined in a list
much appreciated. For this project I am just looking at specific combinations or pairs (I have other projects where I might want to plot all pairs). I've edited my question with the finished plot using your code. I wanted to visualise the diagonal distances between the Mean of a scatter of points.
you need to switch to looping rather than a list comprehension and use add_trace() instead of add_traces()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.