【问题标题】:How can I plot 1-d list onto existing plot without knowing x locations?如何在不知道 x 位置的情况下将一维列表绘制到现有图上?
【发布时间】:2022-01-18 09:09:15
【问题描述】:

我正在用datetimepower 绘制一个图表,可以在附图中的蓝色大线中看到。

绘制图像后,我尝试在同一图中插入一维列表 (only_unusual)。

实际上df['power'] 包括一维列表(only_unusual)的值。

但如果不将 y 轴与 only_unusual 一起添加,我无法绘制图表。

如何在同一个图中绘制only_unusual 列表,以便它在主蓝线上以不同的颜色显示?

我的代码:

fig, ax = plt.subplots(figsize=(16,12))
ax.plot(pd.to_datetime(df['datetime']), df['power'], color='b',label='Normal')
ax.scatter(only_unusual ,  color='red', label='Unusual',marker='o')
ax.xaxis_date()
plt.xlabel('Date Time')
plt.ylabel('power')
plt.legend()
fig.autofmt_xdate()
plt.show()

我的数据:

only_unusual : [13.266, 4.213291, 2.756, 3.6722, 12.356, 12.193, 10.318, 12.203, 8.7549, 9.536, 9.10677, 1.417]




df :

        datetime              invno  power
0       2021-12-01 00:00:00    1     0.000
5       2021-12-01 01:00:00    1     0.000
10      2021-12-01 02:00:00    1     0.000
15      2021-12-01 03:00:00    1     0.000
20      2021-12-01 04:00:00    1     0.000
....     ...... ...... ....    ..     .....

1129    2021-12-10 09:00:00    5    2.914
1134    2021-12-10 10:00:00    5    10.318

...     ... ... ...
1149    2021-12-10 13:00:00    5     2.756
1154    2021-12-10 14:00:00    5     1.297
1159    2021-12-10 15:00:00    5     1.503
1164    2021-12-10 16:00:00   5      1.417
1169    2021-12-10 17:00:00    5     0.084
1170 rows × 3 columns

【问题讨论】:

  • 你的意思是像ax.scatter(pd.to_datetime(df['datetime']), only_unusual, color='red', label='Unusual', marker='o')
  • 是的,当我使用这一行时,它会给出像 x 一样的错误,并且 y 必须是相同的大小。任何想法摆脱这个错误?我想我需要匹配 datetime idx 和 only_unusual idx。
  • 所以看来only_unusual 只是一个子集。我想这就是这句话的意思:“df['power']包括only_unusual的值”
  • df['datetime]的总长度为1170,通常only_unusual的len为6或7。需要什么样的信息?我会尽量在这里提供。在意识到不同的大小后,我来这里寻求帮助。

标签: python python-3.x matplotlib plot seaborn


【解决方案1】:

如果列表包含 y 值 (power)

通常我们可以找到与 isin 匹配的 y 值,但由于这些是浮点数,我们应该使用 np.isclose 来检查容差。

  1. 使用np.isclosearray broadcasting 找到具有一定容差的matches

    matches = df[np.isclose(df['power'].values[:, None], only_unusual).any(axis=1)]
    
    #                  datetime  invno   power
    # 1134  2021-12-10 10:00:00      5  10.318
    # 1149  2021-12-10 13:00:00      5   2.756
    # 1164  2021-12-10 16:00:00      5   1.417
    
  2. 然后散点图这些matches:

    ax.scatter(pd.to_datetime(matches['datetime']), matches['power'], color='r', label='Unusual', marker='o')
    #                         ^^^^^^^               ^^^^^^^
    

完整代码(pandas 1.3.3、numpy 1.20.3、matplotlib 1.3.5):

df = pd.DataFrame({'datetime': {0: '2021-12-01 00:00:00', 5: '2021-12-01 01:00:00', 10: '2021-12-01 02:00:00', 15: '2021-12-01 03:00:00', 20: '2021-12-01 04:00:00', 1129: '2021-12-10 09:00:00', 1134: '2021-12-10 10:00:00', 1149: '2021-12-10 13:00:00', 1154: '2021-12-10 14:00:00', 1159: '2021-12-10 15:00:00', 1164: '2021-12-10 16:00:00', 1169: '2021-12-10 17:00:00'}, 'invno': {0: 1, 5: 1, 10: 1, 15: 1, 20: 1, 1129: 5, 1134: 5, 1149: 5, 1154: 5, 1159: 5, 1164: 5, 1169: 5}, 'power': {0: 0.0, 5: 0.0, 10: 0.0, 15: 0.0, 20: 0.0, 1129: 2.914, 1134: 10.318, 1149: 2.756, 1154: 1.297, 1159: 1.503, 1164: 1.417, 1169: 0.084}})
only_unusual = [13.266, 4.213291, 2.756, 3.6722, 12.356, 12.193, 10.318, 12.203, 8.7549, 9.536, 9.10677, 1.417]

matches = df[np.isclose(df['power'].values[:, None], only_unusual).any(axis=1)]

fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(pd.to_datetime(df['datetime']), df['power'], color='b', label='Normal')
ax.scatter(pd.to_datetime(matches['datetime']), matches['power'], color='r', label='Unusual', marker='o')
ax.xaxis_date()
plt.xlabel('Date Time')
plt.ylabel('Power')
plt.legend()
fig.autofmt_xdate()


如果列表包含 x 值 (datetime)

  1. 我们可以只使用isin 来查找匹配的datetime 行:

    matches = df[df['datetime'].isin(only_unusual)]
    
  2. 然后使用这些matches(而不是df)作为散点图:

    ax.scatter(pd.to_datetime(matches['datetime']), matches['power'], color='r', label='Unusual', marker='o')
    #                         ^^^^^^^               ^^^^^^^
    

【讨论】:

  • 非常感谢您的时间和帮助。列表包含 y 值。当我尝试与 y 值匹配时,匹配项正在提取所有日期时间(不仅是必需的)。不是应该提取对应的日期时间吗?
  • 您可以使用dfonly_unusual 的样本来编辑您的帖子吗?
  • 好的,让我添加我的数据。
  • 你好。非常感谢。会投票给你的答案。这对我帮助很大。如果 only_unusual 中有 0,那么这会给我带来问题,因为 df['power'] 有很多 0 单元格。
  • 是的,它奏效了。除了 0 的情况。如果有 0,那么我必须在获得 only_unusual 之前应用另一个逻辑来进行匹配。
猜你喜欢
  • 2023-03-10
  • 2021-12-16
  • 1970-01-01
  • 2011-11-09
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多