Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting one scatterplot with multiple dataframes with ggplot in python

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.

like image 959
scld Avatar asked Mar 13 '14 11:03

scld


People also ask

Is ggplot better than Matplotlib?

Both packages achieved very similar results. But the contour lines, labels, and legend in matplotlib are superior to ggplot2.

How do I plot multiple data frames in R?

To plot multiple datasets, we first draw a graph with a single dataset using the plot() function. Then we add the second data set using the points() or lines() function.


1 Answers

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)

like image 128
prl900 Avatar answered Oct 31 '22 17:10

prl900