Skip to content

Working with Matplotlib#

Open in Colab

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.

Matplotlib figures in Neptune

See in Neptune  Code examples  

Before you start#

(Optional) To log static Matplotlib figures as interactive Plotly charts, install the plotly library:

pip install plotly
conda install -c conda-forge plotly
Plotly incompatibility

Plotly is not compatible with the latest Matplotlib (3.5.0+) due to the fact that mpl_to_plotly() uses deprecated Matplotlib functionalities.

To use Plotly together with Matplotlib, you may need to downgrade Matplotlib to version 3.2 or upgrade Plotly to 5.0+. For more information, see Plotly issue 1568 and issue 3624 on GitHub.

Matplotlib logging example#

  1. Import Neptune and start a run:

    import neptune.new as neptune
    
    run = neptune.init_run()  # (1)!
    
    1. If you haven't set up your credentials, you can log anonymously: neptune.init_run(api_token=neptune.ANONYMOUS_API_TOKEN, project="common/matplotlib-support")
  2. Create a sample figure:

    import matplotlib.pyplot as plt
    import numpy as np
    
    np.random. Seed(42)
    data = np.random.randn(2, 100)
    
    figure, ax = plt.subplots(2, 2, figsize=(5, 5))
    ax[0, 0].hist(data[0])
    ax[1, 0].scatter(data[0], data[1])
    ax[0, 1].plot(data[0], data[1])
    
  3. Log the figure to Neptune

    1. As a static image:

      run["static-img"].upload(figure)
      
    2. (With Plotly installed) As an interactive Plotly chart:

      from neptune.new.types import File
      
      run["interactive-img"].upload(File.as_html(figure))
      
      Info

      Not all Matplotlib charts can be converted to interactive Plotly charts. If the conversion is not possible, Neptune falls back to logging the chart as an image.

  4. To stop the connection to Neptune and sync all data, call the stop() method:

    run.stop()
    
    Warning

    Always call stop() in interactive environments, such as a Python interpreter or Jupyter notebook. The connection to Neptune is not stopped when the cell has finished executing, but rather when the entire notebook stops.

    If you're running a script, the connection is stopped automatically when the script finishes executing. However, it's a best practice to call stop() when the connection is no longer needed.

  5. To open the run, click the Neptune link in the console output.

    Example link: https://app.neptune.ai/common/matplotlib-support/e/MAT-1

  6. Explore the figures in the All metadata section: