2

so I've been fiddling around with Plotly and can't seem to figure out how to combine all results of my linear functions into one graph in Plotly. For my dataset, I have an example where the Independent Variable is called the IV and the dependent variables are the Letters. Below is an example of a dataset. DataSet Example

Now I've worked out how to fit the linear model using Sklearn for each dependent variable, but it only shows up in individual graphs.

#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()

Giving me these results : Results of linear functions of letters

I combined all the regression fits into one graph in Matplotlib giving me the desired results here: Combined results of linear functions

You might ask, why Plotly if you already have it on Matplotlib? Well, in Plotly, I know I can untick and tick which data I would like to show up on the graph which is useful in comparing maybe the gradient of certain Letters. Hopefully someone can lend me a hand. Thank you!

-EDIT So I attempted to combine the linear functions together with the code below. However, instead of the results being on top of each other with the same origin. It instead joined after the end of each result showing this graph which is not what I want.

#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()

acosta1556
  • 53
  • 4
  • You can add it by using add_trace(go.Scatter()) in the loop after the first graph drawing setting. – r-beginners Jun 26 '21 at 13:57
  • like this? `fig = go.Figure([` `go.Scatter(name = column_names[i+1], x=Data0['IV'], ` `y=Data0[column_names[i+1]], mode='markers'),` `add_trace(go.Scatter(name='Regression Fit', x=x_range, y=y_range, ``mode='lines'))` `])` `fig.show()` When I run it, it says add_trace is not defined – acosta1556 Jun 26 '21 at 14:12
  • I find [this answer](https://stackoverflow.com/questions/51637838/how-to-annotate-subplots-in-plotly-inside-a-for-loop) to be very helpful – r-beginners Jun 26 '21 at 14:20
  • When I run the code from the answerer, his output data are in individual graphs. I want it so that all the results are in one graph, just like the last image in my initial post on Matplotlib – acosta1556 Jun 26 '21 at 14:35
  • It was a lack of confirmation. I am sorry, but please refer to the following [answer](https://stackoverflow.com/questions/60926439/plotly-add-traces-using-a-loop). – r-beginners Jun 26 '21 at 14:53
  • Hi there, I've looked at the answer and it does make sense thank you! However, when I attempt to do it myself, the results join at the end of each result instead of being on top of each other. I've edited my initial post showing what I added. – acosta1556 Jun 26 '21 at 15:55

1 Answers1

1

The problem is that you are resetting the figure with fig=go.Figure()

Moving that line outside the loop should fix your problem.

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()
Derek O
  • 16,770
  • 4
  • 24
  • 43
  • 1
    This solved my problem. Thank you soo much! I can't believe I only needed to take out `fig=go.Figure()` I just couldn't get my head around it aha – acosta1556 Jun 26 '21 at 17:29
  • No problem - Plotly does take some getting used to for sure! – Derek O Jun 26 '21 at 18:17