gpt4 book ai didi

python-3.x - Plotly 散点图趋势线出现在散点下方。如何让趋势线出现在散点图上? [Python]

转载 作者:行者123 更新时间:2023-12-04 07:50:34 27 4
gpt4 key购买 nike

我正在尝试为 Plotly 散点图绘制趋势线,但我不知道如何让趋势线出现在散点图上。下面是我使用的代码:

import plotly.express as px 
fig = px.scatter(df, x='Percentage_LIFT', y='Average_Daily_Contacts',
title='Percent on LIFT vs Average Daily Contacts',
trendline = "ols", trendline_color_override="red")
fig.show()

Here is the scatterplot the code produced:

enter image description here

如何让趋势线出现在散点图上?

最佳答案

我在使用 px.scatter 时没有看到相同的行为。但只要您只有一条显示散点的轨迹和一条显示趋势线的轨迹,您就可以使用以下命令来切换数据在图形对象中的显示顺序:

fig.data = fig.data[::-1]

这将变成:

enter image description here

进入这个:

enter image description here

完整代码:

# imports
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],
template = 'plotly_dark',
trendline = 'ols',
title = "fig.update_traces(mode = 'lines')",
trendline_color_override='red')

fig.data = fig.data[::-1]
fig.show()

关于python-3.x - Plotly 散点图趋势线出现在散点下方。如何让趋势线出现在散点图上? [Python],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67005348/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com