前提・実現したいこと
csvデータからループ処理で三次元の折れ線グラフを作成しようとすると,エラーになってしまいます。
id_==1でbreakさせた場合はグラフが出力されますが,id_==2以降になると以下のようなエラーが発生します。
おそらくid_==2以降はインデックスの参照が0から開始されないためエラーになってしまうと思うのですが,上手く解決できませんでした。
発生しているエラーメッセージ
Traceback (most recent call last): File "C:/Users/XXXXXX/graph_line.py", line 26, in <module> ax.plot(xt, yt, zt) # id==2以降からエラーになってしまう File "C:\Users\XXXXXX\AppData\Local\Programs\Python\Python36-32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1524, in plot if len(args[0]) == 0 or cbook.is_scalar(args[0][0]) : File "C:\Users\XXXXXX\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\series.py", line 767, in __getitem__ result = self.index.get_value(self, key) File "C:\Users\XXXXXX\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexes\base.py", line 3118, in get_value tz=getattr(series.dtype, 'tz', None)) File "pandas\_libs\index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value File "pandas\_libs\index.pyx", line 114, in pandas._libs.index.IndexEngine.get_value File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\hashtable_class_helper.pxi", line 958, in pandas._libs.hashtable.Int64HashTable.get_item File "pandas\_libs\hashtable_class_helper.pxi", line 964, in pandas._libs.hashtable.Int64HashTable.get_item KeyError: 0
該当のソースコード
python
1import matplotlib.pyplot as plt 2from mpl_toolkits.mplot3d import Axes3D 3import pandas as pd 4 5df = pd.read_csv('data.csv', names=('id', 'time', 'x', 'y', 'z')) 6fig = plt.figure() 7ax = fig.add_subplot(111, projection='3d') 8 9 10for id_ in df["id"].unique(): 11 tmp = df.query("id==@id_") 12 xt, yt, zt = tmp["x"], tmp["y"], tmp["z"] 13 ax.plot(xt, yt, zt) # id==2以降からエラーになってしまう 14 if id_ == 2: # id_==1までは作図可能 15 break 16 17plt.show()
###サンプルCSV
|id|time|x|y|z|
|:--|:--:|--:|
|1| 0| -2.107227| 0.7345147| -7.195769|
|1| 0.49733| -1.828499| 0.5122257| -7.120043|
|1| 0.9856975| -1.51933| 0.4999981| -7.063078|
|1| 1.491172| -1.213579| 0.4999981| -7.049526|
|1| 1.996647| -0.8575201| 0.4999981| -7.09712|
|1| 2.485592| -0.3712597| 0.499999| -7.088724|
|1| 2.990747| 0.1331902| 0.4999981| -7.085234|
|1| 3.496319| 0.5924625| 0.4999981| -7.098866|
|1| 3.985729| 1.065119| 0.4999981| -7.130569|
|1| 4.490369| 1.56337| 0.499999| -7.161113|
|2| 0| -5.27637| 0.7345147| -6.805758|
|2| 0.49733| -5.046183| 0.5122257| -6.86202|
|2| 0.9856975| -4.656722| 0.499999| -6.795047|
|2| 1.491172| -4.204307| 0.499999| -6.750597|
|2| 1.996647| -3.915286| 0.4999981| -6.715219|
|2| 2.485592| -3.619803| 0.4999981| -6.793261|
|2| 2.990747| -3.348916| 0.4999981| -6.751401|
|2| 3.496319| -3.089452| 0.4999981| -6.797764|
|2| 3.985729| -2.924153| 0.4999981| -6.867144|
|2| 4.490369| -2.642242| 0.4999981| -6.865789|
試したこと
ilocを使用して,
xt, yt, zt = tmp["x"].iloc[id_], tmp["y"].iloc[id_], tmp["z"].iloc[id_]
としてみたところ,TypeErrorが吐き出されました
Traceback (most recent call last):
File "C:/Users/XXXXXX/graph_line.py", line 26, in <module>
ax.plot(xt, yt, zt) # id==2以降からエラーになってしまう
File "C:\Users\XXXXXX\AppData\Local\Programs\Python\Python36-32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1535, in plot
zs = np.ones(len(xs)) * zs
TypeError: object of type 'numpy.float64' has no len()
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/09 01:26
2019/01/09 01:30
2019/01/09 03:00