-1

I have a large file with data stored in columns like that:

Pression Volume Temperature
2 3 6
4 2 8
5 3 15

I would like to plot together the values from different columns to compare them with the values from an other given column. For example, the pression and the volume in y-axis and the temperature in x-axis.

In my example, it could give something like that:

Where the pression is in blue, the volume in red and the temperature in x-axis.

How can I do it ?

Thank you

EDIT

The values are space separated and the file is a .dat file

I can't get each value by hand, my real file is quite large for it

Julien
  • 763
  • 8
  • 32

1 Answers1

1

Here it is:

df = pd.DataFrame({'Pressure': {0: 2, 1: 4, 2: 5}, 'Volume': {0: 3, 1: 2, 2: 3}, 'Temperature': {0: 6, 1: 8, 2: 15}})

df.plot(x= 'Temperature', y=['Pressure', 'Volume'], marker='o')
plt.show()

enter image description here

In response to you comment, if you have a csv file called 'sample.csv' that looks like this:

Pressure,Volume,Temperature
2,3,6
4,2,8
5,3,15

You can create the dataframe and plot it as shown below:

df = pd.read_csv('sample.csv')
df.plot(x= 'Temperature', y=['Pressure', 'Volume'], marker='o')
plt.show()
pakpe
  • 5,391
  • 2
  • 8
  • 23
  • Thank you for this answer but it is not what I am looking for, because you got by hand the values. I would like to get all the values from a column in a list for example with a while loop or something better I hope then plot them – Julien May 13 '21 at 21:41
  • You don't need a for loop. Provide a sample of your data (e.g. in a list or csv file) and I'll show you how to do it. – pakpe May 13 '21 at 21:43
  • 1
    @Julien See my amended answer about how to turn your csv data file into a dataframe and the plot it. – pakpe May 13 '21 at 21:48
  • It returns " KeyError: 'pression' " I checked the spelling. Maybe it comes from the read_csv: I have a file.dat and I can't get a csv... – Julien May 13 '21 at 22:00
  • You need to convert your dat file to csv. Check this out: https://anyconv.com/dat-to-csv-converter/ – pakpe May 13 '21 at 22:03
  • I can't convert it to a csv file, there is an error. I tried to take an other csv file but I still have the same key error with the csv file... – Julien May 13 '21 at 22:16
  • Notice that in my code, I have called it 'Pressure' not 'pression.' – pakpe May 13 '21 at 22:22
  • Yes, I noticed it, and I took the same column name (copy past), after I tried to add a space but still nothing – Julien May 13 '21 at 22:25
  • Also, I tried to show df and I have the table, so the data are imported in a good way – Julien May 13 '21 at 22:29
  • Print your df and post a screen shot of its first few rows. – pakpe May 13 '21 at 22:32
  • Ok, I fixed this error by doing: df = pd.read_csv(data, sep=' ') – Julien May 13 '21 at 22:37
  • I think it works, I have a graphic. Thank you. – Julien May 13 '21 at 22:41
  • 1
    No problem. That was a lot of work. Make sure you accept the answer, not just upvote it. – pakpe May 13 '21 at 22:42