Search code examples
matplotliberrorbar

Polarplot errorbar maplotlib


I am stuck with a trivial problem on how to make in a polar plot the error bars radial. Here is my code:

#definition of the inerpolated red curve
tck = interpolate.splrep(phi_mol1D, MFPAD,s=0)
xnew = np.arange(-180, 180, 0.5)
ynew = interpolate.splev(xnew, tck)

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
#plotting the red curve
plt.plot(xnew/180.*np.pi, ynew, c="r", alpha=0.5)
#error bars at each point
plt.errorbar(phi_mol1D/180.*np.pi, MFPAD, yerr=MFPADerr, mec='green', fmt="o", color="b", lw= 20, ms=5) 
#adjustments
ax.set_rticks([2000,4000,6000, 8000])
ax.set_yticklabels([])
ax.grid(True)

The result is the following, where the error bars aretangential i.e. rotated 90° compared to what I would expect. I have just noticed that the thickness of the errorbars is proportional to their distance from the centre, which shouldn't be.

polar plot with errorbars

I have seen this attempt to rotate makers using transform=Affine2D().rotate_deg(90), but it doesn't work with my syntax.

PS. is it possible to move the labels of the angle radially, so they don't overlap with the graph?

Thank you for your help!


Solution

  • The answer was shockingly trivial: the absolute values of the yerr was very small and the parameter lw = 20 is for the width of the bar. The corrected version would be something like:

    plt.errorbar(phi_mol1D/180.*np.pi, MFPAD, yerr=MFPADerr*10, mec='green', fmt="o", color="b", ms=5) 
    

    which returns this:

    corrected version with the right errobars

    if anyone knows how to move the degree labels radially, that would be awesome. I apologize for the unuseful post.