[Python] Plotting from a xlsx file from a mounted drive in Python errors

Discussão em 'Python' iniciado por Stack, Maio 6, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make a python plot of this excel file.

    However, I keep getting errors about values and inf. I want to plot the time (first column) against a couple of different other columns (storage times). The code I have so far is this:

    from google.colab import drive
    drive.mount('/content/drive')


    import matplotlib.pyplot as plt
    import numpy as np
    from sympy import S, symbols, printing
    import scipy.optimize as sci
    import pandas as pd
    from pathlib import Path
    from scipy import optimize
    import statistics

    #Importing all the relevant packages for the code, plotting, finding the best fit, analyzing
    our data and error analysis
    file=pd.read_excel(r'/content/drive/MyDrive/Measurements storage time 20250605.xlsx')
    x_axis=(file['Time - Plot 0']).astype(str)
    y_axis=(file.iloc[:, 1]).astype(str)
    #Making an array of our calculated Indices of Refraction
    plt.title("Intensity for different storage times") #Giving our plot a relevant title that
    also explains what is being plotted so the graph is stand-alone
    plt.xlabel("Time [ms]") #Giving the x axis a label
    plt.ylabel("Intensity [counts/s]") #Giving the y axis a label
    plt.grid() #Giving the plot a grid.
    def f(x,a,b):
    f=a*x**3+b*x**2+c*x
    return f #Defining our function that is going to be fitted through our data points
    def g(x,a,b): #Definin the theoretical function
    g=a*x+b
    return g
    plt.plot(x_axis,y_axis, color='orange', label= 'measured') #Making a
    scatter plot of our data points with a Label
    fit1, pcov = optimize.curve_fit(f, x_axis, y_axis) #Using scipy making
    a best fit throug the data points
    plt.plot(x_axis, f(x_axis,*fit1), label='Best Fit', color='green')
    #Displaying the equation of best fit line in the plot #Displaying the
    equation of best fit line in the plot
    plt.legend(loc='best') #Showing the labels belonging to both graphs

    Continue reading...

Compartilhe esta Página