Plotly,如何将我的线性函数的所有结果组合在一张图中

Plotly, how to combine all results of my linear function in one graph

所以我一直在摆弄 Plotly,似乎无法弄清楚如何将我的线性函数的所有结果组合到 Plotly 中的一张图中。对于我的数据集,我有一个示例,其中自变量称为 IV,因变量是字母。下面是一个数据集的例子。

现在我已经弄清楚如何使用 Sklearn 为每个因变量拟合线性模型,但它只显示在单个图表中。

#Data processing import
import numpy as np
import pandas as pd
    #import csv
#Visualisation import
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression # Regression linear model
import plotly.express as px
import plotly.graph_objects as go
sns.set()

#===================Main function==============================================
#------------------------------------------------------------------------------
plt.close("all")

#Reading the data-------------------------------------------------------------
filename="test_dataset.xls"

#Reading the input file-------------------------------------------------------
Data0=pd.read_excel (filename)

#Read number of data, number of column and column names-----------------------
number_of_data=Data0.shape[0]
number_of_I_columns=Data0.shape[1]-1 #The first column is for the X-axis=IV, to access, eg. Data0.iloc[0,0]
column_names=Data0.columns #to access, eg. Data0.columns[1]



x = Data0.IV.values.reshape(-1, 1)

# print(x)
#fit least square for each letter data---------------------------------------------
for i in range(number_of_I_columns): #for each letter data
    
    y=Data0[column_names[i+1]].values
    y=y.astype('float64')

    #Least square fitting-----------------------------------------------------
    model = LinearRegression(fit_intercept=True)
    model.fit(x,y)
    


    #Predict the letter with the fitted model----------------------------

    x_range = np.linspace(x.min(), x.max())
    y_range = model.predict(x_range.reshape(-1, 1))


    fig = go.Figure([
            go.Scatter(name = column_names[i+1], x=Data0['IV'], y=Data0[column_names[i+1]], mode='markers'),
            go.Scatter(name='Regression Fit', x=x_range, y=y_range, mode='lines')
    ])
    fig.show()

给我这些结果:

我在 Matplotlib 中将所有回归拟合合并到一张图中,在这里给出了我想要的结果:

您可能会问,如果您已经在 Matplotlib 上安装了 Plotly,为什么还要这样做?好吧,在 Plotly 中,我知道我可以取消勾选并勾选我想在图表上显示的数据,这对于比较某些字母的梯度可能很有用。 希望有人能帮我一把。谢谢!

-编辑 所以我试图将线性函数与下面的代码结合起来。但是,结果不会以相同的来源彼此重叠。相反,它在每个结果结束后加入,显示这张图,这不是我想要的。

#Reading the data-------------------------------------------------------------
filename="test_dataset.xls"

#Reading the input file-------------------------------------------------------
Data0=pd.read_excel (filename)

#Read number of data, number of column and column names-----------------------
number_of_data=Data0.shape[0]
number_of_I_columns=Data0.shape[1]-1 #The first column is for the X-axis=IV, to access, eg. Data0.iloc[0,0]
column_names=Data0.columns #to access, eg. Data0.columns[1]



x = Data0.IV.values.reshape(-1, 1)
ys = []
# print(x)
#fit least square for each letter data---------------------------------------------
for i in range(number_of_I_columns): #for each letter data
    
    y=Data0[column_names[i+1]].values
    y=y.astype('float64')
#     ys.append(y)
    #Least square fitting-----------------------------------------------------
    model = LinearRegression(fit_intercept=True)
    model.fit(x,y)
    


    #Predict the letter with the fitted model----------------------------

    x_range = np.linspace(x.min(), x.max())
    y_range = model.predict(x_range.reshape(-1, 1))
    ys.append(y_range)


###  MY ATTEMPT OF COMBINING LINEAR FUNCTIONS
ys = np.array(ys)


colnames = list(column_names)
for i in range(ys.shape[0]):
#     print(ys[:,i])
    fig = go.Figure()
    fig.add_trace(go.Scatter(x = x[:,0], y=ys[:,i], name= colnames[i+1]))
fig.show()

问题是您正在用 fig=go.Figure()

重置数字

将该行移到循环外应该可以解决您的问题。

fig = go.Figure()
for i in range(ys.shape[0]):    
    fig.add_trace(go.Scatter(x = x[:,0], y=ys[:,i], name= colnames[i+1]))
fig.show()