如何在python中的极坐标中绘制误差线? [英] How to plot error bars in polar coordinates in python?

查看:58
本文介绍了如何在python中的极坐标中绘制误差线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:我想在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()

但是我想添加错误栏,但找不到足够的解决方案.是否已经有一些预构建的 matplotlib 代码?或者有谁知道如何正确定义误差线?据我了解,r-error 只是一条直线,而 theta-error 应该是一段圆.

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.

推荐答案

错误栏的局限性在于,它们是用 hline vline 集合绘制的,因此不能在极坐标中正确旋转(为此存在一个问题, https://github.com/matplotlib/matplotlib/issues/441 ).一个近似的解决方法是让大写的大小为零:

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

如果您希望theta误差线是圆形的,则必须自己实现.最简单的方法是

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.

这篇关于如何在python中的极坐标中绘制误差线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