Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot error bars in polar coordinates in python?

I have the following problem: I want to plot some data points in polar coordinates in python, which is easy, using some code like

import numpy as np
import matplotlib.pyplot as plt

r = 1e04 * np.array([5.31,5.29,5.25,5.19,5.09,4.92,4.67,4.27,3.75,3.56])
theta = 2*np.pi/360 * np.array(list(range(0, 100, 10)))

plt.polar(theta, r, "ro")

plt.show()

but I want to add error bars and I don't find any sufficient solution. Is there already some prebuild matplotlib-code? Or does anyone know how to define the error bars properly? As I understand it, the r-error is just a straight line while the theta-error should be a segment of a circle.

like image 575
Madao Avatar asked Oct 27 '14 08:10

Madao


1 Answers

On limitation of errorbar is that the caps are drawn with hline and vline collections so the caps to not properly rotate in polar coordinates (there is an issue open for this, https://github.com/matplotlib/matplotlib/issues/441 ). An approximate workaround is to just make the caps have zero size:

import numpy as np
import pylab as plt

fig = plt.figure()
ax = plt.axes(polar=True)

r =  np.array([5.31,5.29,5.25,5.19,5.09,4.92,4.67,4.27,3.75,3.56])
theta = 2*np.pi/360 * np.array(list(range(0, 100, 10)))

ax.plot(theta, r, "ro")
ax.errorbar(theta, r, yerr=1, xerr=.1, capsize=0)

plt.show()

polar plot with error bars

If you want the theta error bars to be circular you will have to implement that your self. The easiest way is

th_err = 1

for th,  _r in zip(theta, r):
    local_theta = np.linspace(-th_err, th_err, 15) + th
    local_r = np.ones(15) * _r
    ax.plot(local_theta, local_r, color='k', marker='')

plt.show()

For small errors this won't really make a difference, but will matter for large errors.

like image 103
tacaswell Avatar answered Oct 11 '22 16:10

tacaswell