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

matplotlib で複数のグラフを並べて表示する方法

plot.subplots() でグラフの縦方向と横方向の数を指定する。 例 import numpy as np from matplotlib import pyplot as plt x1 = …

no image

matplotlib で、ヒストグラムを作成する方法

ヒストグラムを描くには、data_hist を使う。 ヒストグラムの縦棒の数は bins で指定する。 例 from matplotlib import pyplot as plt data_hist …

no image

python3 でファイルに内容を追記する方法

ファイルを開くとき、「a+」を指定することで、data.txt というファイルを書き込みモードで開く。(もしファイルが存在していなければ作成する。) 例 f = open(‘data.txt’, ‘a …

no image

python3 で辞書(dictionary)を削除する

clear() することによって、辞書が存在した状態のまま、キーと値をすべて削除することができる。 例 dict1 = {“名前”:”太郎”, “年齢”: 20, “住所”: “東京都千代田区大手町1 …

no image

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

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