How to plot error bars in polar coordinates in python?

北慕城南 提交于 2019-12-10 14:07:42

问题


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.


回答1:


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()

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.




回答2:


I would have recommended something like this:

import numpy as np
import pylab as plt

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

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)))

ax.plot(theta, r, "ro")
ax.errorbar(theta, r, xerr=0.5, yerr=0.4)

plt.show()

But there seeems to be some problem. I dont know if it is inherent to pylab. Kind of at a loss as to what to do :)



来源:https://stackoverflow.com/questions/26583620/how-to-plot-error-bars-in-polar-coordinates-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!