Skip to Content
Pythonデータサイエンスハンドブック 第2版 ―Jupyter、NumPy、pandas、Matplotlib、scikit-learnを使ったデータ分析、機械学習
book

Pythonデータサイエンスハンドブック 第2版 ―Jupyter、NumPy、pandas、Matplotlib、scikit-learnを使ったデータ分析、機械学習

by Jake VanderPlas, 菊池 彰
February 2024
Intermediate to advanced
576 pages
9h 17m
Japanese
O'Reilly Japan, Inc.
Content preview from Pythonデータサイエンスハンドブック 第2版 ―Jupyter、NumPy、pandas、Matplotlib、scikit-learnを使ったデータ分析、機械学習

29章凡例のカスタマイズ

プロットの凡例は、さまざまなプロット要素に意味を割り当て、可視化の結果をわかりやすくします。簡単な凡例を作成する方法はすでに紹介しました。ここでは、Matplotlibの凡例の配置と見た目をカスタマイズする方法を説明します。

最も単純な凡例は、plt.legendで作成できます。この関数は、ラベル付きプロット要素の凡例を自動的に作成します(図29-1を参照)。

In [1]: import matplotlib.pyplot as plt
        plt.style.use('seaborn-whitegrid')

In [2]: %matplotlib inline
        import numpy as np

In [3]: x = np.linspace(0, 10, 1000)
        fig, ax = plt.subplots()
        ax.plot(x, np.sin(x), '-b', label='Sine')
        ax.plot(x, np.cos(x), '--r', label='Cosine')
        ax.axis('equal')
        leg = ax.legend()
デフォルトの凡例

図29-1 デフォルトの凡例

凡例のカスタマイズ方法はさまざまです。例えば、位置の指定や枠線の追加が可能です(図29-2を参照)。

In [4]: ax.legend(loc='upper left', frameon=True)
        fig

図29-2 凡例のカスタマイズ

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Pythonデータサイエンスハンドブック ―Jupyter、NumPy、pandas、Matplotlib、scikit-learnを使ったデータ分析、機械学習

Pythonデータサイエンスハンドブック ―Jupyter、NumPy、pandas、Matplotlib、scikit-learnを使ったデータ分析、機械学習

Jake VanderPlas, 菊池 彰
推薦システム実践入門 ―仕事で使える導入ガイド

推薦システム実践入門 ―仕事で使える導入ガイド

風間 正弘, 飯塚 洸二郎, 松村 優也
プロダクトマネージャーのしごと 第2版 ―1日目から使える実践ガイド

プロダクトマネージャーのしごと 第2版 ―1日目から使える実践ガイド

Matt LeMay, 永瀬 美穂, 吉羽 龍太郎, 原田 騎郎, 高橋 一貴

Publisher Resources

ISBN: 9784814400638Publisher Website