When plotting data via plt.plot
and plt.errorbar
it happens that the plt.errorbar
has a higher layer of display, meaning that it is always plotted in front of plt.plot()
data.
For using:
plt.plot(x, data_1, color='r')
plt.errorbar(x, data_2 ,yerr= std_2, color='b')
The errorbars and value points overlap the red data as expected, because the errorbar was plotted afterwards:
plt.errorbar plotted after plt.plot
But if you now use
plt.errorbar(x, data_2 ,yerr= std_2, color='b')
plt.plot(x, data_1, color='r')
The points of the plt.errorbar() are still a layer of display before the plt.plot() data:
plt.plot plotted after plt.errorbar
especially when looking at the whole plot (zooming out) the plt.plot() data with already unreasonably big markersize can be barely recognised:
I tried using plt.scatter instead of plt.plot, but the layer of display problem is the same here
for both (plt.plot and plt.scatter) I clearly expected, that their layer of display is higher when putting them below the plt.errorbar() function in the code
You should use the barsabove
argument of the errorbar
. If you just do, e.g.:
import numpy as np
from matplotlib import pyplot as plt
x = np.linspace(0, 4, 15)
y = x
yerr = 0.5
fig, ax = plt.subplots()
ax.errorbar(x, y, yerr=yerr, mfc="b", marker="o", ms=15, ls="none", capsize=2)
ax.plot([1.5, 2.5], [1.5, 2.5], marker="o", ls="none", mfc="r", mec="r", ms=50)
you will get something similar to what you are seeing:
If you add barsabove=True
, you will instead get:
x = np.linspace(0, 4, 15)
y = x
yerr = 0.5
fig, ax = plt.subplots()
ax.errorbar(x, y, yerr=yerr, mfc="b", marker="o", ms=15, ls="none", capsize=2, barsabove=True)
ax.plot([1.5, 2.5], [1.5, 2.5], marker="o", ls="none", mfc="r", mec="r", ms=50)
You can also investigate using zorder
for fine grained control of the plotting order.