gpt4 book ai didi

python - Matplot : indicate with dots on the graph if conditions are met

转载 作者:行者123 更新时间:2023-12-01 01:11:01 31 4
gpt4 key购买 nike

我的数据框如下所示:

                     time   price   direction   event   High_cross  movement_up triggered_up    
26 2019-01-01 11:37:57.434 0.00224242 up crossing up True False
27 2019-01-01 11:37:57.784 0.00224243 up 0 False False
28 2019-01-01 11:38:38.575 0.00224171 up 0 False False
29 2019-01-01 11:38:57.602 0.00224311 up 0 False False
30 2019-01-01 11:39:21.907 0.00224473 up 0 True movement
31 2019-01-01 11:39:40.320 0.00224422 up 0 True True
32 2019-01-01 11:40:16.966 0.00224697 up 0 True True
33 2019-01-01 11:41:16.966 0.00224694 down 0 True Movement
34 2019-01-01 11:42:16.966 0.00224674 down 0 false false
35 2019-01-01 11:43:16.966 0.00224672 down crossing down false false Yes

您好,我需要的是当满足某些条件时在图表上用点指示特定值。现在我只绘制了价格与时间的关系图:

df['价格'] = df.price.astype(float)
df.plot(y='价格', x='时间')

我现在需要的是当列High_cross设置为True并且在同一行上列movement_up等于movement 在价格处加一个点。当event列等于crossing down并且triggered upYes时执行相同的操作。最后,当eventcrossing down并且triggered up等于No时,情况相同......我看了文档中有一些内容,但我无法找到有关如何使用多个条件的解释...有任何帮助吗?谢谢!

这是我试图找到符合我的条件的行:

`movement= df[(df.High_cross == True) & (df.movement_up == 'movement')]
price_movement = movement.price

crossing_yes = df[(df.event == 'crossing down') & (df.triggered_up == 'Yes')]
price_crossing_yes = crossing_yes.price

crossing_no = df[(df.event == 'crossing down') & (df.triggered_up == 'No')]
price_crossing_no = crossing_no.price`

但我不知道如何在图表上绘制这些价格..

最佳答案

您可以使用

绘制提取的值
plt.scatter(movement['time'].tolist(), price_movement.tolist(), marker='o')
plt.scatter(crossing_yes['time'].tolist(), price_crossing_yes.tolist(), marker='.')
plt.scatter(crossing_no['time'].tolist(), price_crossing_no.tolist(), marker='+')

movementcrossing_yescrossing_noDataFrame 对象,因此必须指定列。这相当于例如

time_movement = movement.time
plt.scatter(time_movement.tolist(), price_movement.tolist())

但稍微不那么冗长。您当然可以通过执行以下命令进一步压缩内联过滤

plt.scatter(df[(df.High_cross == True)&(df.movement_up == 'movement')]['time'].tolist(), 
df[(df.High_cross == True)&(df.movement_up == 'movement')]['price'].tolist(),
marker='o')
plt.scatter(df[(df.event == 'crossing down')&(df.triggered_up == 'Yes')]['time'].tolist(),
df[(df.event == 'crossing down')&(df.triggered_up == 'Yes')]['price'].tolist(),
marker='.')
plt.scatter(df[(df.event == 'crossing down')&(df.triggered_up == 'No')]['time'].tolist(),
df[(df.event == 'crossing down')&(df.triggered_up == 'No')]['price'].tolist(),
marker='+')

这稍微更简洁,适合循环,但确实需要第二遍,否则强度会降低。

这是使用提供的数据的完整示例

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(
[['2019-01-01 11:37:57.434', 0.00224242, 'up', 'crossing up', True, False],
['2019-01-01 11:37:57.784', 0.00224243, 'up', 0, False, False],
['2019-01-01 11:38:38.575', 0.00224171, 'up', 0, False, False],
['2019-01-01 11:38:57.602', 0.00224311, 'up', 0, False, False],
['2019-01-01 11:39:21.907', 0.00224473, 'up', 0, True, 'movement'],
['2019-01-01 11:39:40.320', 0.00224422, 'up', 0, True, True],
['2019-01-01 11:40:16.966', 0.00224697, 'up', 0, True, True],
['2019-01-01 11:41:16.966', 0.00224694, 'down', 0, True, 'Movement'],
['2019-01-01 11:42:16.966', 0.00224674, 'down', 0, False, False],
['2019-01-01 11:43:16.966', 0.00224672, 'down', 'crossing down',
False, False, 'Yes']])

df.columns = ['time', 'price', 'direction', 'event', 'High_cross',
'movement_up', 'triggered_up']

df['price'] = df.price.astype(float)
df['time'] = pd.to_datetime(df['time'])
df.plot(y='price', x='time', legend=False)
plt.scatter(df[(df.High_cross == True)&(df.movement_up == 'movement')]['time'].tolist(),
df[(df.High_cross == True)&(df.movement_up == 'movement')]['price'].tolist(),
marker='o')
plt.scatter(df[(df.event == 'crossing down')&(df.triggered_up == 'Yes')]['time'].tolist(),
df[(df.event == 'crossing down')&(df.triggered_up == 'Yes')]['price'].tolist(),
marker='.')
plt.scatter(df[(df.event == 'crossing down')&(df.triggered_up == 'No')]['time'].tolist(),
df[(df.event == 'crossing down')&(df.triggered_up == 'No')]['price'].tolist(),
marker='+')
plt.ylabel("Price")
plt.xlabel("Time")
plt.show()

Price over time

关于python - Matplot : indicate with dots on the graph if conditions are met,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54870746/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com