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:

polar plot

however, expect produce:

expected polar plot

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

enter image description here

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

enter image description here


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -