【问题标题】:Adding a 2nd order polynomial trend line px.scatter添加二阶多项式趋势线 px.scatter
【发布时间】:2022-10-24 03:10:26
【问题描述】:
我有一个数据(数据框中有很多行)并且能够生成线性趋势线。
我还想在图中添加二阶多项式趋势线。
如何才能做到这一点?
谢谢
这是仅在一行上的代码:
import pandas as pd, numpy as np
import plotly.express as px
import statsmodels
import nbformat
x = [1,2,3,4,5,6]
y = [-2875, -2976, -9346, -15533, -18393, -20615]
fig = px.scatter(x=x, y=y, trendline="ols")
fig.show()
这是代码的情节
【问题讨论】:
标签:
python
plot
scatter-plot
polynomials
trendline
【解决方案1】:
pd.options.display.float_format = '{:,.3f}'.format
plt.plot(x, y , 'go')
plt.plot(x, p(x),'r')
plt.plot(x,p2(x))
for x1,y1 in zip(x,y):
label = '{:,.3f}'.format(y1)
plt.annotate(label, # this is the text
(x1,y1), # these are the coordinates to position the label
textcoords="offset points", # how to position the text
xytext=(1,4), # distance from text to points (x,y)
ha='center') # horizontal alignment can be left, right or center
plt.show()