Skip to content

Quickstart

Emil Wagman edited this page Nov 28, 2023 · 17 revisions

Quickstart

This quickstart guide will include some quick and easy examples to get started but for more detailed documentation, check out the API reference page.

Import these essential packages before continuing:

import numpy as np
import wplt

Plotting some coordinates

let's start off by plotting some points onto a graph and see what the result will look like. Coordinates are split into two lists with x-coordinates in the first, and y-coordinates in the other.

wplt.plot([1, 2, 3, 4], [1, 3, 2, 5])
wplt.preview()

You can replace .preview() with .save() to output an image file with figure on it.

Plotting with numpy

Most commonly you'd want to plot Numpy arrays so let's try that out. For more information regarding Numpy check out the Numpy docs.

X = np.linspace(0, 10, 101)
Y = X**2

wplt.plot(X, Y) 
wplt.preview()  

Multiple plots

Within a single figure you can have however many plots you want.

X = np.linspace(0, 2*np.pi, 100)
Y1 = np.sin(X)
Y2 = np.cos(X)

wplt.plot(X, Y1) 
wplt.plot(X, Y2)
wplt.preview()  

Custom figures

When your projects get more complex it can be useful to split your graphs into multiple separate figures that you can preview and save separetly. This also allows you to customize your figure with figsize which adjusts the outputted image.

import wplt
import numpy as np

X = np.linspace(0, 2*np.pi, 100)
Y = np.cos(X)

fig = wplt.Figure(figsize=(8, 8))
fig.plot(X, Y) 
fig.preview()

Adding details to a figure

The figures so far have been quite dull and devoid of details, which makes the figures kind of hard to read. Adding a background grid to draw your to coordinates, and axes to ground the data, among other things, would help improve it.

In WPLT, these are disabled by default to allow for picking-and-choosing while also creating more intentionality in what should and should not be displayed.

X = np.linspace(-np.pi, np.pi, 100)
Y = np.sin(X)

fig = wplt.Figure()
fig.plot(X, Y, label="Plot 1")  
fig.plot(X, Y*2, label="Plot 2") 

fig.xaxis()   # Display x-axis
fig.yaxis()   # Display y-axis
fig.grid(1, 1)    # Display background grid

# Display legend (displays plot labels in top-left corner. Requires labelling)
fig.legend()

# Title the figure
fig.title("Demo of detailed figure")

fig.preview()    

Animating graph via TimeSeries

Animating a graph can added a bit of visual flair, while also adding an extra dimension of information. Included with WPLT and its more classical (Matlab-like) graphical plotting, are TimeSeries, which allow you to add a time-based variable into your plots, that interpolate from some set start value to some other end value. They are meant to be used together with numpy arrays, and support all of the basic arithmetic operations, while allowing for the use of functions with TimeSeries.apply(func, *args).

TimeSeries do not support .preview() so instead we have to save it onto disk, and then view it.

import numpy as np
import wplt

c = wplt.TimeSeries(0, 4)
X = np.linspace(0, 4*np.pi, 100)
Y = wplt.TimeSeries.apply(np.sin, c*X)

wplt.plot(X, Y)
wplt.save()

Drawing with vector coordinates over time

Because X and Y are seperated from each other and do not in actuality have to depend on each other, we can plot data that isn't dependent on X, and instead think in terms of generating coordinates. Here is an example using vector coordinates to draw an animated circle, using TimeSeries to interpolate from the angle 0 to 2*pi.

import numpy as np
import wplt

phi = wplt.TimeSeries(0, np.pi*2)
phi_space = wplt.TimeSeries.apply(np.linspace, 0, phi, 100)
X = wplt.TimeSeries.apply(np.cos, phi_space)
Y = wplt.TimeSeries.apply(np.sin, phi_space)

wplt.plot(X, Y)
wplt.save()

API reference

For more details regarding WPLT and its functionality, head over to the API reference

Clone this wiki locally