0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

matplotlibのerrorbarの凡例のアイコン表示でハマったこと

Posted at

まえがき

修論書くため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()

Untitled.png
横軸の誤差のキャップはなんか途中だし、色がおかしいと困っていた。

解決策

なぜかは分からないが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()

Untitled2.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?