我需要在图形区域的末尾延长一行,该行由2个DataFrame点和DatetimeIndex组成,如下图所示:
该怎么做?借助数学运算,我可以计算出直线恰好在x轴上的位置,这是不可能的,因为您无法测量1月4日至1月5日之间的距离,因为这是1天。
import pandas as pd
import plotly.graph_objects as go
df = pd.DataFrame({
'score':[30, 60, 38, 10, 50, 70, 40],
'date':['2000-01-01', '2000-01-02', '2000-01-03', '2000-01-04', '2000-01-05', '2000-01-06', '2000-01-07'],
})
df['date'] = pd.to_datetime(df['date'])
fig = go.Figure()
fig.add_shape(
dict(
type="line",
x0=df.iloc[1]['date'],
y0=df.iloc[1]['score'],
x1=df.iloc[2]['date'],
y1=df.iloc[2]['score'],
line=dict(
color="RoyalBlue",
width=3
)
))
fig.add_trace(
go.Scatter(
mode='markers',
x=df['date'],
y=df['score'],
marker=dict(
color='LightSkyBlue',
size=12,
line=dict(
color='MediumPurple',
width=4
)
),
)
)
fig.update_xaxes(tickmode='auto',)
fig.show()
答案 0 :(得分:0)
只要您将分钟数转换成几天,这没什么大不了的。在这里,我已经为您计算了日期
import pandas as pd
import plotly.graph_objects as go
df = pd.DataFrame({
'score':[30, 60, 38, 10, 50, 70, 40],
'date':['2000-01-01', '2000-01-02', '2000-01-03', '2000-01-04', '2000-01-05', '2000-01-06', '2000-01-07'],
})
df['date'] = pd.to_datetime(df['date'])
fig = go.Figure()
fig.add_shape(
dict(
type="line",
x0=df.iloc[1]['date'],
y0=df.iloc[1]['score'],
x1=df.iloc[2]['date'],
y1=df.iloc[2]['score'],
line=dict(
color="RoyalBlue",
width=3
)
))
fig.add_shape(
dict(
type="line",
x0=df.iloc[2]['date'],
y0=df.iloc[2]['score'],
x1=pd.Timestamp(2000,1,4,17,16),
y1=0,
line=dict(
color="red",
dash="dot",
width=3
)
))
fig.add_trace(
go.Scatter(
mode='markers',
x=df['date'],
y=df['score'],
marker=dict(
color='LightSkyBlue',
size=12,
line=dict(
color='MediumPurple',
width=4
)
),
)
)
fig.update_xaxes(tickmode='auto',)
fig.show()