Pythonのコードについて。3D散布図を作っているのですが、色の分け方がわかりません。今やろうとしているのは2つのパターン①Z軸の値が低いほど色が濃くなる②個別に(またはグループで)色を変えるです。今のところはimport matplotlib.pyplot as pltx = [35.0038911226567,35.0080001192339, 35.0051856028373,35.0190695133124 ,35.0170013137849,35.0253093786291, 35.019775374585,35.030379805763, 35.0302403554252,35.0323741432034, 35.0305858916861,35.0278576985089, 35.0304363470036,35.0314821942626, 35.0325298487105]y = [135.752990149628,135.755255227701, 135.764640079703,135.754910117998, 135.767888642116,135.767341505597, 135.759169118744,135.753341125753, 135.753150056864,135.74648751296, 135.746556612704,135.751153150455, 135.735479005851,135.735753085946, 135.732776345063]z = [-42,-63,6.8,-18.1,33.7,30.3,22.5, 8.325,-12.5,-51.7,21.825,-43.6,35.73,35.73,-25.1]fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection': '3d'})ax.scatter(x, y, z)ax.set_xlabel(\u0026quot;x\u0026quot;)ax.set_ylabel(\u0026quot;y\u0026quot;)ax.set_zlabel(\u0026quot;z\u0026quot;)ax.view_init(elev=30, azim=180)plt.show()で、下の図のようになっています。

1件の回答

回答を書く

1076582

2026-05-30 11:10

+ フォロー

>①Z軸の値が低いほど色が濃くなる

>②個別に(またはグループで)色を変える





①、② 試しましたがピンとこない(人間の視覚に合わない)



そこで調べたら

PyVista が一番適合しそう

... ですが自分の環境では正常動作しなかった(重いしGPU依存)



③案として

matplotlib ⇒ plotly

(実行したらブラウザに表示されます)

※ インストールが必要

\u0026gt; pip install plotly

※これもピンとこない。PyVista が(本格らしいので)良いのかも





もしかしたら、データの空間的広がりが小さすぎて、3D として認識できない構造になっているのかも。





★サンプルスクリプトのデータ部分(x[],y[],z[])は②③では省略してます



~~ [①対応版] ~~

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D



x = [35.0038911226567,35.0080001192339,

    35.0051856028373,35.0190695133124,

    35.0170013137849,35.0253093786291,

    35.019775374585,35.030379805763,

    35.0302403554252,35.0323741432034,

    35.0305858916861,35.0278576985089,

    35.0304363470036,35.0314821942626,

    35.0325298487105]

y = [135.752990149628,135.755255227701,

    135.764640079703,135.754910117998,

    135.767888642116,135.767341505597,

    135.759169118744,135.753341125753,

    135.753150056864,135.74648751296,

    135.746556612704,135.751153150455,

    135.735479005851,135.735753085946,

    135.732776345063]

z = [-42,-63,6.8,-18.1,33.7,30.3,22.5,

    8.325,-12.5,-51.7,21.825,-43.6,35.73,35.73,-25.1]



fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection': '3d'})



sc = ax.scatter(x, y, z, c=z, cmap='viridis')  # Z値に応じた色分け

fig.colorbar(sc, ax=ax, shrink=0.5, aspect=10)



ax.set_xlabel(\u0026quot;x\u0026quot;)

ax.set_ylabel(\u0026quot;y\u0026quot;)

ax.set_zlabel(\u0026quot;z\u0026quot;)

ax.view_init(elev=30, azim=180)

plt.show()





~~ [②対応版] ~~

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D



x[...]

y[...]

z[...]



# 任意の色リスト(例:グループ分けや個別指定)

colors = ['red', 'blue', 'green', 'orange', 'purple',

          'cyan', 'magenta', 'yellow', 'black', 'gray',

          'pink', 'lime', 'navy', 'brown', 'olive']  # 15個



fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection': '3d'})

ax.scatter(x, y, z, c=colors)



ax.set_xlabel(\u0026quot;x\u0026quot;)

ax.set_ylabel(\u0026quot;y\u0026quot;)

ax.set_zlabel(\u0026quot;z\u0026quot;)

ax.view_init(elev=30, azim=180)

plt.show()







~~ [③対応版] ~~

import plotly.graph_objects as go



# データ

x[...]

y[...]

z[...]



fig = go.Figure()



# 散布図

fig.add_trace(go.Scatter3d(

    x=x,

    y=y,

    z=z,

    mode='markers',

    marker=dict(

        size=6,

        color=z,          # z の値で色分け(自然なグラデーション)

        colorscale='Viridis',

        opacity=0.9

    )

))



# レイアウト調整(見やすいカメラ位置)

fig.update_layout(

    title=\u0026quot;Plotly 3D Scatter (No Surface)\u0026quot;,

    width=900,

    height=700,

    scene=dict(

        xaxis_title='x',

        yaxis_title='y',

        zaxis_title='z',

        camera=dict(eye=dict(x=1.8, y=1.8, z=1.2)),

        bgcolor=\u0026quot;white\u0026quot;

    )

)



fig.show()



うったえる有益だ(0シェアするブックマークする

関連質問

Copyright © 2026 AQ188.com All Rights Reserved.

博識 著作権所有