Matplotlib

Video Walkthrough

What will you get with this integration?

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. This integration lets you log charts generated in matplotlib to Neptune for easy reference and collaboration.

Introduction

This guide will show you how to:

  • Initialize Neptune and create a run,

  • Log matplotlib charts to Neptune.

Step 0: Before you start

Make sure that:

Install neptune-client and matplotlib

Depending on your operating system open a terminal or CMD and run this command. All required libraries are available via pip and conda:

pip
conda
pip
pip install neptune-client matplotlib
conda
conda install -c conda-forge neptune-client matplotlib

For more help see installing neptune-client.

This integration is tested with neptune-client==0.9.5 and matplotlib==3.2.2.

Install plotly (optional)

If you want to log static matplotlib figures to interactive plotly charts, you need to install plotly.

pip
conda
pip
pip install plotly
conda
conda install -c conda-forge plotly

This integration is tested with plotly==5.0.0.

Step 1: Import and initialize Neptune

import neptune.new as neptune
run = neptune.init(api_token='<your_api_token>', project='<your_project_name>')

You can use the api_token='ANONYMOUS' and project='common/matplotlib-support' to explore without having to create a Neptune account

Executing this cell will give you a link looking like: https://app.neptune.ai/common/matplotlib-support/e/MAT-3352 with common/matplotlib-support replaced by your project, and MAT-3352 replaced by your run.

Click on the link to open the run in Neptune. For now, it is empty but keep the tab with the run open to see what happens next.

Step 2: Log Matplotlib figures to Neptune

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])

Log as a static image

run["static-img"].upload(neptune.types.File.as_image(figure))

Log as an interactive chart

If you have plotly installed, you can log your static matplotlib figures as interactive plotly charts.

run['interactive-img'].upload(neptune.types.File.as_html(figure))

Not all matplotlib charts can be converted to interactive plotly charts. If the conversion is not possible, neptune-client will return a warning and fall back on logging the chart as an image.

Step 3: Stop logging

Once you are done logging, you should stop tracking the run using the stop() method. This is needed only while logging from a notebook environment. While logging through a script, Neptune automatically stops tracking once the script has completed execution.

run.stop()

Step 4: Explore the charts in Neptune

The images can be found in the All metadata section:

Conclusion

You’ve learned how to:

  • Initialize Neptune and create a run,

  • Log matplotlib charts to Neptune.

What's next

Now that you know how to log charts to Neptune, you can learn: