7

I am trying to get data from two separate dataframes onto the same scatterplot. I have seen solutions in R that use something like:

ggplot() + geom_point(data = df1, aes(df1.x,df2.y)) + geom_point(data = df2,aes(df2.x, df2.y))

But in python, with the ggplot module, I get errors when I try to use ggplot() with no args. Is this just a limitation of the module? I know I can likely use another tool to do the plotting but I would prefer a ggplot solution if possible.

My first data frame consists of Voltage information every 2 minutes and temperature information every one hour, so combining the two dataframes is not 1 to 1. Also, I would prefer to stick with Python because the rest of my solution is in python.

1 Answer 1

12

Just giving one dataframe as argument for ggplot() and the other inside the second geom_point declaration should do the work:

ggplot(aes(x='x', y='y'), data=df1) + geom_point() + 
       geom_point(aes(x='x', y='y'), data=df2)

(I prefer using the column name notation, I think is more elegant, but this is just a personal preference)

Sign up to request clarification or add additional context in comments.

4 Comments

That works, thank you. So, the 'base' ggplot needs its own dataset and subsequent geom_point() usages can add different datasets. Is that the gist of it?
It also surprised me the first time I realised about that. I don't really know the reason behind not allowing an empty ggplot() and including data in subsequent geom_point(). Maybe one of the main developers of the project could answer this better.
I think the error on an empty ggplot() call comes from the time where it wasn't allowed to specify a new df in each geom.
Doesn't seem to work for me with ggplot 0.11.2. I get ValueError: First argument must be a sequence. The first ggplot(...)+geom_point() part is working, the second geom_point(aes(...), data = df2) part is also working, but I can't + the two

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.