Will
Will

Reputation: 737

Python Seaborn Pandas Dataframe plot first few groups

I have a need to plot only the first n number of groups, or plot several plots of n items out of a set of groups from a pandas dataframe. The frame contains columns as

import pandas as pd
import seaborn as sns; sns.set()
import numpy as np
datain = np.loadtxt("data.txt")

df = pd.DataFrame(data = datain, columns = ["t","p","x","y","z"])

Simply loaded in from a file using numpy. Current plotting code is,

ax2 = sns.scatterplot("t","x", data = df, hue = "p")
plt.show()

The hue field is giving the grouping, so it's grouping by a polymer number parameter from the data file. The structure is that the are "N" polymers in the file, let's say 10, so the first 10 lines are each a different "p" value with the same "t" value and some x,y,z coordinate data. Plotting the coordinate data versus time is the main application. Let's say as an example, I want to plot the first 3 groups, so the first 3 polymers, out of the set in that plot command, I need to know how to do that, and then of course plot the next 3, etc. Very new to dataframes, so it's a bit puzzling to me how to manage this.

Edit for clarity, here's the table, using N = 5,

t   p  x  y  z
10  0  1  3  2
10  1  4  2  1
10  2  5  6  3
10  3  7  5  3
10  4  -9  5  2
20  0  1  -1  1
20  1  0  1  -1
20  2  3  9  -2
20  3  5  6  9
20  4  -5  9  6

So a desired output would be for the first 2 groups:

t   p  x  y  z
10  0  1  3  2
10  1  4  2  1
20  0  1  -1  1
20  1  0  1  -1

And then I could still plot by grouping their p values.

Upvotes: 0

Views: 1052

Answers (1)

AlexK
AlexK

Reputation: 3011

If you specifically need multiple groups (polymers) plotted together on the same chart, you can subset/filter your dataframe to only the polymer (p) values that you need for your plot, e.g.:

df[df['p'].isin([0,1])]

and pass the output to the scatterplot command.

Upvotes: 1

Related Questions