Skip to content

Commit

Permalink
feat: update api spec to handle multi-plot with multiple layers (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
SaaiVenkat authored Sep 13, 2024
1 parent c930c79 commit 75e103b
Show file tree
Hide file tree
Showing 8 changed files with 2,331 additions and 70 deletions.
144 changes: 74 additions & 70 deletions api/latest.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,51 +30,40 @@
"properties": {
"id": {
"type": "string",
"example": "container-figure-id"
"example": "figure-id"
},
"selector": {
"library": {
"type": "string",
"example": ""
"example": "matplotlib"
},
"coordinate": {
"type": "string",
"example": "cartesian"
},
"layout": {
"type": "object",
"properties": {
"rows": {
"type": "integer",
"example": 3
"example": 2
},
"columns": {
"type": "integer",
"example": 2
"example": 3
}
}
},
"title": {
"type": "string",
"example": "Penguin Bill Dimensions by Species and Sex"
"example": "figure-title"
},
"axes": {
"type": "object",
"properties": {
"x": {
"type": "object",
"properties": {
"label": {
"type": "string",
"example": "Bill Length (mm)"
}
}
},
"y": {
"type": "object",
"properties": {
"label": {
"type": "string",
"example": "Bill Depth (mm)"
}
}
}
}
"subtitle": {
"type": "string",
"example": "R-figure-subtitle"
},
"caption": {
"type": "string",
"example": "R-figure-caption"
},
"panels": {
"type": "array",
Expand All @@ -83,66 +72,81 @@
"properties": {
"id": {
"type": "string",
"example": "panel-01"
},
"selector": {
"type": "string",
"example": ""
},
"type": {
"type": "string",
"description": "Specifies the type of the item, e.g., 'point', 'bar', or 'line'.",
"enum": [
"bar",
"hist",
"heat",
"box",
"point",
"line",
"stacked",
"dodged"
],
"example": "point"
"example": "panel-id"
},
"title": {
"type": "string",
"example": "Adelie - Female"
"example": "panel-title"
},
"axes": {
"type": "object",
"properties": {
"x": {
"type": "object",
"properties": {
"label": {
"type": "string",
"example": "Bill Length (mm)"
}
}
"type": "string",
"example": "x-axis"
},
"y": {
"type": "object",
"properties": {
"label": {
"type": "string",
"example": "Bill Depth (mm)"
}
}
"type": "string",
"example": "y-axis"
},
"z": {
"type": "string",
"example": "z-axis"
}
}
},
"data": {
"legend": {
"type": "object",
"additionalProperties": {
"type": "string"
},
"example": {
"gentoo": "red",
"adelie": "green"
}
},
"layers": {
"type": "array",
"items": {
"type": "object",
"properties": {
"x": {
"type": "number",
"example": 39.5
"type": {
"type": "string",
"description": "Layer type, e.g., 'bar', 'line', 'scatter', etc.",
"enum": [
"bar",
"hist",
"heat",
"box",
"point",
"line",
"stacked",
"dodged"
],
"example": "layer-type"
},
"y": {
"type": "number",
"example": 17.4
"selector": {
"type": "array",
"items": {
"type": "string",
"example": "selectors"
}
},
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"x": {
"type": "number",
"example": 39.5
},
"y": {
"type": "number",
"example": 17.4
}
}
}
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions examples/multi-layer/multi-layer-seaborn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Load example dataset
tips = sns.load_dataset("tips")

# Prepare data for the bar chart (average total bill by day)
avg_bill = tips.groupby('day')['total_bill'].mean().reset_index()

# Prepare data for the line chart (average tip by day)
avg_tip = tips.groupby('day')['tip'].mean().reset_index()

# Start plotting
plt.figure(figsize=(10, 6))
sns.set(style="whitegrid")

# Create bar chart
bar = sns.barplot(x='day', y='total_bill', data=avg_bill, color='lightblue', label='Average Total Bill')

# Create line chart
line = sns.lineplot(x='day', y='tip', data=avg_tip, sort=False, marker='o', color='red', label='Average Tip')

# Adding legend
plt.legend()

# Show the plot
plt.show()
66 changes: 66 additions & 0 deletions examples/multi-layer/mutli-layer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"id": "figure-id",
"library": "seaborn",
"coordinate": "cartesian",
"layout": {
"rows": 1,
"columns": 1
},
"title": "Average Bill and Tip by Day of Week",
"panels": [
{
"id": "panel-01",
"title": "Bar and Line Chart",
"axes": {
"x": "Day of the Week",
"y": "Amount in USD"
},
"layers": [
{
"type": "bar",
"selector": [],
"data": [
{
"x": "Thur",
"y": 17.68
},
{
"x": "Fri",
"y": 17.15
},
{
"x": "Sat",
"y": 20.44
},
{
"x": "Sun",
"y": 21.41
}
]
},
{
"type": "line",
"selector": [],
"data": [
{
"x": "Thur",
"y": 2.77
},
{
"x": "Fri",
"y": 2.73
},
{
"x": "Sat",
"y": 2.99
},
{
"x": "Sun",
"y": 3.26
}
]
}
]
}
]
}
65 changes: 65 additions & 0 deletions examples/multi-panel/multi-panel-R.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Load the required libraries
library(ggplot2)
library(palmerpenguins)
library(svglite)

# Load the penguins dataset
data("penguins")
penguins <- na.omit(penguins)

# Create a ggplot
p <- ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm)) +
geom_point() +
facet_grid(species ~ sex) +
labs(
title = "Penguin Bill Dimensions by Species and Sex",
x = "Bill Length (mm)",
y = "Bill Depth (mm)"
) +
theme(
plot.title = element_text(size = 16, hjust = 0.5),
strip.text = element_text(size = 12),
axis.title = element_text(size = 12)
)

# Save the plot as an SVG file using ggsave and svglite
ggsave("multi-panel.svg", plot = p, device = svglite, width = 10, height = 8)

# Extract raw data used for plotting per panel
plot_data <- ggplot_build(p)$data[[1]]
# Split the data by panel
plot_data_split <- split(plot_data, plot_data$PANEL)

# Save each panel data as a separate data frame object
panel_data_list <- lapply(names(plot_data_split), function(panel) {
panel_data <- plot_data_split[[panel]]
assign(paste0("panel_data_", panel), panel_data, envir = .GlobalEnv)
return(panel_data)
})

# Assign names to the list elements for easier reference
names(panel_data_list) <- names(plot_data_split)

# Load the required library for JSON
library(jsonlite)

# Save each panel data as a JSON file containing only x and y variables
lapply(names(panel_data_list), function(panel) {
panel_data <- panel_data_list[[panel]][, c("x", "y")]
json_file <- paste0("panel_data_", panel, ".json")
write_json(panel_data, json_file)
})



# Extract the layout information to get the panel titles
layout_info <- ggplot_build(p)$layout$layout

# Create a data frame that maps panel numbers to species and sex
panel_mapping <- data.frame(
PANEL = layout_info$PANEL,
species = layout_info$species,
sex = layout_info$sex
)

print(panel_mapping)
36 changes: 36 additions & 0 deletions examples/multi-panel/multi-panel-matplotlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import matplotlib.pyplot as plt
import seaborn as sns

# Load the penguins dataset using seaborn
penguins = sns.load_dataset("penguins")

# Get unique species and sexes
species = penguins["species"].dropna().unique()
sexes = penguins["sex"].dropna().unique()

# Create a figure and a grid of subplots
fig, axes = plt.subplots(
nrows=len(species), ncols=len(sexes), figsize=(12, 8), sharex=True, sharey=True
)

# Set a global title for the figure
fig.suptitle("Penguin Bill Dimensions by Species and Sex", fontsize=16)

# Loop through species and sexes to create each subplot
for i, sp in enumerate(species):
for j, sex in enumerate(sexes):
ax = axes[i, j]
subset = penguins[(penguins["species"] == sp) & (penguins["sex"] == sex)]
ax.scatter(subset["bill_length_mm"], subset["bill_depth_mm"])

# Set panel-specific titles
ax.set_title(f"{sp} - {sex}", fontsize=12)

# Set x and y labels for the leftmost and bottom plots only
if i == len(species) - 1:
ax.set_xlabel("Bill Length (mm)")
if j == 0:
ax.set_ylabel("Bill Depth (mm)")

# Show the plot
plt.show()
Loading

0 comments on commit 75e103b

Please sign in to comment.