セクション 3. 章 2
single
凡例の追加
メニューを表示するにはスワイプしてください
グラフに複数の要素が含まれている場合、凡例を追加することで各要素が何を表しているかを明確にできます。matplotlib には凡例を作成するためのさまざまな方法があります。
最初の方法
すべてのラベルを plt.legend() 内で直接定義できます:
123456789101112131415161718import 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()
plt.legend() にラベルのリストを渡すことで、凡例が左上隅に作成される。
第二の方法
label= パラメータを使用して、プロット関数内で直接ラベルを指定することも可能。
1234567891011121314151617181920import 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()
ここでは、plt.legend() がプロットされた要素から自動的にラベルを取得します。
第三の方法
返されたアーティストの set_label() メソッドを使ってラベルを設定することもできます。
12345678910111213141516171819202122import 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()
凡例の位置
loc 引数は凡例の表示位置を制御。デフォルトの 'best' は、matplotlib が自動的に最適な位置を選択。
12345678910111213141516171819202122import 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()
loc に指定できる値:
'upper right', 'upper left', 'lower left', 'lower right', 'right', 'center left', 'center right', 'lower center', 'center'。
さらに学ぶ
さらに詳しくは、 legend() ドキュメントをご覧ください。
タスク
スワイプしてコーディングを開始
- 最も低いバーに
'primary sector'というラベルを、適切なキーワード引数を指定して付与。 - 中央のバーに
'secondary sector'というラベルを、適切なキーワード引数を指定して付与。 - 最も高いバーに
'tertiary sector'というラベルを、適切なキーワード引数を指定して付与。 - 凡例を右側の中央に配置。
解答
すべて明確でしたか?
フィードバックありがとうございます!
セクション 3. 章 2
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください