1

I've been trying to figure out plotting with plotnine, being the most ggplot2-esque library in Python from what I've heard, and I'm having difficulty accomplishing it in PyCharm in Python scripts. This seems like a problem that has many solutions for matplotlib and other plotting libraries which also experience this bug, but using their answers (adding DISPLAY=True to environment variables, adding show() at the end of plots, etc) doesn't work for plotnine.

I'm trying to plot the following, which works in a regular ipython window (which I access through Terminal):

from plotnine import *
from plotnine.data import *

(ggplot(diamonds)
 + stat_summary(aes('clarity', 'price', fill='cut'), geom='col', position='dodge')
 + stat_summary(aes('clarity', 'price', color='cut'), geom='linerange', size=1, position=position_dodge(width=0.9))
 + scale_color_hue(l=.3)
 + theme(figure_size=(11, 8))
)

What am I missing here, or am I not supposed to try plotting from within a script, which is normally so convenient for everything else?

P.S. Resolution: This seems to be a bug within PyCharm that the community may not have noticed yet, but that is somewhat fair as the plotnine library is a bit new as well. My workaround solution was to eschew from trying to do it in a script and instead to plot from within the ipython console in PyCharm, which works.

halfer
  • 19,824
  • 17
  • 99
  • 186
Coolio2654
  • 1,589
  • 3
  • 21
  • 46

1 Answers1

5

You have to print the ggplot object.

p = (ggplot(diamonds)
     + stat_summary(aes('clarity', 'price', fill='cut'), geom='col', position='dodge')
     + stat_summary(aes('clarity', 'price', color='cut'), geom='linerange', size=1, position=position_dodge(width=0.9))
     + scale_color_hue(l=.3)
     + theme(figure_size=(11, 8))
)

print(p)

IPython automatically prints the last result in cell for you. For other environments you have to do it explicitly

has2k1
  • 2,095
  • 18
  • 16