python-3.x - 如何添加整个数据框行作为散点图注释

标签 python-3.x pandas matplotlib mplcursors plot-annotations

我正在散点图上绘制 Pandas DataFrame 的两列,我希望每个点显示 DataFrame 的所有行值。我看过this post ,并尝试用 mplcursors 做类似的事情:

import pandas as pd
from datetime import date, datetime, time, timedelta
import numpy as np
import matplotlib.pyplot as plt
from mplcursors import cursor

df = pd.DataFrame()
df['datetime'] = pd.date_range(start='2016-01-01', end='2016-01-14', freq='30T')
#df = df.set_index('datetime')
df['x1'] = np.random.randint(-30, 30, size=len(df))
df['x2'] = np.random.randint(-30, 20, size=len(df))
df['x3'] = np.random.randint(-20, 30, size=len(df))
df['y1'] = np.random.randint(-100, 100, size=len(df))
df['y2'] = np.random.randint(-300, 200, size=len(df))
df['y3'] = np.random.randint(-200, 300, size=len(df))

def conditions(s):
    if (s['y1'] > 20) or (s['y3'] < 0):
        return 'group1'
    elif (s['x3'] < 20):
        return 'group2'
    elif (s['x2'] == 0):
        return 'group3'
    else:
        return 'group4'

df['category'] = df.apply(conditions, axis=1)

fig = plt.figure(figsize=(12,4))

ax1 = plt.subplot(121)
ax1.scatter(df.x1, df.y1, label='test1')
ax1.scatter(df.x2, df.y2, label='test2')
#cursor(hover=True)
ax1.set_xlabel('test1')
ax1.set_ylabel('test2')
ax1.legend(['test1','test2'])
cr1 = cursor(ax1,hover=True)
#ax1.annotation_names = df.columns.tolist()
cr1.connect("add", lambda x: x.annotation.set_text(df.columns.tolist()[x.target.index]))

ax2 = plt.subplot(122)
ax2.scatter(df.x1, df.y1, label='test1')
ax2.scatter(df.x3, df.y3, label='test3')
ax2.set_xlabel('test1')
ax2.set_ylabel('test3')
ax2.legend(['test1','test3'])
cr2 = cursor(ax2,hover=True)
#ax2.annotation_names = df.columns.tolist()
cr2.connect("add", lambda x: x.annotation.set_text(df.columns.tolist()[x.target.index]))

# save figure
import pickle
pickle.dump(fig, open('FigureObject.fig.pickle', 'wb'))
plt.show()

当我将鼠标悬停在某个点上时,我想看到一个包含(例如)的标签:

datetime = 2016-01-01 00:00:00 
x1 = 1 
x2 = -4 
x3 = 22 
y1 = -42 
y2 = -219 
y3 = -158    
category = group1

但我收到这种类型的错误:

cr2.connect("add", lambda x: x.annotation.set_text(df.columns.tolist()[x.target.index]))
IndexError: list index out of range

如何修复它?

最佳答案

  • 由于df.columns.tolist()[x.target.index]而发生IndexError
    • df.columns.tolist() 是一个包含 7 列的列表,然后通过 [x.target.index] 建立索引。
  • df.iloc[x.target.index, :].to_dict() 将以 dict 形式获取该点所需的行数据
    • 列表理解为每个对创建一个字符串列表
    • '\n'.join(...) 创建一个字符串,每列由 \n 分隔
  • mplcursors v0.5.1 中,Selection.target.index 已弃用,请改用 Selection.index
    • df.iloc[x.index, :] 而不是 df.iloc[x.target.index, :]
cr1.connect("add", lambda x: x.annotation.set_text('\n'.join([f'{k}: {v}' for k, v in df.iloc[x.index, :].to_dict().items()])))

enter image description here

  • 或者,使用 .to_string()
cr1.connect("add", lambda x: x.annotation.set_text(df.iloc[x.index, :].to_string()))

enter image description here

关于python-3.x - 如何添加整个数据框行作为散点图注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68796153/

相关文章:

Python 从 XML 中提取的内容保持相同的架构

python - 在给定日期时间之前查找列表中的最新日期时间

python - 将字符串列从 DataFrame 转换为 float for .sum()

python - 从 (row,col,values) 的元组列表构造 pandas DataFrame

python - 如何在Python ftplib中检查FTP登录是否成功?

python - 带参数调用程序

python - 如何修复pygame中的阿拉伯语/波斯语文本和字体?

python - 如何在 Python 中对 Matplotlib 进行线程化?

python - 使用不带 griddata() 的一维数组的二维绘图

python - Matplotlib masking - 根据像素当前的颜色值重置像素的 zorder?