Shreya
Shreya

Reputation: 151

Groupby and plot

I have a dataset with multiple rows for a same date. For example:

date        col1    col2
1854-11-09  314.2   567.9
1854-11-17  322.8   678.9
1854-11-17  432.8   435.0
1854-11-21  678.9   476.2
1854-11-21  872.7   892.0
1854-11-21  656.9   490.2
1854-11-21  278.9   586.8

I want to plot graphs for individual dates for the last two columns. For example: there will a x-y graph with one point for the 1st date. For the 2nd date there will be 2 points (2 x-axis data, 2 y-axis data), for the 3rd date 4 data points and so on.

I tried groupby but it is not working.

I am also confused about the best way to potray such result because there are two many dates, any suggestion on that front is also welcome.

Upvotes: 1

Views: 72

Answers (1)

Aditya Bhattacharya
Aditya Bhattacharya

Reputation: 1014

The following solution might work for you.

Convert your data into a dataframe using pandas, this is how it should look:

    date          x      y
0   1854-11-09  314.2   567.9
1   1854-11-17  322.8   678.9
2   1854-11-17  432.8   435.0
3   1854-11-21  678.9   476.2
4   1854-11-21  872.7   892.0

Then use groupby by 'date' and for each group values use matplotlib's scatter plot to plot the x-y coordinates or points.

for group, values in data.groupby(data['date']):
        plt.scatter(values['x'], values['y'], c ='r')
        plt.show()

Finally, this is how the plot will look like:

enter image description here

Upvotes: 1

Related Questions