polm23
polm23

Reputation: 15593

How to plot variables separately with FacetGrid in Seaborn

I want to graph different variables separately (as small multiples) to see if any of them have any important trends. Let's say I'm tracking health stats of various kinds of lizards, and I want a separate graph for weight and length to see if there were any sudden changes. Here's my data:

month   hand    color   weight  length
1       left    blue    123.16  13.9
1       left    red     125.62  12.84
1       right   blue    186.46  7.18
1       right   red     152.3   7.51
2       left    blue    4465    187.77
2       left    red     116.27  10.6
2       right   blue    189.13  14.67
2       right   red     82.78   14.18
3       left    blue    124.85  13.25
3       left    red     178.51  8.33
3       right   blue    98.88   10.68
3       right   red     142.87  5.91

What I would like is two graphs, one for weight and one for length, and each graph has four lines (for right-handed blue lizards, right-handed red lizards, etc.).

I assume this counts as tiny data, which the FacetGrid docs say is required, because each row is an "observation" since weight and length are measured at the same time.

The FacetGrid seems set up to make a different graph based on values of variables. That seems clear from this example from the documentation:

>>> import matplotlib.pyplot as plt
>>> g = sns.FacetGrid(tips, col="time",  row="smoker")
>>> g = g.map(plt.hist, "total_bill")

Seaborn FacetGrid example

Is there some way I can specify a list of variables / columns for it to use for each graph instead? Or do I have to use another approach?

Upvotes: 0

Views: 634

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339092

You may add a new column, aggregating the hand and the color column

df["handcolor"] = df["hand"] + df["color"]

Then it seems you want to plot a line graph.

E.g.

fig, (ax1,ax2) = plt.subplots(ncols=2)
ax1.set_title("weight")
ax2.set_title("length")
for n,grp in df.groupby("handcolor"):
    ax1.plot(grp.month, grp.weight)
    ax2.plot(grp.month, grp.length)

enter image description here

Upvotes: 1

Related Questions