是否可以用Plotly折线图绘制趋势线?
figure = px.line(x=year_week, y=num_accidents,
labels=dict(time_from_db="Time",
num_accidents="Num of Accidents"),
title="Number of Accidents Per Week", line_shape='spline',
trendline="ols")
除非我删除趋势线="0",否则上述代码不起作用;旧的";。
您可以使用px.scatter
而不是px.line
;
你只需要更新你的图形对象并将模式更改为线条,如:
fig.update_traces(mode = 'lines')
全代码
import pandas as pd
import plotly.express as px
# data
df = px.data.stocks()[['GOOG', 'AAPL']]
# your choices
target = 'GOOG'
# plotly
fig = px.scatter(df,
x=target,
y=[c for c in df.columns if c != target],
trendline='ols',
title="trendline example")
fig.update_traces(mode='lines')
fig.data[-1].line.color='green'
fig
我希望它能解决你的问题。
问候,
Leonardo