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 で、y軸に二種類の軸を設定する方法

y軸に、2種類のグラフを異なるスケールでプロットする。 matplotlib axes の twinx() を使う。 https://matplotlib.org/stable/api/_as_gen …

no image

pythonで配列(リスト)の、ある要素がわかっているときにその次の要素を取得する方法。

next() を使う。 例 my_arr = [‘春’,’夏’,’秋’,’冬’] my_iter = iter(my_arr) print(next(my_iter,’なし’)) print(next …

no image

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

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

no image

matplotlib でグラフ表示ウィンドウの画面上の位置を自由に設定する方法

matplotlib.use(‘TkAgg’) としておき、 get_current_fig_manager().window.wm_geometry(“+20+50”) として、(+20+50)のと …

no image

python の __str__ と __repr__ とは

python でクラスの情報を文字列で表示するとき、__str__ メソッドと __repr__ メソッドが使える。__str__ は、プログラマーとは限らないユーザーに「読める形で情報を表示」するこ …