Lately, I’ve been working a lot on data with multiple dimensions. I’m used to work a lot with Bokeh but one of it’s limitations is 3D. Matplotlib always offers a way of plotting almost anything I can imagine, but this time I didn’t want to sacrifice the interactivity that I’m used to have in Bokeh.
The last couple of months I’ve been doing a nice Python postgrade in the UB by Ramon Crehuet and Fermin Huarte and one of the most exciting things we have been working with is a service called plotly.
I’m not a huge fan of using a proprietary service for doing research, but in this case it looked like a great way to go. It’s easy to install and easy to integrate inside the Jupyter Notebooks.
There is just one observation I would like to do, WHY is this the easiest plotting library ever ? It’s super easy and very very intuitive to use..
Set the credentials:
from plotly.tools import set_credentials_file
set_credentials_file(username='xx', api_key='xx', stream_ids=['xx', 'xx'])
Then look for the plot type you want to create, in this case a 3D Scatter made from a 3D random walker with 4 walkers:
def adimensional_random_walkers(walkers, steps, dimensions):
workers = np.zeros(walkers*dimensions)
for i in range(steps):
random_step = np.random.randint(-1, 2, size=walkers*dimensions)
workers = np.c_[workers, random_step]
return np.cumsum(workers, axis=1).reshape(walkers,dimensions,steps+1) # +1 for starting point
four_walkers = adimensional_random_walkers(4, 100, 3)
from plotly.plotly import iplot
from plotly.graph_objs import Scatter3d, Data, Marker
walkers = []
for walker in four_walkers:
trace = Scatter3d(
x=walker[0],
y=walker[1],
z=walker[2],
)
walkers.append(trace)
data = Data(walkers)
iplot(data, filename = 'four-3d-random-walkers')
Very useful to if doing a PCA and you want to check the result in a moment:
from plotly.plotly import iplot
from plotly.graph_objs import Scatter3d, Data, Marker
# First three dimensions from reduced X VS the Y
trace0 = Scatter3d(
x=Reduced_with_PCA[:, 0],
y=Reduced_with_PCA[:, 1],
z=Reduced_with_PCA[:, 2],
marker=Marker(color=Y, colorscale='Portland'),
mode='markers'
)
data = Data([trace0])
iplot(data, filename = 'pca-cloud')