我想将我的折线图和散点图相互结合(散点图上的折线图)。
这是我试过的:
def regression_scatterplot(productname):
data_aggr_period_prod_loop = data_aggr_period_prod.loc[data_aggr_period_prod['product'] == productname].sort_values('period')
data_aggr_period_prod_loop["period_index"] = np.array([date_map[ix] for ix in data_aggr_period_prod_loop['period']])
trace1 = px.scatter(data_aggr_period_prod_loop, x = "period_index", y = "npi")
regression_results_filtered = regression_results.loc[regression_results['Product'] == productname]
x = data_aggr_period_prod_loop["period_index"]
y = regression_results_filtered['Slope'][0]*x+regression_results_filtered['Intercept'][0]
trace2 = px.line(regression_results_filtered, x, y)
fig = make_subplots
fig.add_trace(trace1)
fig.add_trace(trace2)
iplot(fig)
然后当我尝试这样做时,我得到了这个错误代码:
AttributeError: 'function' object has no attribute 'add_trace'
有人能告诉我这两个图是如何成功叠加的吗?
您的数字无法重现,因为我们没有您拥有的数据。但是,我建议按以下方式构建您的图形的创建:
import plotly.graph_objects as go
def regression_scatterplot(productname):
data_aggr_period_prod_loop["period_index"] = np.array([date_map[ix] for ix in data_aggr_period_prod_loop['period']])
trace1 = go.Scatter(x=data_aggr_period_prod_loop["period_index"], y=data_aggr_period_prod_loop["npi"], mode='markers')
regression_results_filtered = regression_results.loc[regression_results['Product'] == productname]
x = data_aggr_period_prod_loop["period_index"]
y = regression_results_filtered['Slope'][0]*x+regression_results_filtered['Intercept'][0]
trace2 = go.Scatter(x=x, y=y, mode='lines')
fig = go.Figure()
fig.add_trace(trace1)
fig.add_trace(trace2)