まえがき
修論書くためmatplotlibで図表を作っていた時にハマったけど、検索してもあまり日本語の情報がなかったために、備忘録的に書いておきます。
ハマったこと
横軸と縦軸の両方に誤差があるタイプの誤差付き散布図を作ろうとしていたときに、
凡例のアイコンの表示が上手くいかなかった。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
labels=['tekitou1','tekitou2','tekitou3']
x=[1., 2., 3.]
y=[1., 2., 3.]
xerr=[0.5, 0.2, 0.6]
yerr=[0.5, 0.2, 0.4]
fig = plt.figure(figsize=(6,4))
ax = fig.add_subplot()
for i in np.arange(len(x)):
ax.errorbar(x[i],y[i],yerr=yerr[i],xerr=xerr[i],
label=labels[i], capsize=4, marker='o',
ecolor='blue', color="red")
ax.legend(fontsize=18)
plt.show()
横軸の誤差のキャップはなんか途中だし、色がおかしいと困っていた。
解決策
なぜかは分からないがerrorbarの引数の中にls=''
を追加すると直る。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
labels=['tekitou1','tekitou2','tekitou3']
x=[1., 2., 3.]
y=[1., 2., 3.]
xerr=[0.5, 0.2, 0.6]
yerr=[0.5, 0.2, 0.4]
fig = plt.figure(figsize=(6,4))
ax = fig.add_subplot()
for i in np.arange(len(x)):
ax.errorbar(x[i],y[i],yerr=yerr[i],xerr=xerr[i],
label=labels[i], capsize=4, marker='o',
ecolor='blue', color="red", ls='')
ax.legend(fontsize=18)
plt.show()