In addition to interactive, no-code Plot cell, Latch Plots allows developers to programmatically use any library and generate plots of interest.

There are two main ways to programmatically create custom plots:

  1. Output an image: In a Python cell, you can use any Python library to generate and display the plot as a static image.
  2. Output a Plotly figure: In a Python cell, you can use the Plotly library to generate an interactive Plotly figure

Option 1: Display a plot as an image

Step 1: Install custom Python packages

To install your Python libraries, first click on Custom Data Analysis to create a Python cell. Paste in the following Python code to install packages:

import os

os.system("/opt/mamba/envs/plots-faas/bin/pip install matplotlib")

Step 2: Create a plot and display it as an image

To display a plot from a custom Python package, you have to first save the plot as an image, and display the image in the Python cell.

Below, we provide example code of how you can do so:

import pandas as pd
import matplotlib.pyplot as plt

# Load the dataset
df = pd.read_csv("https://latch-public.s3.us-west-2.amazonaws.com/plots/test-data/iris.csv")

# Create the scatter plot
plt.figure(figsize=(10, 6))

print(df)

plt.scatter(df['sepal.length'], df['sepal.width'], c='blue', label='Sepal')
plt.scatter(df['petal.length'], df['petal.width'], c='green', label='Petal')

# Add labels and title
plt.xlabel('Length')
plt.ylabel('Width')
plt.title('Iris Dataset: Sepal and Petal Dimensions')
plt.legend()

# Save the plot as an image
plt.savefig('iris_scatter_plot.png')

# Save the image to Latch Data
from latch.ldata.path import LPath
latch_path = LPath("latch:///iris_scatter_plot.png")
latch_path.upload_from("iris_scatter_plot.png")

# Display the image
from lplots.widgets.text import w_text_output

w_text_output(content="Below is the output image")
w_text_output(content="![](latch:///iris_scatter_plot.png)")

The code above includes:

  1. Loading the dataset: The dataset is loaded from a CSV file using pandas.
  2. Creating a scatter plot: We create a scatter plot using matplotlib with sepal and petal dimensions in different colors.
  3. Saving the plot as an image: The plot is saved as iris_scatter_plot.png.
  4. Uploading to Latch Data: The saved image is uploaded to Latch Data using LPath.
  5. Displaying the image: The image is displayed in the notebook using w_text_output.

Option 2: Display a custom, interactive Plotly figure

Step 1: Import the Plotly library and create an interactive plot

First, click on Custom Data Analysis to create a new Python cell. Next, import the Plotly Express library and create your plot of interest.

In the example below, we created a scatter plot for the iris dataset, and save the Plotly object as a Python variable called fig.

import plotly.express as px

df = px.data.iris()

fig = px.scatter(
  df,
  x="sepal_length",
  y="sepal_width",
  template="simple_white"
)

Step 2: Create a Plot cell

Click on Plot to create a new interactive plot cell.

Step 3: Select the Plotly object as the source for the Plot cell

Click Data Source and select the Python variable that represents your Plotly object.

In our iris example above, the variable is called fig. The interactive figure will be displayed in the middle of the Plots notebook’s screen, and will automatically update if the fig object is updated by running the upstream code.