3

I have the following data:

import pandas as pd
import matplotlib.pyplot as plt
test = pd.DataFrame({"series":["plot1","plot1","plot1","plot2","plot2","plot2"],
                     "ind_var": [0,1,2,0,1,2],
                     "dep_var": [0,1,2,0,2,4]})

plot1 is described by f(x)=x, while plot2 is g(x)=2x.

I want to plot both series in the same figure and have them share the same x and y axis. I came across this solution, but it requires the data to be in wide format (it needs one line of code per group, which I find inconvenient).

I want to know if there's something similar to the color argument from ggplot2. That is, an argument which will plot one column grouped by test["series"]. The advantage of doing this is more evident when there's multiple series, so you don't have to write one line per series.

I found a similar question and this is as close as I could get:

# test.set_index("ind_var", inplace=True) This is the line I was missing
test.groupby("series")["dep_var"].plot(legend=True)
plt.show()

Almost

Is there a way to plot both series in a single figure using something along the lines of grooupby?

Arturo Sbr
  • 5,567
  • 4
  • 38
  • 76
  • 2
    What's wrong with the solution you found? – ImportanceOfBeingErnest Sep 09 '19 at 20:49
  • If I were to follow the first one I would have to write 135 lines of code (I have 135 groups). The second solution didn't work. Both series share the same `x` variable, so they should cover the same range and be plotted over the same y scale. – Arturo Sbr Sep 09 '19 at 20:50
  • Loops are one option. At the same time, doesn't pandas handle this situation out of the box? – Mad Physicist Sep 09 '19 at 20:52
  • 1
    No, that is not true. `"plot1"` contains x values 0,1,2, while `"plot2"` contains 3,4,5. – ImportanceOfBeingErnest Sep 09 '19 at 20:55
  • @MadPhysicist Not that I know of. Please explain. – Arturo Sbr Sep 09 '19 at 20:55
  • 3
    `sns.lineplot(x='ind_var', y='dep_var', hue='series', data=test)`? – Quang Hoang Sep 09 '19 at 20:56
  • Or `test.pivot(index='ind_var', columns='series', values='dep_var').plot()`? – Quang Hoang Sep 09 '19 at 20:57
  • @ImportanceOfBeingErnest My bad! I edited the question. – Arturo Sbr Sep 09 '19 at 20:58
  • 1
    Yeah, but you didn't edit the image. With the changed data you will get exactly what you ask for. – ImportanceOfBeingErnest Sep 09 '19 at 20:58
  • @ImportanceOfBeingErnest You are absolutely right. I updated the question accordingly. Thanks! Regardless, I was looking for something along the lines of Quang Hong's first comment. – Arturo Sbr Sep 09 '19 at 21:06
  • @QuangHoang Your first comment is exactly what I was looking for. Even the syntax looks like `ggplot2`. You want to go ahead and post the answer so I can close the question? – Arturo Sbr Sep 09 '19 at 21:07
  • @ImportanceOfBeingErnest Is there any chance you could remove the duplicate suggestion? I was specifically looking for an answer using `matplotlib`, not `seaborn`. It just so happens that I liked the `seaborn` solution, but I wasn't asking for it explicitly. If anything, shouldn't this be a duplicate of the question I made a reference to originally? – Arturo Sbr Sep 09 '19 at 21:14

0 Answers0