python - 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)
解决方案
您没有提供数据示例,因此我为您制作了一个可重复的示例。
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)
结果:
推荐阅读
- javascript - 切换多个 div
- mysql - 如何通过解释 docker-compose.yml 中的卷来定位用于备份的主机存储?
- javascript - TypeError:无法读取开玩笑酶测试中未定义的属性
- python - 在 Pandas 中将函数应用于 loc 调用会导致底层数据帧发生变化?
- navigation - ScreenManager 外部的按钮,用于控制 ScreenManager 屏幕加载
- html - 在表格单元格内的两个图像之间放置一个 Div
- android - 如果在viewmodel中做简单的操作,我们需要livedata吗
- java - 如何从数组中删除重复元素
- javascript - jQuery函数在第一次点击后停止工作
- java - Spring JPA Hibernate 自动填充审计字段(创建 ID/时间戳等)