Search code examples
pythonmatplotlibggplot2python-ggplot

plot ad hoc the sum of two columns python


I have a dataframe with 2 columns namely mean and sd.

is there any way that I can plot ad hoc the summation of these two columns?

I do not want to create a third column but I want to plot the: mean + sd as one column.

In R world using ggplot you can use dplyr and do something like:

df %>% mutate(sumsd = sum+sd) %>% ggplot(.) etc without saving the data into a column.


Solution

  • The data to be plotted needs to be somewhere in memory; a dataframe column is one whay to achieve that. Assuming the following data

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    a = np.random.rand(100,20)
    df = pd.DataFrame({"mean" : np.mean(a,1),
                        "std" :  np.std(a,1)})
    

    You may now use the pandas plotting wrapper. Just add the two columns.

    (df["mean"]+df["std"]).plot()
    

    You may also sum the dataframe,

    df.sum(axis=1).plot()
    

    or if you have more columns,

    df[["mean","std"]].sum(axis=1).plot()
    

    The same can of course be done using matplotlib directly,

    # case 1
    plt.plot(df.index, (df["mean"]+df["std"]))
    # case 2
    plt.plot(df.index, df.sum(axis=1))
    # case 3
    plt.plot(df.index, df[["mean","std"]].sum(axis=1))