Skip to content

Instantly share code, notes, and snippets.

@datavistics
Created September 8, 2017 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datavistics/86bfe08c7d653e08c4c9a33001f8f88e to your computer and use it in GitHub Desktop.
Save datavistics/86bfe08c7d653e08c4c9a33001f8f88e to your computer and use it in GitHub Desktop.
An attempt to use plotnine to create a barplot with a line.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@has2k1
Copy link

has2k1 commented Sep 8, 2017

Very subtle but it is all in the warning. The issue is you have 4 groups (Alpha, Beta, ...) and each of those groups has a single point. You may consider this an edge case where the automatic grouping that is done for you is not sufficient (or just not what you expect). Solution is to manually set the group for geom_line.

(ggplot(df) + 
   geom_bar(aes(x='letter',y='pos'), stat='identity') + 
   geom_line(aes(x='letter',y='num_of_letters'), stat='identity', group=1) + 
   ggtitle('Greek Letter Analysis')
)

tmp0

To understand why the behaviour behind the warning is not wrong, check this out

d = {'letter': ['Alpha', 'Beta', 'Delta', 'Gamma'] * 2,
     'pos': [1, 2, 3, 4] * 2,
     'num_of_letters': [5, 4, 5, 5] * 2}

df = pd.DataFrame(d)

df.loc[4:, 'num_of_letters'] += 0.8
df

(ggplot(df) +
   geom_bar(aes(x='letter',y='pos'), stat='identity') +
   geom_line(aes(x='letter',y='num_of_letters')) +
   ggtitle('Greek Letter Analysis')
)

tmp1

With the above there are 2 points per group so you can get lines. Depending on what you are trying to do you may want to improve on such a plot and add some color.

(ggplot(df)
   geom_bar(aes(x='letter',y='pos', fill='letter'), stat='identity') +
   geom_line(aes(x='letter', y='num_of_letters', color='letter'), size=1) +
   scale_color_hue(l=0.45)  + # some contrast to make the lines stick out
  ggtitle('Greek Letter Analysis') +
)

tmp2

@datavistics
Copy link
Author

Hey, that is totally fair, I just couldn't figure that out on my own or via documentation. Its a great package, I do wish there was more community support.

Its very kind of you to respond with such an indepth explanation. I really appreciate that @has2k1

@has2k1
Copy link

has2k1 commented Sep 9, 2017

Great. I will add this example to the tutorials page of the documentation. That should have been my thought in the first place.

@raijinspecial
Copy link

sorry for the noob question, but I am trying to change the colors of bars plotted using geom_col. I used fill to color them by a category from my dataframe, but I would like to be able to specify which color represents a specific group, I am failing to find a clear way to do this.

Plotnine is wonderful, its my go to now.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment