Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ 凡例の追加 | プロットのカスタマイズ
Pythonによる究極の可視化
セクション 3.  2
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実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

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

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

セクション 3.  2
single

single

AIに質問する

expand

AIに質問する

ChatGPT

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

some-alt