How to plot data in a csv file with python matplolib

Asked 2 years ago, Updated 2 years ago, 107 views

First of all, regarding the contents of the csv file, it says x1,x2,1,y from the first column.(Actually 101 rows and 4 columns)

"x1", "x2", "1", "y"
-0.626,-0.620,1,0.282
0.183,0.042,1,1.732
-0.835,-0.910,1,-0.293
1.595,0.158,1,2.506
0.329,-0.654,1,0.615

Therefore, I would like to set the horizontal axis to a*x1+b*x2 and the vertical axis to y, but it does not work.How do I extract data one line at a time and multiply and plot the variables a,b?

python python3 matplotlib csv

2022-09-30 15:44

1 Answers

I am not sure about the setting of a and b, but I will answer as a fixed value.Also, the CSV file is aaa.csv.

import numpy as np
    import matplotlib.pyplot asplt
    data=np.loadtxt('aaa.csv', delimiter=',', skiprows=1)
    print("data\n", data)
    a = 1
    b = 2
    x1 = data [:, 0]
    x2 = data [:,1]
    y=data[:,3]
    print(x1,x2,y)
    x = a* x1 + b* x2
    config=plt.figure()
    ax=fig.add_subplot(1,1,1)
    ax.scatter(x,y)
    ax.set_xlabel('x')
    ax.set_ylabel('y')

Like this?


2022-09-30 15:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.