如何在不知道 x 位置的情况下将一维列表绘制到现有图上?
How can I plot 1-d list onto existing plot without knowing x locations?
我正在用 datetime
和 power
绘制图表,可以在所附图片的大蓝线中看到。
绘制图像后,我试图在同一图中插入一维列表 (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
如果列表包含 y 值 (power
)
通常我们可以找到与 isin
, but since these are floats, we should use np.isclose
匹配的 y 值以检查公差。
使用np.isclose
with array 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
然后散点图这些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
)
我们可以只使用 isin
来查找匹配的 datetime
行:
matches = df[df['datetime'].isin(only_unusual)]
然后将这些 matches
(而不是 df
)用于散点图:
ax.scatter(pd.to_datetime(matches['datetime']), matches['power'], color='r', label='Unusual', marker='o')
# ^^^^^^^ ^^^^^^^
我正在用 datetime
和 power
绘制图表,可以在所附图片的大蓝线中看到。
绘制图像后,我试图在同一图中插入一维列表 (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
如果列表包含 y 值 (power
)
通常我们可以找到与 isin
, but since these are floats, we should use np.isclose
匹配的 y 值以检查公差。
使用
np.isclose
with array 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
然后散点图这些
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
)
我们可以只使用
isin
来查找匹配的datetime
行:matches = df[df['datetime'].isin(only_unusual)]
然后将这些
matches
(而不是df
)用于散点图:ax.scatter(pd.to_datetime(matches['datetime']), matches['power'], color='r', label='Unusual', marker='o') # ^^^^^^^ ^^^^^^^