Search code examples
pythonggplot2plotplotnine

How to put errorbars on only one group in plotnine?


How do I put error bars on only one group in plotnine?

For example, say I have a dataframe:

dat = pd.DataFrame(data={"Type":["A","A","B","B"],"x":[1,2,1,2],"y":[1,2,2,4]}) 

Type  x  y
0    A  1  1
1    A  2  2
2    B  1  2
3    B  2  4

I can then plot two lines:

My plot

ggplot(dat,aes(x="x",y="y",linetype="Type")) + geom_line()

How woudld I then only add errorbars to A? Or more generally, how do I specify a subset of data in plotnine in subsequent additions to the plot?


Solution

  • Each geom layer has a data= argument which allows to specify the data to be used for this layer, whether from a second dataset or a subset of the global data, i.e. if you want to use only a subset of our data pass a filtered dataset to the data= argument.

    import pandas as pd
    from plotnine import *
    
    dat = pd.DataFrame(data={"Type":["A","A","B","B"],"x":[1,2,1,2],"y":[1,2,2,4]})
    
    ggplot(dat,aes(x="x",y="y",linetype="Type")) + \
        geom_line() + \
        geom_errorbar(
            aes(ymin="y-.5", ymax="y+.5"), 
            data = dat[dat["Type"] == "A"], 
            width = .1)
    

    enter image description here