Plotly

Plotly is a python library used for data visualization.
That's all there is to it. 
And while the most commonly used data visualization tool is MatPlot, we'll be using Plotly simply because it's newer, more interactive, provides more features and is usually much more attractive (this one is actually the main reason I'm using this :D )
For Example, here's a sample graph from their own website 


Later in the article, we'll see how to make it ourselves, so keep on reading...
This is something I made..(PS. You can interact with it)



Alright! 

Now let's begin with installing the necessary package, i.e., Plotly
I'm sure that by now, you already have some decent  experience on working with environments and downloading packages and libraries.
If you don't, or if you just need a reference, check out the following link.

Now the basics.

Plotting in Plotly follows a similar (almost same actually...wait, that's what similar means....yeah, so... similar) pattern :
  1. You initialize a graph object
  2. You define the type of plotting that's gonna show up (also called trace)
  3. You define the layout of the overall graph (you know...basic stuff like naming the x-axis, y-axis and the title of the graph)
And that's it!


So let's start by creating a really simple graph with some randomly (but manually) chosen values of x, and y, and we'll see how to scatter those points or join them by a line.

Let's start with coding!

First we import NumPy and graph_objects from Plotly
import numpy as np
import plotly.graph_objects as go
We then set the values of x and y:
x = np.linspace(1, 8, 8)
y = np.array([12, 43, 65, 23, 25, 7, 12, 52])
Now comes the part with the graph.
We'll first initialize a graph_object with 
fig = go.Figure()
Now, we add the trace, i.e., we define the type of line/scatter that we want to show in the graph.
We do that with:
fig.add_trace(go.Scatter(x = x, y = y, mode = 'lines'))
x should be equal to the x values (and we've named them x)
Similarly y = y values (and we've named them y in our case)
mode can be either 'markers' or 'lines' or 'lines+markers'
'markers' means it'll only plot the points.
'lines' would make lines joining all the points
'lines+markers' would do both.

Now, we'll update the default layout of the graph with 
fig.update_layout(xaxis_title = 'x', yaxis_title = 'y', title = 'Sample Table with Scatter Points')
This one is pretty self-explanatory.
we define the x-axis title, the y-axis title and the the title of the whole graph.
And finally we'll type
fig.show()
to display the graph.

The final code, looks like this:
import numpy as np
import plotly.graph_objects as go
x = np.linspace(1, 8, 8)
y = np.array([12, 43, 65, 23, 25, 7, 12, 52])
fig = go.Figure()
fig.add_trace(go.Scatter(x = x, y = y, mode = 'lines'))
fig.update_layout(xaxis_title = 'x', yaxis_title = 'y', title = 'Sample Table with Lines')
fig.show()
And it will produce a graph that looks something like this:



If you want to overlay another trace onto it, just add another trace!
Lemme just add another trace to the same graph and show you the result.
I'll add 
fig.add_trace(go.Scatter(x = x, y = y_2, mode = 'markers', marker = dict(color = x, colorscale = 'Viridis', size = 10, opacity = 0.7), name ="Trace 2"))

So now the whole code is :
import numpy as np
import plotly.graph_objects as go
x = np.linspace(1, 8, 8)
y = np.array([12, 43, 65, 23, 25, 7, 12, 52])
y_2 = np.array([56, 29, 7, 34, 49, 12, 1, 45])
fig = go.Figure()
fig.add_trace(go.Scatter(x = x, y =y, name = 'Trace 1'))
fig.add_trace(go.Scatter(x = x, y = y_2, mode = 'markers', marker = dict(color = x, colorscale = 'Viridis', size = 10, opacity = 0.7), name = 'Trace 2'))
fig.update_layout(xaxis_title = 'x', yaxis_title = 'y', title = 'Sample Table with Lines and Scatter Points')
fig.show()

And voila!

We have the new graph!

Now if you noticed, there are a lot if new attributes that I added this time, that I've not explained before.
So allow me explain

1. When you don't select any type of mode, the default mode would be selected, i.e., lines+markers.
2. When you select the mode as 'markers', you can also decide the properties of the markers and decide them using a dictionary. The color dictates along what direction will the graph be coloured. The colorscale decides the colour theme. And size and opacity decide the size the....guess what!??...size and opacity of the markers.
Now when we have 2 traces, we an label them separately with the name attribute, here I've named the first trace as "Trace 1" and second one as "Trace 2"

Lemme just show you a couple more codes,  just to reinforce the learning and get you comfortable with this whole thing
import numpy as np
import plotly.graph_objects as go
x = np.linspace(-10,10, 1000)
y = 1/(1+np.exp(-x))
fig = go.Figure()
fig.add_trace(go.Scatter(x = x, y = y))
fig.update_layout(xaxis_title = "z", yaxis_title = "sigmoid(z)", title = 'Sigmoid Function')
fig.show()
The above code will result in:

I understand I haven't mentioned np.linspace before, so let me tell you now.
np.linspace(<starting_number>, <ending_number>, <n>)
will give you n numbers from <starting number> to <ending_number> with equal intervals between them.

Alright now
Following a similar pattern, let's just draw 4 more activation functions together!
Write the following code:
import plotly.graph_objs as go
import numpy
x = np.linspace(-2, 2, 1000)
y1 = 1/(1+np.exp(-x))
y2 = (np.exp(x)- np.exp(-x))/(np.exp(x) + np.exp(-x))
y3 = np.maximum(0, x)
y4 = np.maximum(0.1*x, x)
fig = go.Figure()
fig.add_trace(go.Scatter(x = x, y = y1, name = "sigmoid"))
fig.add_trace(go.Scatter(x = x, y = y2, name = "tanh"))
fig.add_trace(go.Scatter(x = x, y = y3, name = "ReLU"))
fig.add_trace(go.Scatter(x = x, y = y4, name = "Leaky ReLU"))
fig.update_layout(xaxis_title = "z", yaxis_title = "g(z)", title = "Activation Functions")
fig.show()
The graph should be something like this:

The ReLU and Leaky ReLU overlap after z = 0. and are therefore, indistinguishable in the graph, but you can show and hide different traces by clicking on them on the upper right portion where all of them are mentioned.

Now, following the same pattern, let's transition to 3-D graphs.
The only difference is that, now we'd be using Scatter3d, instead of just Scatter, and that there'd be an extra parameter to take another input z.

Let's just dive straight onto the code.

import chart_studio.plotly as py
import plotly.graph_objs as go
import numpy as np
import chart_studio as cs
x = np.linspace(-100, 100, 500)
y = np.sin(x)
z = np.cos(x)
fig = go.Figure()
fig.add_trace(go.Scatter3d(x = x, y = y, z = z, mode = 'markers', marker = dict(color = x, colorscale = 'Viridis', opacity = 0.5)))
fig.show()

And just like that, we have that same graph that was shown off earlier.

And to finish it off with the promised graph, that was shown in the thumbnail, type this:
import plotly.graph_objects as go

import pandas as pd
# Read data from a csv
z_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv')
fig = go.Figure()
fig.add_trace(go.Surface(z=z_data.values))
fig.update_layout(title='Mt Bruno Elevation')
fig.show()



I sincerely hope you had as much fun making these graphs and playing with them, as much as I had exploring them and writing them here.

Attaching these graphs into a blog like this would require a another blog post of its own.
If you're interested, Hit Me Up Fam!
Until next time
Cheers!
Satwik

Previous Post : NumPy
Next Post : Notations

Comments

Post a Comment

Popular posts from this blog

Solving Sudoku

Computing Expressions