首页 > 解决方案 > python数据框散点图:在同一行的值之间画线

问题描述

我设法在一个图中显示了来自同一数据帧的两个散点图,并尝试将同一行中的点与图中的一条线链接起来。任何人都可能知道我该怎么做?谢谢。

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)

标签: pythondataframehyperlinklinescatter

解决方案


您没有提供数据示例,因此我为您制作了一个可重复的示例。

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

这是原始散点图:

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

以下是它们之间的界限:

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)

结果:

在此处输入图像描述


推荐阅读