0

I managed to show two scatter plots from the same dataframe on one figure and try to link the point from the same row with a line on the figure. By any chance anyone might have an idea how i can do it? Thanks.

ax = pldata.plot(kind='scatter', x='column1', y='column2', 
    c='DarkBlue', label='Left', s=25)
pldata.plot(kind='scatter', x='column3', y='column4', c='DarkGreen', 
    label='Right', s=25, ax=ax)

1 Answer 1

1

You didn't provide an example of your data so I made a reproducible example for you.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(20, 4), columns=['x', 'y', 'xx', 'yy'])
print(df)
Out[31]: 
          x         y        xx        yy
0  0.362230  0.678728  0.905515  0.236933
1  0.998008  0.613584  0.425929  0.133023
2  0.236703  0.742487  0.812784  0.237387
3  0.833180  0.417141  0.503885  0.560123
4  0.193055  0.474450  0.249819  0.716194

Here's the original scatterplot:

plt.scatter(df.x, df.y)
plt.scatter(df.xx, df.yy)

And here are the lines between them:

for i in range(df.shape[0]):
    a, b, c, d = zip(df.iloc[i, :])
    plt.plot([a, c], [b, d], c='black', alpha=.3)

The result:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Please do not add a comment on your question or on an answer to say "Thank you". Comments are meant for requesting clarification, leaving constructive criticism, or adding relevant but minor additional information – not for socializing. If you want to say "thank you," vote and accept that person's answer. READ: What should I do when someone answers my question?.

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.