How To Create Dataset Visualization

Create visual representation of datasets using Labguru's API and automation tool

Malka Hoffmann avatar
Written by Malka Hoffmann
Updated over a week ago

Important note: creating visualization requires our Workflow Editor add-on.

Data visualization is an essential tool as it allows us to analyze complex information and identify patterns and correlations that may not be immediately obvious. Visualizing data makes it easier to draw valuable insights and make well-informed decisions based on the obtained data.

In this article, you will learn how to utilize the visualization API endpoint to generate and attach visual representations to your datasets.
Below is a step-by-step guide for creating and posting a plot under the 'Visualization' tab of a dataset.

​In this example, we will create a visualization for the following Dataset:

Step 1: Create a new flow and choose a trigger
In the Workflow Editor create a new flow and set the trigger to "Vector - Create", to have the flow running when a new vector (i.e. row) is added to the dataset:

Step 2: Add a condition step

Add a condition step for the flow to be executed only when a vector is added to the target dataset. Use the vector's 'dataset_id' attribute to access the ID of the updated dataset:

Step 3: Add a GET step to retrieve the target Dataset
​Use a 'GET dataset' step to retrieve and store the Dataset's JSON as a variable, use the Dataset's ID:


Step 4: Add a Scripter Step and use it for writing the code in the following steps

Step 5: Retrieve data from the dataset and create a DataFrame

Store the dataset retrieved in Step 3 as a variable within the scripter to pull the required information, and create a DataFrame (using the Pandas library):

import requests
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, DayLocator

# Retrieve data from dataset
data_list =[]
dataset = variable ("dataset_1")
for vector in dataset["vectors"]:
data_list.append(vector["vector"]["data"])
print (vector["vector"]["data"])

# Create a DataFrame from the data
df = pd.DataFrame(data_list)

*'vector' refers to a row in the dataset


Step 6: Create a plot

Using the Matplotlib Python library, create a plot based on the retrieved data and save it as a PNG file:

# Convert 'Date' column to datetime
df['Date'] = pd.to_datetime(df['Date'], format='%d-%m-%Y')

# Convert 'Temperature' column to numeric
df['Temperature'] = pd.to_numeric(df['Temperature'])

# Plotting the line plot
plt.plot(df['Date'], df['Temperature'], marker='o', linestyle='-')
plt.title('Temperature Trends Over Time')
plt.xlabel('Date')
plt.ylabel('Temperature (°C)')
plt.grid(True)

# Use DateFormatter to format the date labels
date_format = DateFormatter('%m-%d')
plt.gca().xaxis.set_major_formatter(date_format)

# Use DayLocator to set the locator for the x-axis to only consider dates
plt.gca().xaxis.set_major_locator(DayLocator())

# Save the plot to a PNG file
plt.savefig('temperature_trends.png')

Step 7: Upload the PNG file to Labguru

Upload the created plot to the dataset page:

#Upload the atachment to Labguru:
file_name = 'temperature_trends.png'

with open(file_name, 'rb') as file:
attachment = file.read()

attachment_reponse = requests.post(f'{base()}/api/v1/attachments',
data={
'item[title]': file_name,
'item[attachable_type]': 'LgData::Dataset',
'item[attach_to_uuid]': dataset['uuid'],
'token': token(),
},
files={'item[attachment]': (file_name, attachment)},)
attachment_id = attachment_reponse.json()['id']

Step 8: Delete the previous visualization

In case the visualization tab is constantly being updated, make sure to delete the existing plot and replace it with the new one (otherwise the plots will accumulate under the "Visualization" tab; If this is the desired outcome, you can skip this step)

#Delete previous plot from the "Visualization" tab

#1 - Get the ID of the previous visualization:
get_vis = requests.get(f'{base()}/api/v1/visualizations?&filter={{"dataset_id":"249"}}', json={"token": token()})
vis_id = get_vis.json()[0]["id"]

#2 - Delete the previous visualization
delete_prev_vis = requests.delete(f'{base()}/api/v1/visualizations/{vis_id}', json={"token": token()})
print ("previous visualization deleted", delete_prev_vis.status_code)

Step 9: Post the visualization to the dataset

Use the API visualization endpoint to place the created plot under the "Visualization" tab of the dataset:

#Add the created plot to the visualization tab of the dataset
vis = requests.post(f'{base()}/api/v1/visualizations', json = {
'token': token(),
'item':{
'name':"temperature_trends",
"dataset_id":dataset['id'],
"attachment_id":attachment_id
}
})

print ("visualization created", vis.status_code)

Step 10: Go to the 'Visualization' tab to see the outcome:

For further information on how to use Labguru's Workflow Editor refer to our Workflow Editor User Manual

Did this answer your question?