Import Plotly

We are going to create a surface plot in plotly and apply a custom colorscale to the surface plot. The first step is to import modules from plotly.offline

In [1]:
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
from numpy import arange,meshgrid,sin,pi

Data prepration

This is a simple surface plot of a sin function. The domain of the plot $\Omega \in [0,\pi]\times[0,\pi]$. Meshfrid fuction creates a tensor product mesh from x and y vectors. The output of meshgrid are two matrices containing x and y co-ordinates of each point on the mesh. We then calculate the required value at each co-ordinate and store that in matrix ZZ.

In [9]:
x = arange(0, pi, 0.1)
y = arange(0, pi, 0.1)
XX, YY = meshgrid(x, y)
ZZ = sin(XX)*sin(YY)


The next step is to make the surface plot using the 'Surface()' command. By default the surface plot is are colored by the z variable. But we could make the plot using any desired variable by assigning the surfacecolor property the value of variable. Plotly would then automatically color the plot based on the values of the set variable. For demonstration purpose the plot is color scaled based on the x variable. This could be usefull to create contour plots of stress and strains.

In [15]:
trace=go.Surface(x=XX, y=YY, z=ZZ)
fig = go.Figure(data=[trace])