0

I am researching to make a visual representation of clusters, and I have found the following source: https://plotly.com/python/v3/3d-point-clustering/#3d-clustering-with-alpha-shapes

In it you can find a code of a clustering that has been done and I wish to recreate the same in my Jupyter Notebook. However, I do not get any visual representation.

My code is as follows:

import plotly as py
!pip install plotly==3.10.0
from chart_studio import plotly
import plotly.plotly as py
import pandas as pd

jupyter labextension install jupyterlab-plotly
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/alpha_shape.csv')
df.head()

scatter = dict(
    mode = "markers",
    name = "y",
    type = "scatter3d",
    x = df['x'], y = df['y'], z = df['z'],
    marker = dict( size=2, color="rgb(23, 190, 207)" )
)
clusters = dict(
    alphahull = 7,
    name = "y",
    opacity = 0.1,
    type = "mesh3d",
    x = df['x'], y = df['y'], z = df['z']
)
layout = dict(
    title = '3d point clustering',
    scene = dict(
        xaxis = dict( zeroline=False ),
        yaxis = dict( zeroline=False ),
        zaxis = dict( zeroline=False ),
    )
)
fig = dict( data=[scatter, clusters], layout=layout )
# Use py.iplot() for IPython notebook
py.iplot(fig, filename='3d point clustering')

Does anyone know what error it is?

codder
  • 3
  • 2
  • Does this answer your question? [plotly inside jupyter notebook python](https://stackoverflow.com/questions/41323423/plotly-inside-jupyter-notebook-python) – G. Anderson Jun 29 '22 at 22:10
  • Hi @G.Anderson , I have tried it and did not work... copying exactly what is specified in the ansewer – codder Jun 29 '22 at 23:31

2 Answers2

0

I got it to display in Jupyter notebook. The error was due to plotly authenticiation error, so import iplot from offline.

from plotly.offline import iplot
iplot(fig, filename='3d point clustering')

Here is the complete code:

import chart_studio.plotly as py
from plotly.offline import iplot
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/alpha_shape.csv')
df.head()

scatter = dict(
    mode = "markers",
    name = "y",
    type = "scatter3d",
    x = df['x'], y = df['y'], z = df['z'],
    marker = dict( size=2, color="rgb(23, 190, 207)" )
)
clusters = dict(
    alphahull = 7,
    name = "y",
    opacity = 0.1,
    type = "mesh3d",
    x = df['x'], y = df['y'], z = df['z']
)
layout = dict(
    title = '3d point clustering',
    scene = dict(
        xaxis = dict( zeroline=False ),
        yaxis = dict( zeroline=False ),
        zaxis = dict( zeroline=False ),
    )
)
fig = dict( data=[scatter, clusters], layout=layout )
# Use py.iplot() for IPython notebook
iplot(fig, filename='3d point clustering')
0

I think the core of the issue is that you're using an old version of plotly. Currently it's at 5.9.0, but your code tries to install version 3.10.0 (!pip install plotly==3.10.0).

Since version 4, plotly is "is offline only, and does not include any functionality for uploading figures or data to cloud services". If you're using JupyterLab 3.0 or newer you don't need the jupyterlab-plotly extension either.

To make it work with the current version of plotly you can adjust your code as follows:

import pandas as pd
!pip install plotly
import plotly as py
import plotly.graph_objects as go

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/alpha_shape.csv")
df.head()

scatter = dict(
    mode="markers",
    name="y",
    type="scatter3d",
    x=df["x"],
    y=df["y"],
    z=df["z"],
    marker=dict(size=2, color="rgb(23, 190, 207)"),
)
clusters = dict(alphahull=7, name="y", opacity=0.1, type="mesh3d", x=df["x"], y=df["y"], z=df["z"])
layout = dict(
    title="3d point clustering",
    scene=dict(
        xaxis=dict(zeroline=False),
        yaxis=dict(zeroline=False),
        zaxis=dict(zeroline=False),
    ),
)
fig = go.Figure(data=[scatter, clusters], layout=layout)
fig.show()

The result:

enter image description here

Saaru Lindestøkke
  • 2,067
  • 1
  • 25
  • 51