質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

2276閲覧

axis 2 is out of bounds for array of dimension 2 のエラーがわかりません。

iface

総合スコア42

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2022/01/24 06:08

編集2022/01/24 06:25

前提・実現したいこと

質問失礼します。
以下のような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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

melian

2022/01/25 04:12

エラーが発生しているのは以下で、 plt.hlines(2, int(concat.min(2)), int(concat.max(2))) 原因は min() メソッドに 2 を渡しているからです。min() メソッドの第一パラメータは axis になります。
iface

2022/01/25 09:14

回答ありがとうございます。 修正します。
guest

回答1

0

自己解決

min(),max()メゾットに値を渡していることが原因でした。

投稿2022/01/25 09:15

iface

総合スコア42

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問