Pandas为DataFrame
提供了内置的绘图功能,并具有多个绘图后端引擎(matplotlib
等)。我想直接通过df.plot()
从数据框中绘制交互式3D散点图,但只想出了非交互式图。我在想什么,例如plotly
。我希望有一种独立于探索性数据分析IDE设置依赖性的解决方案(例如,使用JupyterLab时为ipywidget
)。如何通过df.plot()
绘制交互式3D散点图?
答案 0 :(得分:1)
plotly是必经之路...使用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')
答案 1 :(得分:0)
采用df.plot()
方法的情况下,有Cufflinks and Plotly和df.iplot()
。