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 で文字列を入力させて受け取る方法

C言語で scanf のようなものを作るには、python では input() で実現できる。 例 print(‘xの値を入力してください’) x = input() print(‘xの値は ‘ + …

no image

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

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

no image

python3 でコンソールからの入力をリストに格納する方法

入力を受け取るには、input() を使う。range(0,5) で、5個の文字列を順に受け取り、リストに入れてそれを表示する。 例 list1 = [] for i in range(0, …

matplotlib のグラフ作成と gnuplot との対応 比較

起動方法 gnuplot コマンドで $ gnuplot とすれば起動できる。 初期設定ファイルは gnuplotrc にある。 gnuplotrc の場所の見つけ方(macの場合) https:// …

no image

python のプロンプトについて

UNIX で python を起動する ターミナルで $ python インタープリターのプロンプトは >>> となっている。 インタープリタを終了させるときは、Ctrl + D を押す。