问题
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