Files
training.python.datascience/source/plotting/charts/plotly_regression_curve.py
2025-11-15 18:20:01 +01:00

31 lines
1.1 KiB
Python

import pandas as pd
import numpy as np
from plotly.graph_objs import Figure, Scatter
from scipy.ndimage import gaussian_filter
# Créer une courbe aléatoire lissée de valeurs entre 1 et 80
SIZE: int = 500
np.random.seed(4) # Pour reproduire les résultats
df = pd.DataFrame(data={"col1": gaussian_filter(np.random.random(size=SIZE) * 79.0 + 1.0, sigma=SIZE / 133)})
# Calculer les coefficients d'une régression de degré 2 pour col1
c2, c1, c0 = np.polyfit(df.index, df["col1"], 2)
df["reg1"] = c2 * df.index**2 + c1 * df.index + c0
# Créer une image avec deux lignes
figure = Figure(
data=[
Scatter(name="values", x=df.index, y=df["col1"], line={"color": "gray", "width": 1}, mode="lines"),
Scatter(
name="regression", x=df.index, y=df["reg1"], line={"color": "red", "width": 4, "dash": "dot"}, mode="lines"
),
],
layout={
"template": "seaborn",
"xaxis1": {"title": "time"},
"title": "Test de régression",
"font": {"family": "Cabin", "size": 13},
"yaxis1": {"title": "value"},
},
)
figure.show(renderer="browser")