グラフの凡例(legend)を表示すると、大体マーカー・ハンドルが左、ラベルが右という配置になる。これを入れ替えたくなった。
調べてみるとmarkerfirstという引数があり、これをFalseにすると左右が逆転する。
なるほど確かに逆転した。
import numpy as np import matplotlib.pyplot as plt y = np.array([1, 2, 3, 4]) fig = plt.figure(figsize=(3, 3), facecolor="white") ax1 = fig.add_subplot(111) ax1.bar(y, y, label="y") #凡例の挿入 ax1.legend(markerfirst=False)#markerfirstをFlaseにすると、文字が先になる。 plt.show()
ちなみに凡例の中のオブジェクトの名前はhandle(線)、marker(点)、label(文字)だそうです。何か調べものする際にはご活用ください。
参考サイト
https://stackoverflow.com/questions/50916067/how-to-modify-matplotlib-legend