1

I am new in using python.
I am trying to graph 2 variables in Y1 and Y2 (secondary y axis) , and the date in the x axis from a csv file.

I think my main problem is with converting the date in csv.

Moreover is it possible to save the 3 graphs according to the ID (A,B,C)... Thanks a lot.

I added the CSV file that I have and an image of the figure that i am looking for.

Thanks a lot for your advice

ID  date    Y1  Y2 
A   40480   136 83
A   41234   173 23
A   41395   180 29
A   41458   124 60
A   41861   158 27
A   42441   152 26
A   43009   155 51
A   43198   154 38
B   40409   185 71
B   40612   156 36
B   40628   165 39
B   40989   139 77
B   41346   138 20
B   41558   132 85
B   41872   157 58
B   41992   120 91
B   42245   139 43
B   42397   131 34
B   42745   114 68
C   40711   110 68
C   40837   156 38
C   40946   110 63
C   41186   161 46
C   41243   187 20
C   41494   122 55
C   41970   103 19
C   42183   148 78
C   42247   115 33
C   42435   132 92
C   42720   187 43
C   43228   127 28
C   43426   183 45

graph expected

PilouPili
  • 2,601
  • 2
  • 17
  • 31
Mhmd Qadi
  • 47
  • 1
  • 8
  • could you share some more information? – nilay jha Oct 28 '19 at 15:14
  • Thanks for answering , i am trying to plot a graph for 2 values y1 and y2 , together with the date .. i put csv value above to make it clear and i also attached an example graph ,, let me know if it now clear – Mhmd Qadi Oct 28 '19 at 15:17
  • Have you tried anything? First off, you need to read the csv file https://stackoverflow.com/questions/41585078/how-do-i-read-and-write-csv-files-with-python?noredirect=1&lq=1 then you can use matplotlib to plot the contents https://stackoverflow.com/questions/6819653/plotting-points-in-python – usernumber Oct 28 '19 at 15:22
  • i can read the CSV file. put i cant plot with date ... i know that sounds stupid.. but i am very new in python world.. Thanks – Mhmd Qadi Oct 28 '19 at 15:29

2 Answers2

1

Try the matplotlib library, if i understood right, it should work.

from mpl_toolkits import mplot3d
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()

ax = plt.axes(projection='3d')

Data for a three-dimensional line

zaxis = y1
xaxis = date
yaxis = y2
ax.plot3D(xaxis, yaxis, zaxis, 'red')

Data for three-dimensional scattered points

zdat = y1
xdat = date
ydat = y2
ax.scatter3D(xdat, ydat, zdat, c=xdat, cmap='Greens')
DeshDeep Singh
  • 1,817
  • 2
  • 23
  • 43
1

If I understand you correctly, you are looking for three separate graphs for ID=A, ID=B, ID=C. Here is how you could get that:

import pandas as pd
import pylab as plt

data = pd.read_csv('data.dat', sep='\t')  # read your datafile, you might have a different name here

for i, (label, subset) in enumerate(data.groupby('ID')):
    plt.subplot(131+i)
    plt.plot(subset['date'], subset['Y1'])
    plt.plot(subset['date'], subset['Y2'], 'o')
    plt.title('ID: {}'.format(label))
plt.show()

Note that this treats your dates as integers (same as in the datafile).

Ingo
  • 1,103
  • 8
  • 17
  • Hi thanks for the answer , but i have the error for i, (label, subset) in enumerate(d.groupby('ID')): NameError: name 'd' is not defined – Mhmd Qadi Oct 28 '19 at 15:55
  • Sorry, that was a typo. I fixed it in my answer. – Ingo Oct 28 '19 at 17:06
  • sorry for the late response , i was trying to figure the error out, but i couldnt , i sill have this error raise KeyError(gpr) – Mhmd Qadi Nov 11 '19 at 17:11
  • You get a `KeyError`? How do the column names of your data frame look like? I suspect that one of the column names ('ID', 'date', 'Y1', 'Y2') is not there or is incorrectly spelled. For example, it might be that you have a column that pandas parsed as `'ID '`. Note the additional space? That's different from `'ID'`. – Ingo Nov 15 '19 at 15:43
  • i am sorry for that , could it be that the .dat and my file is .csv, but i change it and still cant group with ID – Mhmd Qadi Nov 17 '19 at 22:25
  • What I used as input file, was simply the data from the example you have in your question. That appeared to be tab-separated data. You could also read the data will a regexp separator like so: `data = pd.read_csv('data.dat', sep='\s*', engine='python')`. If you want to check what column names were found in your data, you can use `data.columns` – Ingo Nov 17 '19 at 22:52