OpenAI Canvas is a versatile tool designed to optimize collaborative coding and text editing. With its intuitive interface, Canvas offers a dynamic platform for developers to write, edit and debug code along with ChatGPT's ai-powered assistance. This makes it particularly useful for a wide range of tasks, from basic scripting to complex project management. In this article, I will explore coding with Canvas and share my overall experience.
Key Features and Benefits of Canvas
- Perfect collaboration: Canvas integrates conversational interfaces, allowing users to modify, request feedback, or explore ideas in real time without switching tools.
- Dynamic coding environment: Designed for Python developers, Canvas supports code execution, making it ideal for tasks like data analysis, coding, and visualization.
- Multifunctional platform: Canvas is not just for editing text; It's a versatile space for brainstorming, coding, and structured workflows.
Pay: Why is the o1 model better than GPT-4o?
Practical Coding with Canvas
Getting started
I explored Canvas's coding capabilities with the goal of attaching a data set and performing basic exploratory data analysis (EDA). While the interface was intuitive and promising, I encountered challenges when integrating external data sets.
The challenge: accessing data in the Sandbox
When trying to attach a data set, I discovered that the sandbox environment could not access the file. As a workaround, I pasted a subset of the data directly into Canvas, but this did not solve the problem. Even with the code written correctly, an error message persisted stating that the data could not be found. This limitation highlights the need for improved data integration capabilities within Canvas.
Synthetic data and visualizations
After encountering these limitations, I asked Canvas to generate synthetic data and perform visualizations. The following code was generated and executed successfully:
Prompt: Create a dataset and then do some visualizations on it.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# Step 1: Create a dataset
np.random.seed(42)
data = {
'Category': np.random.choice(('A', 'B', 'C', 'D'), 100),
'Value1': np.random.normal(50, 10, 100),
'Value2': np.random.normal(100, 20, 100),
'Group': np.random.choice(('x', 'Y'), 100)
}
df = pd.DataFrame(data)
# Step 2: Display basic information about the dataset
print("Dataset preview:")
print(df.head())
print("\nDataset summary:")
print(df.describe())
# Step 3: Visualizations
plt.figure(figsize=(12, 8))
# Bar plot: Average of Value1 by Category
plt.subplot(2, 2, 1)
sns.barplot(x='Category', y='Value1', data=df, ci=None)
plt.title('Average of Value1 by Category')
# Box plot: Distribution of Value2 by Group
plt.subplot(2, 2, 2)
sns.boxplot(x='Group', y='Value2', data=df)
plt.title('Distribution of Value2 by Group')
# Scatter plot: Value1 vs. Value2
plt.subplot(2, 2, 3)
sns.scatterplot(x='Value1', y='Value2', hue="Category", data=df)
plt.title('Scatter plot of Value1 vs Value2')
# Histogram: Distribution of Value1
plt.subplot(2, 2, 4)
sns.histplot(df('Value1'), kde=True, bins=20)
plt.title('Histogram of Value1')
plt.tight_layout()
plt.show()
And when I ran it, this was the result:
The results were visually informative and showed the platform's ability to handle basic EDA tasks effectively. After this, I performed advanced EDA to discover deeper insights:
While running this advanced EDA, I got this error:
After fixing the error:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# Step 1: Create a dataset
np.random.seed(42)
data = {
'Category': np.random.choice(('A', 'B', 'C', 'D'), 100),
'Value1': np.random.normal(50, 10, 100),
'Value2': np.random.normal(100, 20, 100),
'Group': np.random.choice(('x', 'Y'), 100)
}
df = pd.DataFrame(data)
# Step 2: Display basic information about the dataset
print("Dataset preview:")
print(df.head())
print("\nDataset summary:")
print(df.describe())
# Advanced EDA
print("\nChecking for missing values:")
print(df.isnull().sum())
# Ensure only numeric data is used for correlation matrix
print("\nCorrelation matrix:")
numeric_df = df.select_dtypes(include=(np.number))
correlation_matrix = numeric_df.corr()
print(correlation_matrix)
# Visualizations for advanced EDA
plt.figure(figsize=(15, 12))
# Heatmap of correlation matrix
plt.subplot(3, 2, 1)
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f")
plt.title('Correlation Matrix Heatmap')
# Pairplot for relationships
sns.pairplot(df, hue="Category", corner=True, diag_kind='kde')
plt.suptitle('Pairplot of Variables', y=1.02)
plt.show()
# Boxen plot: Distribution of Value1 by Category and Group
plt.subplot(3, 2, 2)
sns.boxenplot(x='Category', y='Value1', hue="Group", data=df)
plt.title('Boxen plot of Value1 by Category and Group')
# Violin plot: Distribution of Value2 by Category
plt.subplot(3, 2, 3)
sns.violinplot(x='Category', y='Value2', data=df, hue="Group", split=True)
plt.title('Violin plot of Value2 by Category')
# Count plot: Frequency of Categories
plt.subplot(3, 2, 4)
sns.countplot(x='Category', data=df, hue="Group")
plt.title('Frequency of Categories by Group')
# KDE plot: Distribution of Value1 and Value2
plt.subplot(3, 2, 5)
sns.kdeplot(x='Value1', y='Value2', hue="Category", data=df, fill=True, alpha=0.6)
plt.title('KDE plot of Value1 vs Value2')
plt.tight_layout()
plt.show()
# Outlier detection
print("\nIdentifying potential outliers:")
for column in ('Value1', 'Value2'):
Q1 = df(column).quantile(0.25)
Q3 = df(column).quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
outliers = df((df(column) < lower_bound) | (df(column) > upper_bound))
print(f"Outliers in {column}:\n", outliers)
# Group statistics
print("\nGroup statistics:")
print(df.groupby(('Category', 'Group')).agg({'Value1': ('mean', 'std'), 'Value2': ('mean', 'std')}))
These advanced analyzes highlighted Canvas' capabilities for exploratory tasks, but also highlighted the platform's limitations for integrating external data sets.
Transfer code to other languages
While Canvas primarily supports coding in Python, the platform allows users to port Python code to other languages, such as Java. However, it does not run code in languages other than Python. Below is an example of a port from Python to Java:
import java.util.*;
import java.util.stream.Collectors;
public class DatasetVisualization {
public static void main(String() args) {
// Step 1: Create a synthetic dataset
Random random = new Random(42); // For reproducibility
List ages = random.ints(200, 18, 70).boxed().collect(Collectors.toList());
List incomes = random.ints(200, 30000, 120000).boxed().collect(Collectors.toList());
List genders = random.ints(200, 0, 2).mapToObj(i -> i == 0 ? "Male" : "Female").collect(Collectors.toList());
List spendScores = random.ints(200, 1, 101).boxed().collect(Collectors.toList());
List cities = random.ints(200, 0, 5).mapToObj(i -> {
switch (i) {
case 0: return "New York";
case 1: return "Los Angeles";
case 2: return "Chicago";
case 3: return "Houston";
default: return "Phoenix";
}
}).collect(Collectors.toList());
// Step 2: Create demographic segments
List ageGroups = ages.stream().map(age -> {
if (age <= 30) return "Young";
else if (age <= 50) return "Middle-aged";
else return "Senior";
}).collect(Collectors.toList());
List incomeGroups = incomes.stream().map(income -> {
if (income < 40000) return "Low";
else if (income <= 70000) return "Medium";
else return "High";
}).collect(Collectors.toList());
// Step 3: Print a summary of the dataset
System.out.println("Sample of the dataset:");
for (int i = 0; i < 5; i++) {
System.out.printf("Age: %d, Income: %d, Gender: %s, Spend Score: %d, City: %s, Age Group: %s, Income Group: %s\n",
ages.get(i), incomes.get(i), genders.get(i), spendScores.get(i), cities.get(i), ageGroups.get(i), incomeGroups.get(i));
}
// Step 4: Perform a correlation-like analysis (simplified for Java)
double ageIncomeCorrelation = calculateCorrelation(ages, incomes);
double ageSpendScoreCorrelation = calculateCorrelation(ages, spendScores);
double incomeSpendScoreCorrelation = calculateCorrelation(incomes, spendScores);
System.out.println("\nCorrelation Analysis:");
System.out.printf("Age-Income Correlation: %.2f\n", ageIncomeCorrelation);
System.out.printf("Age-Spend Score Correlation: %.2f\n", ageSpendScoreCorrelation);
System.out.printf("Income-Spend Score Correlation: %.2f\n", incomeSpendScoreCorrelation);
// Visualizations would typically require a separate library for Java, such as JFreeChart or JavaFX.
System.out.println("\nVisualizations are not implemented in this text-based example.");
}
// Helper method to calculate a simplified correlation
private static double calculateCorrelation(List x, List y) {
if (x.size() != y.size()) throw new IllegalArgumentException("Lists must have the same size");
int n = x.size();
double meanX = x.stream().mapToDouble(a -> a).average().orElse(0);
double meanY = y.stream().mapToDouble(a -> a).average().orElse(0);
double covariance = 0;
double varianceX = 0;
double varianceY = 0;
for (int i = 0; i < n; i++) {
double deltaX = x.get(i) - meanX;
double deltaY = y.get(i) - meanY;
covariance += deltaX * deltaY;
varianceX += deltaX * deltaX;
varianceY += deltaY * deltaY;
}
return covariance / Math.sqrt(varianceX * varianceY);
}
}
Although the Java code provides functionality for creating simple data sets and analysis, further development would require additional libraries for visualization.
My experience using Canvas
While Canvas supports Python, integrating external datasets can be challenging due to sandbox restrictions. However, generating synthetic data within Canvas or importing subsets of datasets can mitigate these issues. Additionally, Python code can be ported to other languages, although execution outside of Python is not supported in Canvas.
Overall, Canvas offers a collaborative and easy-to-use environment. Improving its ability to integrate external data and support more programming languages would make it even more versatile and useful.
Conclusion
Coding with ChatGPT Canvas combines ai assistance with a collaborative workspace, making it a practical tool for developers. Whether you're debugging code, analyzing data, or generating ideas, Canvas simplifies the process and increases productivity.
Have you tried coding with Canvas? Share your experiences and tell me how it worked for you in the comments section below.
Stay tuned to Analytics Vidhya blog for more such updates!
Frequently asked questions
ChatGPT Canvas is a feature that allows users to edit, collaborate, and refine large documents or code directly alongside their ChatGPT conversations.
OpenAI offers free access to some ChatGPT features, but advanced features and models often require a paid subscription.
Yes, OpenAI Canvas allows users to edit and refine code directly along with ai-powered suggestions.