Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ 凡例 | セクション
データ可視化&EDA
セクション 1.  10
single

single

book凡例

メニューを表示するにはスワイプしてください

チャートに複数の要素が含まれている場合、凡例を追加することで各要素が何を表しているかを明確にできます。matplotlib には凡例を作成するためのいくつかの方法があります。

最初の方法

すべてのラベルを plt.legend() 内で直接定義できます:

123456789101112131415161718
import numpy as np import matplotlib.pyplot as plt questions = ['question_1', 'question_2', 'question_3'] yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) positions = np.arange(len(questions)) width = 0.3 for i in range(len(answers)): plt.bar(positions + width * i, answers[i], width) plt.xticks(positions + width*(len(answers)-1)/2, questions) plt.legend(['positive answers', 'negative answers']) plt.show()
copy

plt.legend() にラベルのリストを渡すことで、左上隅に凡例を作成。

第二の方法

label= パラメータを使い、プロット関数内で直接ラベルを指定することも可能。

1234567891011121314151617181920
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = [yes_answers, no_answers] labels = ['positive answers', 'negative answers'] width = 0.3 for i in range(len(answers)): plt.bar(positions + width*i, answers[i], width, label=labels[i]) plt.xticks(positions + width*(len(answers)-1)/2, questions) plt.legend() plt.show()
copy

ここでは、plt.legend() がプロットされた要素からラベルを自動的に取得します。

第三の選択肢

返されたアーティストの set_label() メソッドを使用してラベルを設定することも可能です:

12345678910111213141516171819202122
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = [yes_answers, no_answers] width = 0.3 labels = ['positive answers', 'negative answers'] for i in range(len(answers)): bar = plt.bar(positions + width*i, answers[i], width) bar.set_label(labels[i]) center_positions = positions + width*(len(answers)-1)/2 plt.xticks(center_positions, questions) plt.legend(loc='upper center') plt.show()
copy

凡例の位置

loc 引数は凡例の表示位置を制御。デフォルトの 'best' は、matplotlib が自動的に最適な位置を選択。

12345678910111213141516171819202122
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = [yes_answers, no_answers] labels = ['positive answers', 'negative answers'] width = 0.3 for i, label in enumerate(labels): bars = plt.bar(positions + width*i, answers[i], width) bars.set_label(label) center_positions = positions + width*(len(answers)-1)/2 plt.xticks(center_positions, questions) plt.legend(loc='upper center') plt.show()
copy

loc に指定できる値: 'upper right', 'upper left', 'lower left', 'lower right', 'right', 'center left', 'center right', 'lower center', 'center'

Note
さらに学ぶ

さらに詳しく知りたい場合は、 legend() ドキュメント を参照。

タスク

スワイプしてコーディングを開始

  1. 最も低いバーに 'primary sector' というラベルを、適切なキーワード引数を指定して付与。
  2. 中央のバーに 'secondary sector' というラベルを、適切なキーワード引数を指定して付与。
  3. 最も高いバーに 'tertiary sector' というラベルを、適切なキーワード引数を指定して付与。
  4. 凡例を右側の中央に配置。

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  10
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt