Home > Software design >  labeling Confidence interval and coefficient using ggplot in Pandas
labeling Confidence interval and coefficient using ggplot in Pandas

Time:06-24

I tried to label coefficient and Confidence interval using the following code:

pp =p.ggplot(leadslags_plot, p.aes(x = 'label', y = 'mean',
             ymin = 'lb', 
             ymax = 'ub'))  \
    p.geom_line(p.aes(group = 1),color = "b")  \
    p.geom_pointrange(color = "b",size = 0.5)  \
    p.geom_errorbar(color = "r", width = 0.2)  \
    p.scale_color_manual(name= "label:", values = ['b','r'],labels = ["coeff","95 percent CI"] ) \
    p.theme("bottom")  \
    p.xlab("Years before and after ")  \
    p.ylab("value ")  \
    p.geom_hline(yintercept = 0,
             linetype = "dashed")  \
    p.geom_vline(xintercept = 0,
             linetype = "dashed")   

the code generates the plot but does not label the 'coeff' and 'CI'. How can I label 'coeff' and 'CI'

enter image description here

CodePudding user response:

The issue is that to get a legend you have to map on aesthetics. In ggplot2 (the R one) this could be easily achieved by moving color="b" inside aes() which however does not work in plotnine or Python. Maybe there is a more pythonistic way to get around this issue but one option would be to add two helper columns to your dataset which could then be mapped on the color aes:

import pandas as pd
import plotnine as p

leadslags_plot = [[-2, 1, 0, 2], [0, 2, 1, 3], [2, 3, 2, 4]]
leadslags_plot = pd.DataFrame(leadslags_plot, columns=['label', 'mean', 'lb', 'ub'])

leadslags_plot["b"] = "b"
leadslags_plot["r"] = "r"

(p.ggplot(leadslags_plot, p.aes(x = 'label', y = 'mean',
             ymin = 'lb', 
             ymax = 'ub'))  \
    p.geom_line(p.aes(group = 1),color = "b")  \
    p.geom_pointrange(p.aes(color = "b"),size = 0.5)  \
    p.geom_errorbar(p.aes(color = "r"), width = 0.2)  \
    p.scale_color_manual(name= "label:", values = ['b','r'], labels = ["coeff", "95 percent CI"] ) \
    p.theme("bottom", subplots_adjust={'right': 0.8})  \
    p.xlab("Years before and after ")  \
    p.ylab("value ")  \
    p.geom_hline(yintercept = 0,
             linetype = "dashed")  \
    p.geom_vline(xintercept = 0,
             linetype = "dashed"))

enter image description here

  • Related