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

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

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

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

Python

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

Q&A

解決済

1回答

5222閲覧

Matplotlibのグラフ表示でTypeErrorが発生してしまいます。

YOSHITAKEH

総合スコア21

Matplotlib

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

Python

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

0グッド

0クリップ

投稿2018/11/03 14:23

編集2018/11/03 14:29

現状

matplotlibを使って、jupyternote上でグラフ表示をしようとすると、
TypeError: First argument must be a path or file object reading bytes
が出て、実行できません。

知りたいこと

  • TypeErrorは、どのような可能性があるのでしょうか
  • グラフ表示を実現するために解決すべきこと

のアドバイスをいただければ幸いです。

%matplotlib inline import matplotlib.pyplot as plt from numpy.random import normal,rand x = normal(size=200) plt.hist(x,bins=30) plt.show()

試したこと

  • matpplotlib notebook では枠だけ表示され、内容が表示されませんでした。
  • pythonの公式ドキュメントを見たのですが、解決策が見当たりませんでした。
  • その他、変数を変えたりいろいろしてみましたが、同じエラーとなってしまいます。

環境

jupyter note
macbook pro (OS mojave)
anaconda
python 3.7

その他、解決にむけて必要な情報がございましたらご指摘ください。
よろしくお願いいたします。

エラー内容

python

1<matplotlib.lines.Line2D at 0x11e2662e8>] 2Error in callback <function install_repl_displayhook.<locals>.post_execute at 0x11cc982f0> (for post_execute): 3--------------------------------------------------------------------------- 4 5 6/anaconda3/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs) 7 53 renderer.start_filter() 8 54 9---> 55 return draw(artist, renderer, *args, **kwargs) 10 56 finally: 11 57 if artist.get_agg_filter() is not None: 12 13/anaconda3/lib/python3.7/site-packages/matplotlib/figure.py in draw(self, renderer) 14 1491 15 1492 mimage._draw_list_compositing_images( 16-> 1493 renderer, self, artists, self.suppressComposite) 17 1494 18 1495 renderer.close_group('figure') 19 20/anaconda3/lib/python3.7/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite) 21 139 if not_composite or not has_images: 22 140 for a in artists: 23--> 141 a.draw(renderer) 24 142 else: 25 143 # Composite any adjacent images together 26 27/anaconda3/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs) 28 53 renderer.start_filter() 29 54 30---> 55 return draw(artist, renderer, *args, **kwargs) 31 56 finally: 32 57 if artist.get_agg_filter() is not None: 33 34/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe) 35 2633 renderer.stop_rasterizing() 36 2634 37-> 2635 mimage._draw_list_compositing_images(renderer, self, artists) 38 2636 39 2637 renderer.close_group('axes') 40 41/anaconda3/lib/python3.7/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite) 42 139 if not_composite or not has_images: 43 140 for a in artists: 44--> 141 a.draw(renderer) 45 142 else: 46 143 # Composite any adjacent images together 47 48/anaconda3/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs) 49 53 renderer.start_filter() 50 54 51---> 55 return draw(artist, renderer, *args, **kwargs) 52 56 finally: 53 57 if artist.get_agg_filter() is not None: 54 55/anaconda3/lib/python3.7/site-packages/matplotlib/axis.py in draw(self, renderer, *args, **kwargs) 56 1190 ticks_to_draw = self._update_ticks(renderer) 57 1191 ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw, 58-> 1192 renderer) 59 1193 60 1194 for tick in ticks_to_draw: 61 62/anaconda3/lib/python3.7/site-packages/matplotlib/axis.py in _get_tick_bboxes(self, ticks, renderer) 63 1128 for tick in ticks: 64 1129 if tick.label1On and tick.label1.get_visible(): 65-> 1130 extent = tick.label1.get_window_extent(renderer) 66 1131 ticklabelBoxes.append(extent) 67 1132 if tick.label2On and tick.label2.get_visible(): 68 69/anaconda3/lib/python3.7/site-packages/matplotlib/text.py in get_window_extent(self, renderer, dpi) 70 920 raise RuntimeError('Cannot get window extent w/o renderer') 71 921 72--> 922 bbox, info, descent = self._get_layout(self._renderer) 73 923 x, y = self.get_unitless_position() 74 924 x, y = self.get_transform().transform_point((x, y)) 75 76/anaconda3/lib/python3.7/site-packages/matplotlib/text.py in _get_layout(self, renderer) 77 298 tmp, lp_h, lp_bl = renderer.get_text_width_height_descent('lp', 78 299 self._fontproperties, 79--> 300 ismath=False) 80 301 offsety = (lp_h - lp_bl) * self._linespacing 81 302 82 83/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in get_text_width_height_descent(self, s, prop, ismath) 84 243 85 244 flags = get_hinting_flag() 86--> 245 font = self._get_agg_font(prop) 87 246 font.set_text(s, 0.0, flags=flags) 88 247 w, h = font.get_width_height() # width and height of unrotated string 89 90/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in _get_agg_font(self, prop) 91 278 """ 92 279 fname = findfont(prop) 93--> 280 font = get_font(fname) 94 281 95 282 font.clear() 96 97/anaconda3/lib/python3.7/site-packages/matplotlib/font_manager.py in get_font(filename, hinting_factor) 98 1387 if hinting_factor is None: 99 1388 hinting_factor = rcParams['text.hinting_factor'] 100-> 1389 return _get_font(filename, hinting_factor) 101 1390 102 1391 103 104TypeError: First argument must be a path or file object reading bytes 105 106<Figure size 432x288 with 1 Axes>

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

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

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

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

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

