문제

Is it possible to set the same linestyle on matplotlib errorbars as the data points?

In the example below, two lines are plotted. One of them is dashed because of the ls='-.' parameter. However, the errorbars are solid lines. Is it possible to modify the style/look of the errorbars to match the results line?

import matplotlib.pyplot as plt
import numpy as np

x = np.array(range(0,10))
y = np.array(range(0,10))
yerr = np.array(range(1,11)) / 5.0
yerr2 = np.array(range(1,11)) / 4.0

y2 = np.array(range(0,10)) * 1.2

plt.errorbar(x, y, yerr=yerr, lw=8, errorevery=2, ls='-.')
plt.errorbar(x, y2, yerr=yerr2, lw=8, errorevery=3)
plt.show()
도움이 되었습니까?

해결책

It is trivial, changing the linestyle of the errorbars only require a simple .set_linestyle call:

eb1=plt.errorbar(x, y, yerr=yerr, lw=2, errorevery=2, ls='-.')
eb1[-1][0].set_linestyle('--') #eb1[-1][0] is the LineCollection objects of the errorbar lines
eb2=plt.errorbar(x, y2, yerr=yerr2, lw=2, errorevery=3)
eb2[-1][0].set_linestyle('-.')

enter image description here

다른 팁

You can also use the fmt option that is included in the plt.errorbar() command.

For example, in your case:

plt.errorbar(x, y, yerr=yerr, fmt='-.' , lw=8, errorevery=2)
plt.errorbar(x, y2, yerr=yerr2, lw=8, errorevery=3)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top