Polar plot of a function with negative radii using matplotlib

▼魔方 西西 提交于 2019-11-29 12:46:32

The first plot seems correct. It just doesn't show the negative values. This can be overcome by explicitely setting the limits of the r axes.

import matplotlib.pyplot as plt
import numpy

theta = numpy.linspace(-numpy.pi / 2, numpy.pi / 2, 64 + 1)
r = theta

plt.polar(theta, r)
plt.ylim(theta.min(),theta.max())
plt.yticks([-1, 0,1])
plt.show()

This behaviour is based on the assumption that any quantity should be plottable on a polar graph, which might be beneficial for technical questions on relative quantities. E.g. one might ask about the deviation of a quantity in a periodic system from its mean value. In this case the convention used by matplotlib is ideally suited.

From a more mathematical (theoretical) perspective one might argue that negative radii are a point reflection on the origin. In order to replicate this behaviour, one needs to rotate the points of negative r values by π. The expected graph from the question can thus be reproduced by the following code

import matplotlib.pyplot as plt
import numpy as np

theta = np.linspace(-np.pi / 2, np.pi / 2, 64 + 1)
r = theta

plt.polar(theta+(r<0)*np.pi, np.abs(r))

plt.show()

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