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

4
  • could you share some more information? Commented Oct 28, 2019 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 Commented Oct 28, 2019 at 15:17
  • Have you tried anything? First off, you need to read the csv file stackoverflow.com/questions/41585078/… then you can use matplotlib to plot the contents stackoverflow.com/questions/6819653/plotting-points-in-python Commented Oct 28, 2019 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 Commented Oct 28, 2019 at 15:29

2 Answers 2

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')
Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot but i need it as the following image in 2D not 3D i.sstatic.net/2DzRF.png
I have problem with date
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).

6 Comments

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
Sorry, that was a typo. I fixed it in my answer.
sorry for the late response , i was trying to figure the error out, but i couldnt , i sill have this error raise KeyError(gpr)
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'.
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.