3

I am trying to create a 3D scatter plot using matplotlib in a Jupyter Notebook page. The code is not returning any errors, but I have yet to have the plot actually show up. The output is just blank.

Python: 3.7.3 Matplotlib: 3.0.3

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

%matplotlib inline
%matplotlib notebook

threedee = plt.figure().gca(projection='3d')
threedee.scatter(existing_df_2d.PC1, existing_df_2d.PC2, 
existing_df_2d.data_mean)

plt.show()

I included an example of the output (it's blank):

Output

Engineero
  • 12,340
  • 5
  • 53
  • 75
Pythoner
  • 460
  • 6
  • 23

2 Answers2

2

You are using two backends

%matplotlib inline 
%matplotlib notebook

As a result, there seems to be a conflict between the two backends when invoked in parallel one after the other.

P.S: When I tried putting %matplotlib notebook in the same cell as the rest of the code, I did not see any figure. When I put it in a different cell, I see the figure.

Solution: Just use either the %matplotlib inline or %matplotlib notebook in a new separate cell and things will work fine

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • It worked! Thank you! Just out of curiosity, what is the difference between them? When should one be used over the other? – Pythoner Jul 15 '19 at 15:32
  • 1
    @alkhalifas : `%matplotlib notebook` allows you interactive functionalities such as rotating 3d figure, zooming in figures, within the notebook. By the way, I just realised that there is similar question asked [here](https://stackoverflow.com/questions/43545050/using-matplotlib-notebook-after-matplotlib-inline-in-jupyter-notebook-doesnt) – Sheldore Jul 15 '19 at 15:34
-1

In my experience, %matplotlib notebook doesn't work with 3D plots unfortunately. Just use %matplotlib inline and you should be OK.

Engineero
  • 12,340
  • 5
  • 53
  • 75