1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Colab最小構成】Plotlyで3Dグラフを一発で動かす方法

Posted at

はじめに

image.png

Google Colabで、追加のインストールなしにPlotlyの3Dグラフをインタラクティブに表示できる、軽量かつ安定した最小構成をまとめました。

図のPlotlyのバージョンは時期によって異なる場合があるため、あくまで参考程度にご覧ください。

最小コード

以下のコードをColabのセルに貼り付けて実行するだけです。

import plotly.graph_objects as go
import numpy as np

# 球体のメッシュ座標を生成
phi, theta = np.mgrid[0:2*np.pi:40j, 0:np.pi:20j]
x = np.sin(theta) * np.cos(phi)
y = np.sin(theta) * np.sin(phi)
z = np.cos(theta)

# Plotlyで3Dサーフェスを作成
fig = go.Figure(data=[go.Surface(x=x, y=y, z=z, colorscale='Viridis')])
fig.update_layout(scene=dict(aspectmode='data'))
fig.show()

実行結果:

  • 3D回転・ズームが可能
  • pip install不要
  • ブラウザ上でスムーズに動作

image.png

なぜこの構成で動くのか

理由 説明
ブラウザ描画 WebGLベースで動作するため、Colab環境でそのまま表示可能
標準同梱 Colabの最新版にはPlotly 5.24+が標準インストール済み
自動HTML出力 fig.show()でHTMLとJavaScriptが自動生成される

基本パターン3選

① 3D散布図(Scatter3d)

import plotly.graph_objects as go
import numpy as np

x, y, z = np.random.randn(3, 100)
fig = go.Figure(data=[go.Scatter3d(
    x=x, y=y, z=z,
    mode='markers',
    marker=dict(size=4, color=z, colorscale='Plasma')
)])
fig.show()

用途: データの分布を視覚的に把握

image.png

② 関数面の可視化(Surface)

import plotly.graph_objects as go
import numpy as np

X, Y = np.meshgrid(np.linspace(-2, 2, 100), np.linspace(-2, 2, 100))
Z = np.cos(X**2 + Y**2)

fig = go.Figure(data=[go.Surface(x=X, y=Y, z=Z, colorscale='Cividis')])
fig.update_layout(title='cos(x²+y²)', scene=dict(aspectmode='data'))
fig.show()

用途: 数学関数や地形データの立体表示

image.png

③ ワイヤーフレーム風表示

fig.update_traces(
    contours_z=dict(
        show=True,
        usecolormap=True,
        highlightcolor="limegreen",
        project_z=True
    )
)

用途: 等高線を投影して構造を見やすく

image.png

カスタマイズのポイント

軸ラベルと背景設定

fig.update_layout(
    scene=dict(
        xaxis_title='X軸',
        yaxis_title='Y軸',
        zaxis_title='Z軸',
        xaxis=dict(backgroundcolor='rgba(240,240,240,0.5)'),
        yaxis=dict(backgroundcolor='rgba(240,240,240,0.5)'),
        zaxis=dict(backgroundcolor='rgba(240,240,240,0.5)')
    ),
    title='3Dデータの基本可視化'
)

効果:

  • 半透明背景で奥行きが見やすくなる
  • 軸タイトルで数値関係が明確に

image.png

よく使うカラースケール

カラーマップ 用途
Viridis 汎用的で見やすい(デフォルト推奨)
Plasma 赤系のグラデーション
Cividis 色覚特性に配慮した配色

主要パラメータ一覧

パラメータ 説明
plotly.graph_objects 高機能な低レベルAPI(詳細設定向け)
plotly.express 簡易API(px.scatter_3d()など)
aspectmode='data' 各軸のスケールを実データに合わせる
colorscale カラーマップ指定
scene 3D空間の設定(カメラ・軸・背景)

トラブルシューティング

症状 原因・対処法
グラフが表示されない セルを再実行、またはブラウザを再読込
3Dがカクつく Colabのランタイムを再起動
HTML保存したい fig.write_html("plot.html") で保存可能

まとめ

image.png

Plotlyを選ぶ理由

  • 環境構築が不要:pipインストールなしでそのまま実行可能。

  • 軽量で安定:PyVistaやmatplotlibに比べて動作が軽く、扱いやすい。

  • 拡張しやすい:2Dから3Dへの移行がスムーズ。

PyVistaやmatplotlibの環境エラーで悩む前に、まずはPlotlyで手軽に「見える化」してみるのがおすすめです。

参考リンク

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?