tiitoi

2018/11/03 14:42

Windows 10/Ubuntu 16.04でどちらも Python 3.6、matplotlib 2.1.0 で質問欄のコードでグラフが表示されました。コードではなく、環境の問題ではないでしょうか?
can110

2018/11/03 14:44

python3.7なのが気になります。anaconda利用されているので3.6環境構築して再現確認されてはいかがでしょうか?
YOSHITAKEH

2018/11/03 14:53

tiitoi様、can110様、コードというより環境に問題があるということですね。アドバイスありがとうございます。tiitoi様、ご回答もありがとうございます。そちらにて再度ご返信いたします。
guest

回答1

0

ベストアンサー

rcParams['text.hinting_factor']

これの設定の値がおかしいのではないでしょうか?
~/.matplotlib/matplotlibrc の設定を変えられたりしましたか?
設定を修正するか、削除してデフォルトに戻せば直ると思います。

投稿2018/11/03 14:46

tiitoi

総合スコア21956

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

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

YOSHITAKEH

2018/11/03 14:55

tiitoi様 ご回答ありがとうございます。いま思い出すと、確かにグラフの日本語化を試行錯誤している最中に変更した記憶があります。 ただ、どこを修正したのか思い出せません。 もしよろしければ、削除してデフォルトに戻す設定をご教授いただけませんでしょうか。
tiitoi

2018/11/03 15:03 編集

~/.matplotlib/matplotlibrc を消せばよいです。 消すと、<Python のディレクトリ>\site-packages/matplotlib/mpl-data/matplotlibrc のほうから読まれるようになります。 設定は import matplotlib.pyplot を行ったタイミングで読まれるので、Jupyter Notebook の場合は Notebook を Kernel → Shutdown より一旦終了して再度 import を実行してください。 日本語化対応方法は以下の質問の回答にあるので、もしよかったらどうぞ https://teratail.com/questions/155411
YOSHITAKEH

2018/11/03 15:17

ご回答ありがとうございます。 まだ解決はできていないのですが、進捗をご報告いたします。 ~/.matplotlib に、matplotlibrcが存在しませんでした。あったのは、 fontList.jsonファイルとtex.cacheディレクトリだけでした。 そこで、同時並行してご教授いただいた日本語化設定の回答を参考に、matplotlibrcファイルを作成しました。(作成したところで、いま回答を書いております) そこで更に質問させていただきたいのですが、そもそもmatplotlibrcが存在していなかったということは、import matplotlib.pyplotを行っても設定が読み込まれてなかったということでしょうか? お忙しい中、何度も質問してしまって大変申し訳ございません。。
tiitoi

2018/11/03 15:21

~/.matplotlib/matplotlibrc がなければ、<Python のディレクトリ>\site-packages/matplotlib/mpl-data/matplotlibrc のデフォルトの設定が読み込まれるはずです。 以下のコードで現在どこの設定ファイルが読み込まれているか確認できるので、試してみてはどうでしょうか? import matplotlib as mpl print(mpl.matplotlib_fname())
YOSHITAKEH

2018/11/03 15:31

早速のご回答ありがとうございます。 試してみました。以下のファイルを読み込んでいるようです。 /anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc おそらく、日本語化の試行錯誤時に、上記ファイルを変更してしまったのだと記憶しております。 このファイルを削除してしまってはいけないのですよね? Anaconda自体を再インストールしたほうがよろしいのでしょうか。
tiitoi

2018/11/03 15:41 編集

Github のhttps://raw.githubusercontent.com/matplotlib/matplotlib/master/matplotlibrc.template から matplotlibrc.template を右クリックで保存して、matplotlibrc とリネームして差し替えればいいかと思います。 上記に貼った日本語化手順はそこのファイルをコピーして行っているので、そこのファイルがおかしくなっているのであれば、正しいものに差し替えたあとにやり直してください。
YOSHITAKEH

2018/11/03 15:58

ご回答ありがとうございます。 アドバイス頂いた方法で、試してみます。 ありがとうございます。
YOSHITAKEH

2018/11/03 16:05

titoi様、 ご指摘頂いた通りに実行し、無事に表示できました。 追加で、どうやらfont_manager.pyも変更していたようです。 USE_FONTCONFIG = False  をTrueにしていました。 夜分にお付き合い頂きまして、心の底から感謝いたします。 本当に、本当に助かりました。 これから、日本語化設定に移りたいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問