143

When using this in a script (not IPython), nothing happens, i.e. the plot window doesn't appear :

import numpy as np
import pandas as pd
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts.plot()

Even when adding time.sleep(5), there is still nothing. Why?

Is there a way to do it, without having to manually call matplotlib ?

4
  • Do you get any output from ts.plot()? Commented Dec 18, 2015 at 1:34
  • 2
    what do you mean "manually call matplotlib"? the usual way is import matplotlib.pyplot as plt, plt.show() Commented Dec 18, 2015 at 4:22
  • 1
    @itzy : nothing happens when ts.plot(), the program terminates immediately after this. Even if I add time.sleep(10) nothing is displayed Commented Dec 18, 2015 at 8:00
  • 3
    @maxymoo : I thought that doing .plot() on a panda object would handle everything for me (i.e. pandas does the matplotlib job, instead of me) : see this example. Commented Dec 18, 2015 at 8:02

4 Answers 4

241

Once you have made your plot, you need to tell matplotlib to show it. The usual way to do things is to import matplotlib.pyplot and call show from there:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts.plot()
plt.show()

In older versions of pandas, you were able to find a backdoor to matplotlib, as in the example below. NOTE: This no longer works in modern versions of pandas, and I still recommend importing matplotlib separately, as in the example above.

import numpy as np
import pandas as pd
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts.plot()
pd.tseries.plotting.pylab.show()

But all you are doing there is finding somewhere that matplotlib has been imported in pandas, and calling the same show function from there.

Are you trying to avoid calling matplotlib in an effort to speed things up? If so then you are really not speeding anything up, since pandas already imports pyplot:

python -mtimeit -s 'import pandas as pd'
100000000 loops, best of 3: 0.0122 usec per loop

python -mtimeit -s 'import pandas as pd; import matplotlib.pyplot as plt'
100000000 loops, best of 3: 0.0125 usec per loop

Finally, the reason the example you linked in comments doesn't need the call to matplotlib is because it is being run interactively in an iPython notebook, not in a script.

Sign up to request clarification or add additional context in comments.

3 Comments

When I run 'pd.tseries.plotting.pylab.show()' I get "padnas.tseries' has no attribute plotting, is it outdated? You did write that in 2015..
Yes, it looks like that back door to matplotlib is no longer there. I still recommend the first option anyway
Thanks. I think the API design could be better. It is odd we need to know about matplotlib to use panda.plot
8

In case you are using matplotlib, and still, things don't show up in iPython notebook (or Jupyter Lab as well) remember to set the inline option for matplotlib in the notebook.

import matplotlib.pyplot as plt

%matplotlib inline

Then the following code will work flawlessly:

fig, ax = plt.subplots(figsize=(16,9));
change_per_ins.plot(ax=ax, kind='hist')

If you don't set the inline option it won't show up and by adding a plt.show() in the end you will get duplicate outputs.

Comments

1

I did just

import matplotlib.pyplot as plt

%matplotlib inline

and add line

plt.show()

next to df.plot() and it worked well for

3 Comments

Please include which environment you use to be able to insert %matplotlib inline (this is not standard Python that can be used in a script).
%matplotlib inline is used in ipython/jupyter notebook...
Yeah, there is no need to add %matplotlib inline if you are using an IDE like pycharm(I've been using it).
0

The other answers involve importing matplotlib.pyplot and/or calling some second function manually.

Instead, you can configure matplotlib to be in interactive mode with its configuration files.

Simply add the line

interactive: True

to a file called matplotlibrc in one of the following places:

  • In the current working directory
  • In the platform specific user directory specified by matplotlib.get_configdir()
    • On unix-like system, typically /home/username/.config/matplotlib/
    • On Windows C:\\Documents and Settings\\username\\.matplotlib\\

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.