python - Polar plot of a function with negative radii using matplotlib -
the following python code should plot r(theta) = theta on range [-pi/2, pi/2].
import matplotlib.pyplot plt import numpy theta = numpy.linspace(-numpy.pi / 2, numpy.pi / 2, 64 + 1) r = theta plt.polar(theta, r) plt.savefig('polar.png') this produces plot:
however, expect produce:
the negative values of r(theta) seem clipped. how make matplotlib plots negative values of r(theta)?
the first plot seems correct. doesn't show negative values. can overcome explicitely setting limits of r axes.
import matplotlib.pyplot 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 based on assumption quantity should plottable on polar graph, might beneficial technical questions on relative quantities. e.g. 1 might ask deviation of quantity in periodic system mean value. in case convention used matplotlib ideally suited.
from more mathematical (theoretical) perspective 1 might argue negative radii point reflection on origin. in order replicate behaviour, 1 needs rotate points of negative r values π. expected graph question can reproduced following code
import matplotlib.pyplot plt import numpy 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() 



Comments
Post a Comment