###前提・実現したいこと
3D散布図でサンプルのグループ番号ごとにグラデーションで色分けをしたいです。
hsvなどのカラーマップを試してみたりしたのですが、コードが上手く書けず何をしてもエラーになってしまいます。
宜しくお願い致します。
###Data
PC1,PC2,PC3,sample
-0.1413928,0.02173982,-4.15E-01,1
-0.1413101,0.017508996,-4.21E-01,1
-0.1417087,0.014979086,-4.08E-01,1
-0.1411702,0.008670829,-4.09E-01,1
-0.145876,0.123370613,-1.03E-01,2
-0.1433736,0.124895934,-1.41E-01,2
-0.1456516,0.092020696,-1.09E-01,3
-0.1461915,0.094471875,-1.18E-01,3
-0.1461108,0.090837929,-1.21E-01,3
-0.1472447,0.119571378,-4.57E-02,4
-0.1472894,0.115520249,-4.68E-02,4
-0.1467504,0.121762527,-4.68E-02,4
-0.1491286,0.090455109,9.15E-02,5
-0.1487338,0.09812073,9.12E-02,5
-0.1489169,0.072974728,1.14E-01,6
-0.1490467,0.085162278,1.12E-01,6
-0.1489641,0.08360632,1.23E-01,7
-0.148754,0.088689326,1.12E-01,7
-0.1487234,0.090154785,1.12E-01,7
-0.1490534,0.096598206,9.36E-02,8
-0.149099,0.096547726,8.85E-02,8
-0.1491893,0.096628792,1.14E-01,9
-0.1490337,0.10319152,1.07E-01,9
-0.1490627,0.102746721,1.06E-01,9
-0.1479182,0.114529747,1.13E-01,10
-0.1483447,0.106218026,1.26E-01,10
-0.1483964,0.106148189,1.23E-01,11
-0.1482859,0.103531061,1.23E-01,11
-0.1487256,0.110975705,9.76E-02,12
-0.1481255,0.126683827,9.22E-02,12
-0.1479033,0.130060792,8.73E-02,12
-0.1359165,0.061005493,-7.12E-02,13
-0.1372293,0.073245259,-4.54E-02,13
-0.1464516,-0.153001981,2.27E-03,14
-0.1449456,-0.204644258,9.43E-03,14
-0.1474867,-0.111179279,-4.29E-05,15
-0.1463186,-0.170116339,1.78E-02,15
-0.1458789,-0.176175751,2.37E-02,15
-0.142477,-0.255583393,3.95E-02,16
-0.143537,-0.25325231,3.06E-02,16
-0.1428187,-0.278201657,2.85E-02,16
-0.1427046,-0.253661164,3.15E-02,17
-0.1432171,-0.270822563,2.90E-02,17
-0.1431351,-0.272426721,2.78E-02,17
-0.1442404,-0.212473411,5.68E-02,18
-0.1442105,-0.220104095,4.73E-02,18
-0.1439502,-0.216082524,3.67E-02,18
##グラデーションになっていないがグラフはできるソースコード
python
1import pandas as pd 2import matplotlib.pyplot as plt 3from mpl_toolkits.mplot3d import Axes3D 4import matplotlib.colors as mcolors 5 6df = pd.read_csv(r"test.csv", delimiter=",", sep='\s+', skipinitialspace=True) 7 8fig = plt.figure() 9ax = Axes3D(fig) 10 11samples = sorted(set(df['sample'])) 12colors = list(mcolors.CSS4_COLORS.keys()) 13 14# sample毎に描画 15for idx, sample in enumerate(samples): 16 df2 = df[df['sample'] == sample] 17 X = df2["PC1"] 18 Y = df2["PC2"] 19 Z = df2["PC3"] 20 p=ax.scatter(X, Y, Z, c=colors[idx], label=sample) 21 22plt.legend() 23plt.show()
##グラデーションにしようとしてエラーが出るソースコード
Python
1import pandas as pd 2import matplotlib.pyplot as plt 3from mpl_toolkits.mplot3d import Axes3D 4import matplotlib.colors as mcolors 5 6df = pd.read_csv(r"test.csv", delimiter=",", sep='\s+', skipinitialspace=True) 7 8fig = plt.figure() 9ax = Axes3D(fig) 10 11samples = sorted(set(df['sample'])) 12cmapval = df['sample'] 13 14 15# sample毎に描画 16for idx, sample in enumerate(samples): 17 df2 = df[df['sample'] == sample] 18 X = df2["PC1"] 19 Y = df2["PC2"] 20 Z = df2["PC3"] 21 p=ax.scatter(X, Y, Z, c=cmapval, cmap='hsv', label=sample) 22 23plt.legend() 24plt.show() 25
##補足情報(FW/ツールのバージョンなど)
python 3.7
seaborn 0.10.0
pandas 1.0.1
matplotlib 3.2.0
回答1件
あなたの回答
tips
プレビュー