見出し画像

Axes の中身 -プロットとレジェンド-(matplotlib)

Axes.plot() はプロットした line2d のリストを返す。線が一本で中身だけ受け取りたいときは、変数にカンマ(,)をつければオーケー。

Axes.get_lines() で、Axes にそれまでにプロットされた line2d のリストが得られる。
見た目を一気に整えたい時にいいかも。

プロットする時に line2d に label を指定しておくと、Axes.legend() でそのラベルを使用して凡例が表示される。

#"test_plot.py"
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x  = [0, 1]
y1 = [2, 3]
y2 = [3.5, 2.5]

line1, = ax.plot(x, y1, label = "1")
line2, = ax.plot(x, y2, label = "2")

for line in ax.get_lines():
    line.set_linewidth(2)
    line.set_marker("o")

ax.legend()

plt.show()
test_plot.py の結果

参考URL:
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.plot.html
https://matplotlib.org/stable/api/_as_gen/matplotlib.lines.Line2D.html
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html

この記事が気に入ったらサポートをしてみませんか?