MatplotlibはPythonでグラフを作成するときに、よく用いられるライブラリです。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
しかし、デフォルトだと日本語表示ができません!
先日グラフで日本語表示できずとても大変でした…
そこで、今回は自分と同じ悩みを持つ初心者さん向けにこの記事を書いていきます!
- Matplotlibを使ったグラフで日本語を表示する
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
画像と一緒に紹介するので、簡単にできるようになります!
使用環境
解説に入る前に私の環境について簡単に紹介します。
- パソコン → M1 MacbookAir
- OS → macOS Monterey
- 環境 → Google Colaboratory
Google Colaboratoryを使用したので、Mac か Windows かはあまり関係ないです。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
Google Colaboratoryについてはこちらからどうぞ!
![](https://yosshilifeup.com/wp-content/uploads/2021/12/Slide1-3-1-300x169.jpg)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/Slide1-3-1-300x169.jpg)
Google Colaboratoryは非常に便利なので、知らなかった方はぜひこの記事を読んで欲しいです!
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
画像付きで使い方を紹介してるので簡単に始められます!
今回紹介する方法は、Google Colaboratoryを使わなくても通用するので安心してください。
なお今回の記事に使用する画像はGoogle Colaboratoryで実行したものです。
グラフを作成してみよう!
![](https://yosshilifeup.com/wp-content/uploads/2022/02/photo-1543286386-2e659306cd6c.webp)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/photo-1543286386-2e659306cd6c.webp)
まずは、そのまま日本語を入れたグラフを作ってみましょう。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
今回は例としてsin(x)のグラフを作りました
コードと実行結果
![](https://yosshilifeup.com/wp-content/uploads/2022/02/22e759d69570f00dfe539757e35fc9a6.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/22e759d69570f00dfe539757e35fc9a6.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/b3410f19e6dfea36e0108dfe360e6c83.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/b3410f19e6dfea36e0108dfe360e6c83.png)
グラフを見るとわかるように日本語の部分だけ□になってしまいました。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
このことから、日本語が原因だとわかりますね
使えるフォントを確認してみよう!
グラフで日本語を表示するには、日本語に対応したフォントに設定する必要があります。
そこで、まずは使えるフォントを確認しましょう。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
環境によって違うので必ず確認しよう!
コードと実行結果
![](https://yosshilifeup.com/wp-content/uploads/2022/02/209c4b5c1fb8bf0865d90eab8ba92d75.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/209c4b5c1fb8bf0865d90eab8ba92d75.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/e486321896549128ada4f5c7c4005b11.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/e486321896549128ada4f5c7c4005b11.png)
これは、左側に’日本語’を右側のフォントで表示しているというものです。
もし、左側に日本語と表示されたら表示されたフォントの名前を覚えてください。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
私と同じように1つも表示されなくても大丈夫!
日本語と表示されるフォントがないとき
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
さっきのコードで、日本語と表示された方はここは飛ばして大丈夫です!
次に日本語対応のフォントをインストールします
なので、新たに日本語対応のフォントを外から持ってきます。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
とっても簡単なので安心して!
コードと実行結果
![](https://yosshilifeup.com/wp-content/uploads/2022/02/61d4c04c2b1828a56fb1e7a8514f80ca-1024x495.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/61d4c04c2b1828a56fb1e7a8514f80ca-1024x495.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
赤い波線は気にしなくて大丈夫です
これだけで、日本語フォントのインストールができます。
しかも、たったの7秒でインストールが完了します。
それでは、もう1度フォントをいていきましょう。
![](https://yosshilifeup.com/wp-content/uploads/2022/02/9a9c28db5fa5f57c9afe2d0ba30635d4.jpg)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/9a9c28db5fa5f57c9afe2d0ba30635d4.jpg)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/144a7135e25f49718407c7a08ec00b57.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/144a7135e25f49718407c7a08ec00b57.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
インストールした japanize_matplotlib のimportを忘れずに!
新たに、IPAexGothicという日本語対応フォントが増えたのがわかります。
フォントを反映させよう!
フォントを反映せるのは簡単にできます。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
実際に見てみよう!
最初にフォントを確認した時点で日本語対応フォントがあった場合
コードと実行結果
![](https://yosshilifeup.com/wp-content/uploads/2022/02/c6192492bcbdf77a4fd0e9ea01c21930.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/c6192492bcbdf77a4fd0e9ea01c21930.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/dda1348b4d4c7403a041486976f24405.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/dda1348b4d4c7403a041486976f24405.png)
最初にフォントを指定するだけで、日本語が表示されるようになります。
japanize_matplotlibをインストールした場合
こちらの場合も簡単に設定することができます。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
コードを見ていこう!
コードと実行結果
![](https://yosshilifeup.com/wp-content/uploads/2022/02/17b6438cba24d89b87b8b488070697a2.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/17b6438cba24d89b87b8b488070697a2.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/0a6f30909a4721c4e4bcd2802138d875.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/0a6f30909a4721c4e4bcd2802138d875.png)
こちらは最初にjapanize_matplotlibをimportするだけです。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
実行結果は変わりません
どちらの方法も簡単にできるので、焦らず試してください。
まとめ
![](https://yosshilifeup.com/wp-content/uploads/2022/02/photo-1523475472560-d2df97ec485c.webp)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/photo-1523475472560-d2df97ec485c.webp)
今回は、Matplotlibのグラフで日本語表示する方法を紹介しました。
今回の記事では、Windows、Mac問わず、またどの使用環境でも実行できるものだと思います。
この記事では、初心者向けなので特に簡単な方法を紹介しました。
皆さんのお役に立てたら幸いです。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
私は、とても手間取ったので…
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
ではまた、別の記事で!
ユクサスからのお知らせ!
リスト解説シリーズ以外にも、プログラミングに関する記事や初心者向け紹介・解説、私なりの考え方などを発信しています!
![](https://yosshilifeup.com/wp-content/uploads/2021/12/777ed90894ee438e25fed4ff3608b8f8-300x169.jpg)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/777ed90894ee438e25fed4ff3608b8f8-300x169.jpg)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/Slide1-3-1-300x169.jpg)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/Slide1-3-1-300x169.jpg)
![](https://yosshilifeup.com/wp-content/uploads/2022/01/e3c39e84c65d43ff3c84624033e0b206-300x169.jpg)
![](https://yosshilifeup.com/wp-content/uploads/2022/01/e3c39e84c65d43ff3c84624033e0b206-300x169.jpg)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
Twitterもやってるよ!
![](https://yosshilifeup.com/wp-content/uploads/2022/02/66c86978387dc6abe21e143f280c94bf.jpg)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/66c86978387dc6abe21e143f280c94bf.jpg)
日々の積み重ねや気づいたこと、ブログの更新などを発信しています。
興味を持ってくれたら、是非フォローお待ちしてます!
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
気軽にどうぞ!
コメント