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

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

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

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

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

1602閲覧

python matplotlibでのエラーについて

kzi98

総合スコア2

Matplotlib

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

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/08/30 07:45

編集2020/08/30 09:00

前提・実現したいこと

先週までは以下記載の内容のプログラムを動かすことができたのですが、本日実行してみるとプログラムを急に動かせなくなっておりました。
心当たりは、matplotlibをアップデートしたことなのですが、それ以外は何もしておらず、プログラムの内容を変更したりはしておりません…
何が原因なのでしょうか?
エラーはTypeError: 'int' object is not subscriptable という内容です。エラー回避・解消法がご存じの方教えて頂きたいです。
足りない情報等ありましたら追記いたします。よろしくお願いします。

発生している問題・エラーメッセージ

Exception in Tkinter callback Traceback (most recent call last): File "c:\…\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "c:\…\lib\tkinter\__init__.py", line 804, in callit func(*args) File "c:\…\lib\site-packages\matplotlib\backends\_backend_tk.py", line 253, in idle_draw self.draw() File "c:\…\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw super(FigureCanvasTkAgg, self).draw() File "c:\…\lib\site-packages\matplotlib\backends\backend_agg.py", line 407, in draw self.figure.draw(self.renderer) File "c:\…\lib\site-packages\matplotlib\artist.py", line 41, in draw_wrapper return draw(artist, renderer, *args, **kwargs) File "c:\…\lib\site-packages\matplotlib\figure.py", line 1863, in draw mimage._draw_list_compositing_images( File "c:\…\lib\site-packages\matplotlib\image.py", line 131, in _draw_list_compositing_images a.draw(renderer) File "c:\…\lib\site-packages\matplotlib\artist.py", line 41, in draw_wrapper return draw(artist, renderer, *args, **kwargs) File "c:\…\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 461, in draw axis.draw(renderer) File "c:\…\lib\site-packages\matplotlib\artist.py", line 41, in draw_wrapper return draw(artist, renderer, *args, **kwargs) File "c:\…\lib\site-packages\mpl_toolkits\mplot3d\axis3d.py", line 396, in draw info['tick']['linewidth'][tick._major]) TypeError: 'int' object is not subscriptable

該当のソースコード

python

1import matplotlib.pyplot as plt 2from mpl_toolkits.mplot3d import Axes3D 3from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk) 4from matplotlib.figure import Figure 5import matplotlib.colors as mcolors 6 7 8def draw(event): 9 fig = plt.figure(figsize=(4,4)) 10 ax=fig.gca(projection='3d') 11 12 ax.set_xlim([-1., 1.]) 13 ax.set_ylim([-1., 1.]) 14 ax.set_zlim([-1., 1.]) 15 16 for a in [ax.xaxis, ax.yaxis, ax.zaxis]: 17 a.set_ticklabels([]) 18 a._axinfo['grid']['linewidth'] = 0 19 a._axinfo['tick']['linewidth'] = 0 20 21 for a in [ax.w_xaxis, ax.w_yaxis, ax.w_zaxis]: 22 a.line.set_linewidth(0) 23 a.set_pane_color((0., 0., 0., 0.)) 24 #順にRGBA 25 26 ax.set_facecolor('black') 27 28 u, v = np.mgrid[0:2*np.pi:50j, 0:np.pi:25j] 29 30 x = np.cos(u) * np.sin(v) 31 y = np.sin(u) * np.sin(v) 32 z = np.cos(v) 33 34 a=random.random() 35 b=random.random() 36 c=random.random() 37 38 colors = np.zeros((50, 25, 3)) 39 for i in range(0, 25): 40 for j in range(0, 25): 41 colors[i][j][0] = a 42 colors[i][j][1] = b 43 colors[i][j][2] = c 44 45 #colors2=mcolors.rgb_to_hsv(mcolors.to_rgb(colors)) 46 47 ax.plot_surface(x, y, z, facecolors = colors, shade = False) 48 49 moon=random.randint(-90,270) 50 51 52 ax.view_init(elev = 0, azim = moon) 53 54 canvas = FigureCanvasTkAgg(fig, master=root) 55 canvas.get_tk_widget().place(x=0,y=0) 56 57root = tkinter.Tk() 58root.geometry("800x600") 59 60button= tkinter.Button(root, text=u'ボタン',width=15) 61button.bind("<Button-1>",draw) 62button.place(x=300,y=450) 63 64tkinter.mainloop()

試したこと

matplotlibのアップデートが原因かと思い,以前のバージョンのものをインストールしなおしたのですが、それが原因ではありませんでした。

補足情報(FW/ツールのバージョンなど)

python 3
matplot 0.1.9
matplotlib 3.3.1

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

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

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

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

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

tiitoi

2020/08/30 14:31

matplotlib 単体だとならないですが、tkinter と混ぜて使うとなりますね 原因はわからないですが、、
kzi98

2020/08/30 15:25

ありがとうございます。 私の場合、matplotlibのみでも同じエラーが発生するのですが、python もしくは IDEに問題があると考えられますか、、?
tiitoi

2020/08/31 00:03 編集

回答のコードを修正しました。軸の目盛りの線を消す処理をそれと同じ処理である ax.set_axis_off() に置き換えたら tkinter ありでも動作しました。 外部に公開されていない関数を使っていて、バージョンを上げたことによりそれが使えなくなった可能性がありますね。
teamikl

2020/08/31 02:20

