Skip to content

How to use Neptune with Matplotlib#

Open in Colab

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. With Neptune, you can log and display Matplotlib charts as static images or interactive HTML.

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 -U plotly

Tip

To follow the guide without any setup, run the example notebook in Colab

Matplotlib logging example#

  1. Import Neptune and start a run:

    import 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/plotting",
      )
      
  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.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()
    

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

[neptune] [info ] Neptune initialized. Open in the app: https://app.neptune.ai/workspace/project/e/RUN-1

Result

The resulting figure is logged as an HTML object.

You can view it in the All metadata section.