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 で、グラフの点の見た目を変更する方法

「marker」で指定すればよい。 次のページを参考に、plotの「marker」を変更する。 https://matplotlib.org/stable/api/markers_api.html 例 …

no image

python3 で、フォルダがなければ作成するプログラム

os.mkdir() 関数を使う。 すでにフォルダが存在している場合は、エラーを表示する。 例 import os folder_name = ‘./python_create_folder’ try …

no image

python3 で区切り文字を省略して文字列を分割する

string の split 関数は、区切り文字を指定しない場合には半角スペースや改行・タブ文字を使って自動的に分割してくれる。分割結果はリストで返される。 例 str1 = ‘Hello, nice …

no image

matplotlib で、線の種類を変更する方法

「linestyle = 」として、線の種類を指定することができる設定の詳細は、次のリンクを参照。 https://matplotlib.org/stable/gallery/lines_bars_a …

no image

python3 で辞書からタプルを作成する

辞書に対して、items() メソッドを使うと、タプルを作成することができる。 例 cities = {‘東京’:1000,’大阪’:700, ‘広島’:100} for a,b in cities. …