動作報告ですが、matplotlib 3.1.3/win10 では提示のコードで正常に動きました。 (幾つか import 忘れのモジュールのみ修正して)
guest

回答2

0

File "c:\…\lib\site-packages\mpl_toolkits\mplot3d\axis3d.py", line 396, in draw info['tick']['linewidth'][tick._major]) TypeError: 'int' object is not subscriptable

エラー原因の該当箇所について、
mpl_toolkits のバージョンではないでしょうか?


Add support for minor ticks in 3d axes.

The actual implementation is shorter than deleting the copies of "Minor

ticks are not supported" in the various docstrings...

diff

1- tick.tick1line.set_linewidth(info['tick']['linewidth']) 2+ tick.tick1line.set_linewidth( 3+ info['tick']['linewidth'][tick._major])

投稿2020/08/30 20:46

teamikl

総合スコア8664

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

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

kzi98

2020/08/30 22:39

ご回答ありがとうございます。 私の認識であれば、mpl_toolkitは、matplotlibのアップデートやダウングレードを行えば、自動的にバージョンが変わると思っていたのですが、間違っているのでしょうか...? (matplotlib自体の、アップデートやダウングレードは行なってみたのですが改善は見られませんでした。) お手数ですが、教えて頂きたいです。
teamikl

2020/08/31 02:16

matplotlib の一部なので、mpl_toolkits のバージョンというのはおかしかったですね、 インストールされるディレクトリが別れているだけで、 同パッケージ内、バージョンは自動的に追従されるで合ってるはずです。 確認項目: - 以前の正常に動いていた時のバージョンと、 ダウングレードした先バージョンはどのバージョンですか? - また、ダウングレードは正常に適応されていましたか?(ダウングレード後のバージョンの確認方法) 実際にエラーログに現れてる以上は、意図してるバージョンになってない可能性があります。
kzi98

2020/08/31 15:47

お返事ありがとうございます。 教えて頂いたエラー該当箇所のみコメントアウトして実行したところ、前とは違う画像出力にはなってしまうものの、エラーが出ることなく動きました。 内部の関数に変更があるという可能性も、これから念頭に置いておこうと思います。
guest

0

ベストアンサー

for a in [ax.xaxis, ax.yaxis, ax.zaxis]: a.set_ticklabels([]) a._axinfo['grid']['linewidth'] = 0 a._axinfo['tick']['linewidth'] = 0

上がエラーの原因です。
名前が _ から始まっている属性、関数というのは外部に公開されていない (ユーザーが使うことが想定されていない) ものなので、バージョン変更で変わってしまった可能性がありますね。
この処理は目盛りの軸を消す処理だと思いますが、ax.set_axis_off() で同じことができます。

サンプルコード

以下のコードで tkinter ありで動作しました。

python

1import numpy as np 2from matplotlib import colors as mcolors 3from matplotlib import pyplot as plt 4from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk 5from matplotlib.figure import Figure 6from mpl_toolkits.mplot3d import Axes3D 7import tkinter 8 9 10def draw(event): 11 fig = plt.figure(figsize=(4, 4)) 12 13 ax = fig.gca(projection="3d", facecolor="k") 14 ax.set_xlim([-1.0, 1.0]) 15 ax.set_ylim([-1.0, 1.0]) 16 ax.set_zlim([-1.0, 1.0]) 17 ax.set_axis_off() 18 19 u, v = np.mgrid[0 : 2 * np.pi : 50j, 0 : np.pi : 25j] 20 x = np.cos(u) * np.sin(v) 21 y = np.sin(u) * np.sin(v) 22 z = np.cos(v) 23 24 # (50, 25, 3) の色の配列を作成する。 25 colors = np.zeros((50, 25, 3)) 26 colors[:25, :] = np.random.rand(3) 27 28 ax.plot_surface(x, y, z, facecolors=colors, shade=False) 29 30 azim = np.random.randint(-90, 270) 31 ax.view_init(elev=0, azim=azim) 32 33 canvas = FigureCanvasTkAgg(fig, master=root) 34 canvas.get_tk_widget().place(x=0,y=0) 35 36root = tkinter.Tk() 37root.geometry("800x600") 38 39button= tkinter.Button(root, text=u'ボタン',width=15) 40button.bind("<Button-1>",draw) 41button.place(x=300,y=450) 42 43tkinter.mainloop()

投稿2020/08/30 15:36

編集2020/08/31 00:17
tiitoi

総合スコア21956

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

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

teamikl

2020/08/31 02:43 編集

内部のデータ構造 tick の linewidth が数値から辞書に変更があったようです。 v3.2.2 https://github.com/matplotlib/matplotlib/blob/v3.2.2/lib/mpl_toolkits/mplot3d/axis3d.py#L388 v.3.3.0 https://github.com/matplotlib/matplotlib/blob/v3.3.0/lib/mpl_toolkits/mplot3d/axis3d.py#L396 他に差支えが無ければ、解決方法は ダウングレードよりも、 内部の値を直接変更している処理を、API を使って行う方が順当ですね。
tiitoi

2020/08/31 02:49 編集

ソースは確認していなかったのですが、matplotlib のバージョンアップで内部のコードが変わったことで動かなくなっていたみたいですね。 コメントありがとうございます。
kzi98

2020/08/31 23:40

ありがとうございます。 公開されていない関数は、バージョンアップされると使えなくなる場合があると初めて知りました。 頂いたコード通り、軸の目盛りを処理すると、プログラムを実行することができました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問