pythonggplot2plotninegeom-segment

How to create geom_segment() plot on Date x-axis using plotnine in python?


Not able to figure out how to use geom_segment() on Date x-axis in python by using plotnine. Have tried some code but getting error:

import pandas as pd
from plotnine import ggplot, aes, geom_segment
from datetime import date

# this is a sample dataset (created using ai, feel free to tweak or replace if needed)
data = pd.DataFrame({
    'date': pd.date_range('2022-01-01', periods=12, freq="ME"),
    'category': ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C'],
    'value': [10, 20, 30, 15, 25, 35, 20, 30, 40, 25, 35, 45]
})

# Create the plot
(ggplot(data, aes(x='date', y='category', xend='date', yend='value')) + 
    geom_segment(
        size=8.5,
        color="#edece3") + 
    theme(axis_text_x=element_text(angle=45, hjust=1))
(ggplot(data, aes(x='date', y='category', xend='date', yend='category')) + 
    geom_segment(
        size=8.5,
        color="#edece3") + 
    theme(axis_text_x=element_text(angle=45, hjust=1))

even when I try to mention date manually then it doesn't work

ggplot(data, aes(x='date', y='category', xend=date(2022,12,1), yend='value'))
ggplot(data, aes(x='date', y='category', xend=date(2022,12,1), yend='category'))

All I am trying is to create #edece3 color segment to extend from start date to end date for all the categories.

taking below image as an example/inspiration and trying to replicate some part but stuck at the start:

enter image description here


Solution

  • First, I get some errors as you did not import all required functions from plotnine. Second, your are mapping two different variables on y, i.e. the discrete category and the continuous value via yend which will not work. Third, from your question, data and code it's not clear to me how you want to replicate the plot in the image, i.e. is your category the "Country" or the "age_group"?

    Here is one possible plot which can be made based on the provided data which maps the category on y and instead of using a geom_segment (which requires a start and end column) uses a geom_line.

    import pandas as pd
    import plotnine as p9
    from datetime import date
    
    # Create the plot
    (p9.ggplot(data, p9.aes(x='date', y='category', group='category')) + 
        p9.geom_line(
            size=8.5,
            color="#edece3") + 
        p9.geom_point(
            size=8.5,
            color="red") + 
        p9.theme_bw() +
        p9.theme(axis_text_x=p9.element_text(angle=45, hjust=1)))
    

    enter image description here