Search code examples
pythonplotnine

Is there an equivalent to %+% for plotnine?


In R's ggplot2, you can modify the dataset for a saved plot with %+%. Is there an equivalent in Python's plotnine?

As an example, here is what this looks like in R:

library(ggplot2)

df1 <- data.frame(x = 1:10, y = 1:10 * 2)
p1 <- ggplot(data = df1, aes(x = x, y = y)) + geom_line()

df2 <- data.frame(x = 1:10, y = 1:10 * 3)
p1 %+% df2  # produces the same plot using the df2 data.frame

Solution

  • I am not aware about the ggplot function of R, but according to your comments, I think creating a custom function to plot a graph from a df will work.

    def create_plots(df):
        #code to create the ggplot
    

    This way you can easily change out the dataframe. I looked at plotnine's API reference, and there doesn't seem to be a way to do that.