6
  • Running jupyter notebook (python)
  • Plotting using Python Plotnine library
  • I plot and below the output graphic is annoying "ggplot2: (number)" output
  • Normally you would put a ; at the end of your notebook cell, but it doesn't seem to supress the annoying output text when i use Plotnine (but it does obviously work for matplotlib, etc)

Any ideas ?

markus
  • 25,843
  • 5
  • 39
  • 58
TexasTom
  • 309
  • 1
  • 2
  • 6

2 Answers2

8

The point is in calling draw() method with semicolon at the end.

Fully working example:

import pandas
from plotnine import *
from random import randint

# 100 random numbers
random_numbers = [randint(1, 100) for p in range(0, 100)]

# Create DataFrame
df = pd.DataFrame({'number': random_numbers})

# Draw plot
(
    ggplot(df, aes(x='number')) + 
    geom_histogram(bins=20, na_rm=True) +
    ggtitle('Histogram of random numbers') +
    theme_light()
).draw();
Stefan Simik
  • 165
  • 1
  • 6
  • WORKS perfectly ! Much appreciated. I looked forever trying to find a way around having that `` pop up at the end of the output. Doesn't follow the standard matplotlib convention of just putting a `;` at the end of your command, like you pointed out, need to have the `.draw();' at the end... – TexasTom Dec 01 '18 at 01:18
  • WORKS perfectly ! Much appreciated. I looked forever trying to find a way around having that `` pop up at the end of the output. Doesn't follow the standard matplotlib convention of just putting a `;` at the end, like you pointed out, you need to have the `.draw();' at the end... I swear everywhere i have seen plotnine output it doesn't have that .draw(); command, and so you see like on kaggle kernels where that text output isn't supressed. THANK YOU. – TexasTom Dec 01 '18 at 01:30
  • 1
    Any matplotlib figure that is created in a cell is printed to the output when the cell run. Also the result of the last command in a cell is printed to the output. In plain matplotlib, you create a figure, plot stuff on it and when the cell is run that figure will be printed, no matter were in the cell it was created. – has2k1 Dec 11 '18 at 09:31
  • 1
    For plotnine, a figure is only created when the `draw()` method is called. When the `ggplot` object is the last in the cell, it gets printed `repr` style and it has to be a string. Before that string is returned, the `draw()` method is called, hence a figure. – has2k1 Dec 11 '18 at 09:39
  • unfortunately, this does not work well in a loop. E.g. printing a loop with a text block, followed by a plot.draw(); will output all the text blocks first, and then all the plots. – TemplateRex Oct 29 '20 at 10:26
1

The draw(); method didnt't work for me.

However, it did the trick:

warnings.filterwarnings( "ignore", module = "plotnine\..*" )

Pablo Cánovas
  • 151
  • 1
  • 6