我设法在一个图形上显示来自同一数据帧的两个散点图,并尝试将同一行中的点与图形上的一条线连接起来。任何人都可能知道我如何做到这一点?谢谢
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)
您没有提供数据示例,所以我为您制作了一个可复制的示例。
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)