问题描述
有没有什么方法可以像绘制散点图一样轻松地将趋势线添加到绘制表达的折线图?
我尝试使用这一行来创建我的数据框:
fig = px.line(df4,x=df4["DATE"],y=df4['Ratio'],title="Market Ratio",trendline='ols')
但它给出了错误
TypeError: line() got an unexpected keyword argument 'trendline'
谢谢!
解决方法
不,没有。而你不需要它。您只需要 px.scatter
和:
fig.update_traces(mode = 'lines')
如果它did具有px.line
属性,这将产生与trendline
相同的结果。
完整代码:
# imports
import pandas as pd
import plotly.express as px
import plotly.io as pio
# data
df = px.data.stocks()[['GOOG','AAPL']]
# your choices
target = 'GOOG'
colors = px.colors.qualitative.T10
# plotly
fig = px.scatter(df,x = target,y = [c for c in df.columns if c != target],template = 'plotly_dark',color_discrete_sequence = colors,trendline = 'ols',title = "fig.update_traces(mode = 'lines')")
f = fig.full_figure_for_development(warn=False)
fig.update_traces(mode = 'lines')
fig.data[-1].line.color = 'red'
fig.show()