在库存图中绘制特定行的问题

发布于 2025-01-29 11:36:28 字数 561 浏览 2 评论 0原文

我正在使用大熊猫在数据框架中使用DateTime索引绘制库存数据图。我想在df_raw [col_name]!= 0的行中绘制黑色标记/a>。当没有数据帧行满足条件时,图显示fine

您能帮我理解我在做错什么吗?请注意,该股票的数据已有几十年了,但我仅扣除了100万个数据的最后7天。

非常感谢!

fig.add_trace(go.Scatter(
    x=df_raw[df_raw[col_name]!=0].index,
    y=[df_raw['Close'][j] for j in df_raw[df_raw[col_name]!=0].index],
    mode='markers',
    marker=dict(
        size=8,
        color='black',
        symbol='cross'
    ),
    name='Cdl Pattern'
))

I'm using pandas to plot a chart of stock data with datetime index in the dataframe. I'd like to plot black markers for rows where df_raw[col_name]!=0 but it is acting funky. When no dataframe rows meet the condition, the plot displays fine.

Can you help me understand what I'm doing wrong? Note that data for this stock is available for several decades, but I'm pulling only last 7 days of 1m data.

Thanks so much!

fig.add_trace(go.Scatter(
    x=df_raw[df_raw[col_name]!=0].index,
    y=[df_raw['Close'][j] for j in df_raw[df_raw[col_name]!=0].index],
    mode='markers',
    marker=dict(
        size=8,
        color='black',
        symbol='cross'
    ),
    name='Cdl Pattern'
))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昔梦 2025-02-05 11:36:28

我可能已经找到了答案,并希望在此处发布它,以防它帮助他人。这是完整的绘图代码。请注意,它首先绘制“关闭”,然后绘制标记。

绘制“关闭”时,系统使用行号而不是索引,但是在绘制标记时,它明确指示使用索引。由于索引是DateTime,因此两个图最终都以不匹配的X轴。

fig = go.Figure()
fig.add_trace(go.Scatter(
    ###No x-axis provided, the system uses dataframe row numbers###
    y=validation_df['Close'],
    mode='lines+markers',
    name='Original Plot'
))

fig.add_trace(go.Scatter(
    ###Explicitly using .index on x-axis###
    x=df_raw[df_raw[col_name]!=0].index,
    y=[df_raw['Close'][j] for j in df_raw[df_raw[col_name]!=0].index],
    mode='markers',
    marker=dict(
        size=8,
        color='black',
        symbol='cross'
    ),
    name='Cdl Pattern'
))

为了纠正这一点,必须将X轴转换为我使用pd.series的行号。如果您知道的话,请建议一种更好的方法。

fig = go.Figure()
fig.add_trace(go.Scatter(
    y=validation_df['Close'],
    mode='lines+markers',
    name='Original Plot'
))

fig.add_trace(go.Scatter(
    ###using pd.Series to get row numbers for x-axis###
    x=pd.Series(df_raw[col_name].to_list())[pd.Series(df_raw[col_name].to_list())!=0].index,

    y=[df_raw['Close'][j] for j in df_raw[df_raw[col_name]!=0].index],
    
    mode='markers',
    marker=dict(
        size=8,
        color='black',
        symbol='cross'
    ),
    name='Cdl Pattern'
))

I may have found the answer and wanted to post it here in case it helps others. Here's the complete plotting code. Notice that it first plots the 'Close', and then the markers.

When plotting 'Close', the system uses row numbers instead of indices, but when plotting the markers, it is explicitly being directed to use indices. Since indices are Datetime, the two plots end up with mismatched x-axis.

fig = go.Figure()
fig.add_trace(go.Scatter(
    ###No x-axis provided, the system uses dataframe row numbers###
    y=validation_df['Close'],
    mode='lines+markers',
    name='Original Plot'
))

fig.add_trace(go.Scatter(
    ###Explicitly using .index on x-axis###
    x=df_raw[df_raw[col_name]!=0].index,
    y=[df_raw['Close'][j] for j in df_raw[df_raw[col_name]!=0].index],
    mode='markers',
    marker=dict(
        size=8,
        color='black',
        symbol='cross'
    ),
    name='Cdl Pattern'
))

To correct this, the x-axis had to be converted to row numbers for which I used pd.Series. Please suggest a better method if you know one.

fig = go.Figure()
fig.add_trace(go.Scatter(
    y=validation_df['Close'],
    mode='lines+markers',
    name='Original Plot'
))

fig.add_trace(go.Scatter(
    ###using pd.Series to get row numbers for x-axis###
    x=pd.Series(df_raw[col_name].to_list())[pd.Series(df_raw[col_name].to_list())!=0].index,

    y=[df_raw['Close'][j] for j in df_raw[df_raw[col_name]!=0].index],
    
    mode='markers',
    marker=dict(
        size=8,
        color='black',
        symbol='cross'
    ),
    name='Cdl Pattern'
))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文