Plotnine: How remove the ggplot:(xxx) type annoying text output when plotting a graph
Asked Answered
S

2

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 ?

Soraya answered 29/11, 2018 at 16:15 Comment(0)
G
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();
Giblets answered 30/11, 2018 at 19:42 Comment(5)
WORKS perfectly ! Much appreciated. I looked forever trying to find a way around having that <ggplot: (8762473532957)> 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...Soraya
WORKS perfectly ! Much appreciated. I looked forever trying to find a way around having that <ggplot: (8762473532957)> 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.Soraya
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.Jugendstil
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.Jugendstil
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.Aleman
A
2

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

However, it did the trick:

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

Antipas answered 17/6, 2022 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.