Creating Charts and Plots
Visualize data with line, bar, scatter, and more
Equana includes a comprehensive plotting library for creating visualizations directly in your notebook. Charts are rendered inline using Plotly.js, supporting line plots, bar charts, scatter plots, histograms, and pie charts — all with customizable labels, legends, and styling.
In this tutorial, you'll learn to create and customize charts, building from simple plots to complete visualizations. Variables persist between cells, so you can build on previous examples as you progress.
Line Plots
The plot() function is the most common way to visualize data. With a single argument, it plots y-values against indices 1, 2, 3, etc. With two arguments, it pairs x and y values.
# Simple line plot with automatic x-values
y = [1, 4, 9, 16, 25]
plot(y)
title("Squares")Explicit X and Y Data
For more control, provide both x and y data. This is useful for plotting mathematical functions.
# Create x values from 0 to 2π
x = linspace(0, 2*pi, 50)
# Plot sine wave
plot(x, sin(x))
title("Sine Wave")
xlabel("Angle (radians)")
ylabel("Amplitude")Multiple Data Series
Multiple plot() calls add data series to the same chart. Use legend() to label each series. Note that the x variable from the previous cell is still available.
# Plot both sine and cosine using the same x values
plot(x, sin(x))
plot(x, cos(x))
# Add a legend to identify each series
legend("sin(x)", "cos(x)")
title("Trigonometric Functions")Chart Types
Beyond line plots, Equana supports bar charts, scatter plots, histograms, and pie charts.
| Function | Description | Example |
|---|---|---|
plot(x, y) | Line chart | plot(x, sin(x)) |
bar(x, y) | Bar chart | bar([1, 2, 3, 4]) |
scatter(x, y) | Scatter plot | scatter(x, y) |
histogram(data, bins) | Histogram | histogram(randn(100, 1)) |
pie(data) | Pie chart | pie([30, 20, 50]) |
Bar Charts
Bar charts are ideal for categorical data or comparing discrete values.
# Monthly sales data
values = [25, 40, 30, 55, 45]
bar(values)
title("Monthly Sales")
xlabel("Month")
ylabel("Units Sold")Scatter Plots
Scatter plots show the relationship between two variables without connecting the points.
# Generate random data points
x_scatter = rand(30, 1) * 10
y_scatter = rand(30, 1) * 10
scatter(x_scatter, y_scatter)
title("Random Scatter")
xlabel("X Values")
ylabel("Y Values")Histograms
Histograms show the distribution of data. You can optionally specify the number of bins.
# Generate normally distributed data
data = randn(500, 1)
# Create histogram with default bins
histogram(data)
title("Normal Distribution")
xlabel("Value")
ylabel("Frequency")Pie Charts
Pie charts show proportions of a whole.
# Market share data
shares = [35, 25, 20, 15, 5]
pie(shares)
title("Market Share")
legend("Product A", "Product B", "Product C", "Product D", "Other")Labels and Titles
Add context to your charts with titles, axis labels, legends, and text annotations.
| Function | Description | Example |
|---|---|---|
title(str) | Set chart title | title("My Chart") |
xlabel(str) | Set x-axis label | xlabel("Time (s)") |
ylabel(str) | Set y-axis label | ylabel("Value") |
legend("a", "b", ...) | Add legend | legend("sin", "cos") |
text(x, y, str) | Add text annotation | text(2, 5, "Peak") |
Text Annotations
Use text(x, y, str) to place labels at specific data coordinates.
# Plot sine wave and annotate key points
x = linspace(0, 2*pi, 50)
plot(x, sin(x))
# Add text at specific coordinates
text(pi/2, 1, "Maximum")
text(3*pi/2, -1, "Minimum")
title("Annotated Sine Wave")Axis Control
Control axis ranges, tick marks, and grid lines for precise chart formatting.
| Function | Description | Example |
|---|---|---|
xlim([min, max]) | Set x-axis limits | xlim([0, 10]) |
ylim([min, max]) | Set y-axis limits | ylim([-1, 1]) |
axis([xmin, xmax, ymin, ymax]) | Set all limits | axis([0, 10, -1, 1]) |
xticks(values) | Set x tick positions | xticks([0, 5, 10]) |
yticks(values) | Set y tick positions | yticks([-1, 0, 1]) |
grid("on"/"off") | Toggle grid lines | grid("on") |
# Plot with custom axis limits and grid
plot(x, sin(x))
# Set custom axis limits
xlim([0, 2*pi])
ylim([-1.5, 1.5])
# Enable grid
grid("on")
title("Sine with Custom Limits and Grid")Custom Tick Marks
Control exactly where tick marks appear on the axes.
# Plot with custom tick positions
plot(x, sin(x))
# Set custom tick positions
xticks([0, pi/2, pi, 3*pi/2, 2*pi])
yticks([-1, -0.5, 0, 0.5, 1])
title("Custom Tick Marks")Figures and Subplots
Create multiple separate charts with figure(), or arrange charts in a grid with subplot().
| Function | Description | Example |
|---|---|---|
figure(n) | Create/switch to figure | figure() |
subplot(rows, cols, idx) | Create subplot grid | subplot(2, 2, 1) |
hold("on"/"off") | Overlay multiple plots | hold("on") |
yyaxis("left"/"right") | Dual y-axis control | yyaxis("right") |
clf | Clear current figure | clf() |
close(fig) | Close figure | close() |
Multiple Figures
Use figure() to start a new, separate chart.
# First figure - line plot
plot([1, 2, 3, 4])
title("Figure 1: Linear")
# Start a new figure
figure()
# Second figure - bar chart (separate from the first)
bar([4, 3, 2, 1])
title("Figure 2: Bar Chart")Subplot Grids
Use subplot(rows, cols, index) to create a grid of charts. The index is 1-based and counts in row-major order.
# Create a 2x2 grid of charts
# Top-left (position 1)
subplot(2, 2, 1)
plot([1, 4, 9, 16])
title("Line")
# Top-right (position 2)
subplot(2, 2, 2)
bar([3, 7, 2, 5])
title("Bar")
# Bottom-left (position 3)
subplot(2, 2, 3)
scatter(rand(20, 1), rand(20, 1))
title("Scatter")
# Bottom-right (position 4)
subplot(2, 2, 4)
histogram(randn(100, 1))
title("Histogram")Advanced Features
Dual Y-Axis Charts
Compare data with different scales using yyaxis() to plot on left and right y-axes.
# Create x values
x = linspace(0, 10, 50)
# Plot on left y-axis (default)
plot(x, sin(x))
ylabel("Sine")
# Switch to right y-axis
yyaxis("right")
plot(x, x .^ 2)
ylabel("Quadratic")
title("Dual Y-Axis Chart")
xlabel("X")Overlaying Different Chart Types
Use hold("on") to overlay different chart types on the same axes.
# Line plot
plot([1, 2, 3, 4, 5])
# Keep current chart and add bar overlay
hold("on")
bar([0.5, 1, 1.5, 2, 2.5])
legend("Line", "Bar")
title("Mixed Chart Types")Complete Example
Let's put it all together with a comprehensive visualization combining multiple features.
# Generate data
x = linspace(0, 4*pi, 100)
y1 = sin(x)
y2 = cos(x)
# Create plot with multiple series
plot(x, y1)
plot(x, y2)
# Add labels and legend
title("Trigonometric Functions")
xlabel("Angle (radians)")
ylabel("Value")
legend("sin(x)", "cos(x)")
# Customize axis
xlim([0, 4*pi])
ylim([-1.2, 1.2])
# Add grid
grid("on")Statistical Analysis Visualization
Using subplots to show data distribution alongside summary statistics.
# Generate sample data
data = randn(200, 1) * 15 + 50
# Create 2x1 subplot grid
subplot(2, 1, 1)
histogram(data, 15)
title("Distribution of Values")
xlabel("Value")
ylabel("Count")
subplot(2, 1, 2)
bar([mean(data), min(data), max(data)])
title("Summary Statistics")
legend("Mean", "Min", "Max")Summary
You've learned how to create and customize charts in Equana:
Plotting Functions
plot(x, y)— Line charts for continuous databar(x, y)— Bar charts for categorical datascatter(x, y)— Scatter plots for relationshipshistogram(data)— Distribution visualizationpie(data)— Proportional data
Customization
- Labels:
title(),xlabel(),ylabel(),legend(),text() - Axes:
xlim(),ylim(),axis(),xticks(),yticks(),grid() - Layout:
figure(),subplot(),hold(),yyaxis()
Tips
- Multiple
plot()calls automatically merge into one chart - Use
figure()to start a new, separate chart - Different chart types auto-separate unless you use
hold("on") - Variables persist between cells — reuse
x,data, etc.
Further Reading
- Plotting Package Reference — Complete function documentation
- 3D Surface Plots Tutorial — Mesh and surf plots