Python绘制Xaxis范围为09:00到15:30之间的时间...更新plotly'布局“” ' x-axis;?

发布于 2025-01-30 10:39:15 字数 2451 浏览 3 评论 0原文

我正在尝试创建一个情节图表,以分析市场时间(09:00至15:30)。我需要帮助整天设置图片的X轴范围。目前,它显示了“ CSV”文件中可用的范围。

但是,在当天开始时,出口图JPG(1920x1080)是难以辨认的,直到至少中午(由于绘制了绘制方式)

当天开始时的“ some_stock.csv”中的数据...

TIME,open,high,low,close,Volume,RSI,rsi_30,rsi_70
2022-05-19 09:15:00,359.65,359.65,359.65,359.65,0,,30,70
2022-05-19 09:18:00,359.45,359.5,358.5,358.5,138161,,30,70
2022-05-19 09:21:00,358.6,358.75,358.45,358.7,96990,,30,70
2022-05-19 09:24:00,358.7,359.75,358.7,359.75,71892,,30,70
2022-05-19 09:27:00,359.75,359.9,359.25,359.9,64233,,30,70
2022-05-19 09:30:00,360,360.25,359.55,360.25,42732,,30,70
2022-05-19 09:33:00,359.6,360.4,359.6,359.75,103614,,30,70
2022-05-19 09:36:00,359.75,359.85,359.4,359.65,48508,,30,70
2022-05-19 09:39:00,359.85,359.85,359.45,359.5,22259,,30,70
2022-05-19 09:42:00,359.5,359.5,359.3,359.4,44802,,30,70

被执行的代码。

import pandas as pd
from datetime import datetime
import plotly.graph_objects as go

## Creating a time range for that day in datetime format...
now_timeeee = datetime.now().strftime('%Y-%m-%d')
string_start_time = now_timeeee+str(" 09:00:00")
start_timeeee = datetime.strptime(string_start_time, '%Y-%m-%d %H:%M:%S')
string_end_time = now_timeeee+str(" 15:30:00")
end_timeeee = datetime.strptime(string_end_time , '%Y-%m-%d %H:%M:%S')

## Graph Plotting...
df = pd.read_csv("some_stock.csv")

layoutt = go.Layout(autosize=False, width=1920, height=1368, title="some_stock")
fig_001 = go.Figure(data=[go.Candlestick(x=df.index, open=df["open"], high=df["high"], low=df["low"], close=df["close"], name="some_stock"), go.Scatter(x=df.index, y=df["RSI"], mode="markers+lines", name="RSI", yaxis="y2")], layout=layoutt).update_layout(yaxis_domain=[0.25, 1], yaxis2={"domain": [0, 0.20]}, xaxis_rangeslider_visible=False, showlegend=True)
fig_001.update_xaxes(dict(range=[start_timeeee, end_timeeee]))
fig_001.add_trace(go.Scatter(x=df.index, y=df["rsi_30"], name = '30 line', yaxis="y2", line=dict(color='darkcyan', width=1)))
fig_001.add_trace(go.Scatter(x=df.index, y=df["rsi_70"], name = '70 line', yaxis="y2", line=dict(color='darkgreen', width=1)))
fig_001.write_image("images/fig_"+"some_stock"+".jpeg")

导出图片如下。

导出图表

在代码中添加该行与X轴有任何不同的区别固定为900小时至1530小时的范围

fig_001.update_xaxes(dict(range=[start_timeeee, end_timeeee]))

I am trying to create a plotly chart for analysis of stocks in market hours (09:00 to 15:30). I need help in setting the x-axis range for the entire day when the picture is being exported. It currently shows the range to be exactly what is available in the 'csv' file.

However, at the start of the day, the exported chart jpg(1920x1080), is illegible until atleast midday(around 11:30am, because of the way it is ploted.)

See below Data, Code and Pic.

DATA in "some_stock.csv" at the start of the day...

TIME,open,high,low,close,Volume,RSI,rsi_30,rsi_70
2022-05-19 09:15:00,359.65,359.65,359.65,359.65,0,,30,70
2022-05-19 09:18:00,359.45,359.5,358.5,358.5,138161,,30,70
2022-05-19 09:21:00,358.6,358.75,358.45,358.7,96990,,30,70
2022-05-19 09:24:00,358.7,359.75,358.7,359.75,71892,,30,70
2022-05-19 09:27:00,359.75,359.9,359.25,359.9,64233,,30,70
2022-05-19 09:30:00,360,360.25,359.55,360.25,42732,,30,70
2022-05-19 09:33:00,359.6,360.4,359.6,359.75,103614,,30,70
2022-05-19 09:36:00,359.75,359.85,359.4,359.65,48508,,30,70
2022-05-19 09:39:00,359.85,359.85,359.45,359.5,22259,,30,70
2022-05-19 09:42:00,359.5,359.5,359.3,359.4,44802,,30,70

Code being Executed..

import pandas as pd
from datetime import datetime
import plotly.graph_objects as go

## Creating a time range for that day in datetime format...
now_timeeee = datetime.now().strftime('%Y-%m-%d')
string_start_time = now_timeeee+str(" 09:00:00")
start_timeeee = datetime.strptime(string_start_time, '%Y-%m-%d %H:%M:%S')
string_end_time = now_timeeee+str(" 15:30:00")
end_timeeee = datetime.strptime(string_end_time , '%Y-%m-%d %H:%M:%S')

## Graph Plotting...
df = pd.read_csv("some_stock.csv")

layoutt = go.Layout(autosize=False, width=1920, height=1368, title="some_stock")
fig_001 = go.Figure(data=[go.Candlestick(x=df.index, open=df["open"], high=df["high"], low=df["low"], close=df["close"], name="some_stock"), go.Scatter(x=df.index, y=df["RSI"], mode="markers+lines", name="RSI", yaxis="y2")], layout=layoutt).update_layout(yaxis_domain=[0.25, 1], yaxis2={"domain": [0, 0.20]}, xaxis_rangeslider_visible=False, showlegend=True)
fig_001.update_xaxes(dict(range=[start_timeeee, end_timeeee]))
fig_001.add_trace(go.Scatter(x=df.index, y=df["rsi_30"], name = '30 line', yaxis="y2", line=dict(color='darkcyan', width=1)))
fig_001.add_trace(go.Scatter(x=df.index, y=df["rsi_70"], name = '70 line', yaxis="y2", line=dict(color='darkgreen', width=1)))
fig_001.write_image("images/fig_"+"some_stock"+".jpeg")

Exported Picture is as below.

Exported Chart

Adding the line in the code doesnt do any difference to the x-axis which needs to be fixed for the range of 900hr to 1530hr

fig_001.update_xaxes(dict(range=[start_timeeee, end_timeeee]))

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

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

发布评论

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

评论(1

梦年海沫深 2025-02-06 10:39:15

更改问题来找到答案

df = pd.read_csv("some_stock.csv")

通过

df = pd.read_csv("ambujacem_3m.csv", index_col=0, parse_dates=True)

是将日期读取为日期,也使其成为数据的索引。因此,它似乎已经解决了问题。

The answer was found by changing

df = pd.read_csv("some_stock.csv")

to

df = pd.read_csv("ambujacem_3m.csv", index_col=0, parse_dates=True)

The problem was to read the dates as datetime and also, making it the index of the data.. Hence it seems to have resolved the issue..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文