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
1
pip install neptune-client matplotlib
Copied!
1
conda install -c conda-forge neptune-client matplotlib
Copied!
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
1
pip install plotly
Copied!
1
conda install -c conda-forge plotly
Copied!
This integration is tested with plotly==5.0.0.

Step 1: Import and initialize Neptune

1
import neptune.new as neptune
2
3
run = neptune.init(api_token='<your_api_token>', project='<your_project_name>')
Copied!
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

1
import matplotlib.pyplot as plt
2
import numpy as np
3
4
np.random.seed(42)
5
data = np.random.randn(2, 100)
6
7
figure, ax = plt.subplots(2, 2, figsize=(5, 5))
8
ax[0, 0].hist(data[0])
9
ax[1, 0].scatter(data[0], data[1])
10
ax[0, 1].plot(data[0], data[1])
Copied!

Log as a static image

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

Log as an interactive chart

If you have plotly installed, you can log your static matplotlib figures as interactive plotly charts.
1
run['interactive-img'].upload(neptune.types.File.as_html(figure))
Copied!
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.
1
run.stop()
Copied!

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:
Last modified 3mo ago