前提・実現したいこと
質問失礼します。
以下のようなtxtファイルを読み込んで、1~6のそれぞれの数字の最長距離(線分の長さ)を調べてmatplotlibでプロットしたいと考えています。
例えば、"6"の距離を調べたい場合、六の出現場所は、bottom[0], bottom[2], bottom[3]で、それぞれのindexを調べて最長の距離を求めるというものです。
txt
1top = [1 ,0, 3, 1, 4, 2, 3, 2] 2bottom = [6, 4, 6, 6, 3, 0, 5, 5]
1~6それぞれの数字の最長距離をfor文で回してmatplotlibで表示させたいのですが、以下のようなエラーが出てしまいました。
axis 2というのは3次元のことで、なぜ2次元の話をしているのにこのようなエラーが出るのかがわかりません。
よろしくお願いします。
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- AxisError Traceback (most recent call last) <ipython-input-80-1b52cd787767> in <module> 46 47 if __name__ == '__main__': ---> 48 main() 49 50 <ipython-input-80-1b52cd787767> in main() 39 print("bottom : ", bottom) 40 ---> 41 le.h_seg(1) 42 43 <ipython-input-80-1b52cd787767> in h_seg(self, num) 24 25 plt.hlines(1, int(concat.min(1)), int(concat.max(1))) ---> 26 plt.hlines(2, int(concat.min(2)), int(concat.max(2))) 27 28 plt.show() /Applications/anaconda3/lib/python3.8/site-packages/numpy/core/_methods.py in _amin(a, axis, out, keepdims, initial, where) 41 def _amin(a, axis=None, out=None, keepdims=False, 42 initial=_NoValue, where=True): ---> 43 return umr_minimum(a, axis, None, out, keepdims, initial, where) 44 45 def _sum(a, axis=None, dtype=None, out=None, keepdims=False, AxisError: axis 2 is out of bounds for array of dimension 2
該当のソースコード
python
1#一部抜粋 2 3def h_seg(self, num): 4 5 top_index = np.where(np.array(top) == num) 6 bottom_index = np.where(np.array(bottom) == num) 7 concat = np.concatenate([top_index, bottom_index], axis=1) 8 9 plt.hlines(1, int(concat.min(1)), int(concat.max(1))) 10 plt.hlines(2, int(concat.min(2)), int(concat.max(2))) 11 12 plt.show() 13 14def main(): 15 16 with open("routing.txt") as f: 17 reader = csv.reader(f) 18 l = [list(map(int, row)) for row in reader] 19 20 le = left_edge(l[0], l[1]) 21 22 le.h_seg() 23 return 0 24
回答1件
あなたの回答
tips
プレビュー