Search code examples
pythongraphing

I've printed temperature values in one column in a file but want to graph temperature by row


I want the x-axis to be what row the temperature value is in and the y-axis to be the actual value. How do I do this? I printed values to a file into one column.


Solution

  • import matplotlib.pyplot as plt
    
    # get data
    with open("data.txt") as myfile:
        temps = [float(row) for row in myfile]
    
    # generate line numbers
    num_rows = len(temps)
    rownums = list(range(num_rows))   # starting at 0!
    
    # plot
    plt.plot(rownums, temps)
    plt.show()
    

    which gives

    enter image description here