9

I am trying to create a 2x2 plots for Anscombe data-set

Loading Data-set and separating each class in data-set

import seaborn as sns
import matplotlib.pyplot as plt

anscombe = sns.load_dataset('anscombe')

dataset_1 = anscombe[anscombe['dataset'] == 'I']
dataset_2 = anscombe[anscombe['dataset'] == 'II']
dataset_3 = anscombe[anscombe['dataset'] == 'III']
dataset_4 = anscombe[anscombe['dataset'] == 'IV']

Creating a figure and dividing into 4 parts

fig = plt.figure()

axes_1 = fig.add_subplot(2,2,1)
axes_2 = fig.add_subplot(2,2,2)
axes_3 = fig.add_subplot(2,2,3)
axes_4 = fig.add_subplot(2,2,4)

axes_1.plot(dataset_1['x'], dataset_1['y'], 'o')
axes_2.plot(dataset_2['x'], dataset_2['y'], 'o')
axes_3.plot(dataset_3['x'], dataset_3['y'], 'o')
axes_4.plot(dataset_4['x'], dataset_4['y'], 'o')

axes_1.set_title('dataset_1')
axes_2.set_title('dataset_2')
axes_3.set_title('dataset_3')
axes_4.set_title('dataset_4')

fig.suptitle('Anscombe Data')

fig.tight_layout()

The only output which i'm getting at each plot is

[<matplotlib.lines.Line2D at 0x24592c94bc8>]

What am I doing wrong?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
pquest
  • 303
  • 1
  • 3
  • 14
  • 2
    This issue isn't reproducible. I can get your plots with exactly what you have. `conda update conda` and `conda update --all` at the conda prompt. Also, instead of `jupyter notebook`, type `jupyter lab` at the prompt. This is the newer version of jupyter and is already part of the distribution. – Trenton McKinney Apr 09 '20 at 18:58
  • OK..i'm trying. – pquest Apr 09 '20 at 19:05
  • **Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure. """Entry point for launching an IPython kernel** – pquest Apr 09 '20 at 19:32
  • https://stackoverflow.com/questions/37365357/when-i-use-matplotlib-in-jupyter-notebook-it-always-raise-matplotlib-is-curren – Trenton McKinney Apr 09 '20 at 20:16

4 Answers4

18

If you are working with a Jupyter Notebook then you can add the following line to the top cell where you call all your imports. The following command will render your graph

%matplotlib inline

enter image description here

2

Add%matplotlib inline or use matplotlib.pyplot.ion()

after you imported matplotlib.

From plotting docs:

Starting with IPython 5.0 and matplotlib 2.0 you can avoid the use of IPython’s specific magic and use matplotlib.pyplot.ion()/matplotlib.pyplot.ioff() which have the advantages of working outside of IPython as well.

Hossein
  • 24,202
  • 35
  • 119
  • 224
  • restart your kernel and see if that fixed the issue . you can also remove the inline part and see if it shows up in a new window – Hossein Apr 09 '20 at 18:51
  • Tried both still same result..no error received and plots not showing – pquest Apr 09 '20 at 18:53
  • 1
    try forcing it with `plt.draw()` or `plt.show()` and see if that helps. give `%matplotlib notebook` a try as well . – Hossein Apr 09 '20 at 18:56
  • Not working..Could you please check the same code in your environment and check if it is working for you – pquest Apr 09 '20 at 18:58
  • works fine on my end. give `display()` a try . wrap your last statement inside display and see if that works. if that doesnt work, I guess an update would be a good idea! – Hossein Apr 09 '20 at 19:01
  • Maybe my environment has some issue..nothing working – pquest Apr 09 '20 at 19:04
  • 1
    just asking, you are not by any chance using the vscode ipython interactive mode are you? if you are, vscode has its own share of issues, please use Jupyter notebook. if you are using jupyter notebook, try updating it both the jupyter itself and notebook. – Hossein Apr 09 '20 at 19:06
  • I'm using Jupyter notebook..I have updated the Jupyter and jupyter notebook as well...But its still not working – pquest Apr 09 '20 at 19:15
  • This worked for me. I added plt.ioff() at the top of the notebook, after the imports. Without that, the suboplots rendered well before the plotting commands (scatter, etc.) because I have the plot logic beoken into multiple notebook cells and the IPython 'magic' was causing each cell to run to completion before starting the next cell. – nicomp Jan 28 '23 at 11:28
1

I've tried all of the above, eventually I've found out there is a conflict between matplotlib and a library called dtale. When I removed the import dtale command and restarted the kernel all was working prefectly good.

0
%matplot plt

Executing this after plt.show() displayed the plots for me.
Found this here: How do I make matplotlib work in AWS EMR Jupyter notebook?