Search code examples
pythonmatplotlibjupyter-notebookmatplotlib-3d

<Figure size 800x800 with 0 Axes> when using plt.show() Jupyter Notebook


I am trying to do a CISCO IoT Big Data course and i have to do a lab in jupyter notebook with Python kernel. Still i can't see the figure even if i did exactly like it shows me in the lab. It simply shows me: <Figure size 800x800 with 0 Axes>
Here is the code:


%matplotlib inline
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from ipywidgets import interact

def scatter_view(x, y, z, azim, elev):
    # Init figure and axes
    fig = plt.figure(figsize=(8, 8))
    ax = Axes3D(fig)
    
    # Compute scatter plot
    ax.scatter(x, y, z)
    ax.set_xlabel('D rate (Mbit/s)', fontsize=16)
    ax.set_ylabel('U rate (Mbit/s)', fontsize=16)
    ax.set_zlabel('P rate (1/s)', fontsize=16)
    ax.azim = azim
    ax.elev = elev

xi = df_rates['download_rate']
yi = df_rates['upload_rate']
zi = df_rates['ping_rate']
interact(lambda azim, elev: scatter_view(xi, yi, zi, azim, elev),
         azim=(0, 90), elev=(0, 90))
plt.show()

I have tried to change figure size, use matplotlib in line before or after the import. I couldn't find a solution on the internet. I'm pretty new to this concept, if anyone can explain to me why it doesn't show. Thanks!


Solution

  • Replace the line

    ax = Axes3D(fig)
    

    with

    ax = fig.add_subplot(projection='3d')
    

    See the documentation:

    3D Axes (of class Axes3D) are created by passing the projection="3d" keyword argument to Figure.add_subplot:

    import matplotlib.pyplot as plt
    fig = plt.figure()
    ax = fig.add_subplot(projection='3d')
    

    Also quoting the api documentation for Axis3D:

    Note

    As a user, you do not instantiate Axes directly, but use Axes creation methods instead; e.g. from pyplot or Figure: subplots, subplot_mosaic or Figure.add_axes.