Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I have to use plt.show()? [duplicate]

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)  
df_new = pd.DataFrame(np.random.randn(5,3), columns=list('ABC'))

df_new.plot(kind='bar')

I have no idea about weather I have to use plt.show() or not. Because if there is only df_new.plot(kind='bar'),it will shows the plot. If I add plot.show(),it shows also. What is the difference between them? I use Jupyter. Thanks in advance.


1 Answers

I guess your using interactive tool like jupyter.
In case of jupyter as you said df.plot(kind ='bar') displays the graph.
In interactive tools, You need to use plt.show() when all your plots are created.

  import matplotlib.pyplot as plt
  plt.plot(x, y)
  plt.plot(y,z)
  plt.show()

In python IDLE if you just use df.plot((kind = 'bar'), it won't displays the graph on run.
To display the graph without using plt.show() in IDLE you can follow the following steps.

 import matplotlib.pyplot as plt
 from matplotlib import interactive
 import pandas as pd
 import numpy as np

 #Set interactive mode to True
 interactive(True)
 df = pd.DataFrame(np.random.randn(5,3), columns = list('ABC'))
 df.plot(kind = 'bar')

I hope this answers your question.

like image 124
Gags08 Avatar answered Sep 22 '25 22:09

Gags08