matplotlib python3

matplotlib で、凡例を表示する位置を変更する方法

投稿日:

凡例(legend)の表示位置を変更するには、axis の legend の loc を設定する。

次のリンクhttps://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.htmlで、「loc」のところに設定可能な場所の一覧が書かれている。

以下の例では、「center right」に設定している。

import numpy as np
from matplotlib import pyplot as plt

x = np.linspace(-10,10,100)
y1 = np.sin(x)
y2 = np.cos(x)

fig, ax = plt.subplots()
ax.scatter(x,y1, s=100, alpha=0.3,label="sin")
ax.scatter(x,y2, s=100, alpha=0.3,label="cos")
ax.legend(loc='center right')

plt.show()

結果

-matplotlib, python3
-,

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

python3 でタプルを指定した要素で順に並び替える

タプルを、指定した要素で並び替えることができる。「key=lambda x 」で、引数 x を指定。lambda は無名関数を表す。 例 record = [(‘織田信長’, 1534, 15 …

no image

python3 で、dictionary のキー一覧を取得する方法

dict.keys() でキーを取得することができる。 参考:dictionary の値一覧を取得するときは、dict.values() を使う。 例 print(dict.keys())

no image

matplotlib のプロットスクリプト内で計算する方法

gnuplot の場合 gnuplot では、plot “data.txt” u 1:($2*2)などと書くと、 数値を加工(データ値を使った計算)を行った結果をグラフ化するこ …

no image

python でファイルを1行おきに読み込む方法

readline を使って次のように書く。 例 ファイル:data.txt # id name age 1 佐藤太郎 10 2 鈴木花子 18 3 坂本明美 21 4 松村光子 24 5 小川奏子 1 …

no image

python3 の辞書で、キー一覧を取り出して表示する方法

keys()メソッドでは、キー一覧を取得できる。values() では、値を取得できる。items() では、キーと値をタプルとして取得できる。 リストに変換すると、print() でコンソールに表示 …