Dataframes and plotly

Introduction

As your project submissions will be in the form of a Jupyter notebook, it is important to fully utilize all available open-source python libraries towards your project. Here, we briefly run through plotly. As always, we begin with a few import statements; some of these are to ensure that the plots will generate correctly when we convert this notebook into an html file.

Code
import plotly.express as px
import numpy as np
import plotly.io as pio
import plotly.graph_objects as go
import pandas as pd
pio.renderers.default = 'iframe'

Scatter plots

We begin with a very simple plot of \(y=sin\left(t \right)\) where \(t \in \left[0, 10 \right]\).

Code
t = np.linspace(0, 10, 300)
fig = px.scatter(x=t, y=np.sin(3 * t), labels={'x':'t', 'y':'sin(3t)'}) 
fig.show()

If we had multiple items that needed to be plotted, we would need to resort to a dataframe.

Code
df = pd.DataFrame({'t':t, 'y_1': np.sin(t), 'y_2': np.cos(t)})
fig = px.scatter(df, x='t', y=df.columns[1:3]) 
fig.show()

Contour plots

Next, we will generate a contour plot! This requires us to define a grid along the horizontal and vertical axes, and a function that is defined at each grid point.

Code
feature_x = np.arange(0, 50, 2) 
feature_y = np.arange(0, 50, 3) 
 
# Creating 2-D grid of features 
[X, Y] = np.meshgrid(feature_x, feature_y) 
 
Z = np.cos(X / 4) + np.sin(Y / 4) + np.exp(0.0005 * X * Y)
 
fig = go.Figure(data =
     go.Contour(x = feature_x, y = feature_y, z = Z))
 
fig.show()