1

Pandas provides builtin plotting functionality for DataFrames with several plotting backend engines (matplotlib, etc.). Id like to plot an interactive 3D scatterplot directly from a dataframe via df.plot() but came up with non-interactive plots only. I'm thinking of something I get when e.g. plotly. I'd prefer a solution which is independent of exploratory data analysis IDE setup dependencies (e.g. ipywidget when using JupyterLab). How can I plot interactive 3D scatter plots via df.plot()?

thinwybk
  • 4,193
  • 2
  • 40
  • 76

2 Answers2

1

plotly is the way to go...use scatter3d

import plotly as py
import plotly.graph_objs as go
import numpy as np
import pandas as pd

# data
np.random.seed(1)
df = pd.DataFrame(np.random.rand(20, 3), columns=list('ABC'))

trace = go.Scatter3d(
    x=df['A'],
    y=df['B'],
    z=df['C'],

    mode='markers',
    marker=dict(
        size=5,
        color=c,              
        colorscale='Viridis',   
    ),
    name= 'test',

    # list comprehension to add text on hover
    text= [f"A: {a}<br>B: {b}<br>C: {c}" for a,b,c in list(zip(df['A'], df['B'], df['C']))],
    # if you do not want to display x,y,z
    hoverinfo='text'
)


layout = dict(title = 'TEST',)

data = [trace]
fig = dict(data=data, layout=layout)

py.offline.plot(fig, filename = 'Test.html')
It_is_Chris
  • 13,504
  • 2
  • 23
  • 41
  • I know how to use plotly. But I want to create an interactive plot directly from a dataframe with `pd.DataFrame.plot()`. And as far as I know plotly is not supported as pandas plotting backend engine. – thinwybk Apr 23 '20 at 13:29
  • It is not possible yet...https://github.com/plotly/plotly.py/issues/2005 – It_is_Chris Apr 23 '20 at 13:35
0

Holding to the df.plot() approach there is an df.iplot() with Cufflinks and Plotly.

BND
  • 612
  • 1
  • 13
  • 23
  • I already used cufflinks. However I'd prefer a solution which does not depend on `ipywidget`. It's anoying if you get no output and have to care about jupyterlab setup if you want a `hvplot` like solution (whose interactive plots work with JupyterLab out of the box). – thinwybk Apr 23 '20 at 13:39