2

I am trying to make a polar plot of 1/t. What I have so far is below (which may be wrong). How can I finish this or make it work?

from pylab import *
import matplotlib.pyplot as plt

theta = arange(0, 6 * pi, 0.01)


def f(theta):
    return 1 / theta

Polar Plot in Mathematica

4
  • 2
    Check out the matplotlib gallery, which has example plots, and if you click them, will show you the respective source code to generate them. Commented Apr 10, 2013 at 21:27
  • can you specify what you mean by polar plot? Commented Apr 10, 2013 at 21:27
  • @Bitwise I will post a Mathematica picture of it. Commented Apr 10, 2013 at 21:31
  • @mtadd If you would look at the gallery, you would realize that is how some of the code was generated. Commented Apr 10, 2013 at 21:36

3 Answers 3

9

I think the problem is that your first value of f(theta) is 1/0 = inf

theta = np.arange(0, 6*np.pi, .01)[1:] 

def f(x):
    return 1/x

plt.polar(theta, f(theta))

one_over_theta

and it looks even nicer if you zoom in:

zoom

Sign up to request clarification or add additional context in comments.

Comments

3
from mpl_toolkits.axes_grid.axislines import SubplotZero
from matplotlib.ticker import MultipleLocator, FuncFormatter
import matplotlib.pyplot as plt
import numpy as np

plt.ion()

fig = plt.figure(1)
ax = SubplotZero(fig, 111)
fig.add_subplot(ax)

for dir in ax.axis:
    ax.axis[dir].set_visible(dir.endswith("zero"))

ax.set_xlim(-.35,.4)
ax.set_ylim(-.25,.45)
ax.set_aspect('equal')

tick_format = lambda x, i: '' if x == 0.0 else '%.1f' % x
for a in [ax.xaxis, ax.yaxis]:
    a.set_minor_locator(MultipleLocator(0.02))
    a.set_major_formatter(FuncFormatter(tick_format))

theta = np.arange(2*np.pi/3,6*np.pi,0.01)
r = 1 / theta

ax.plot(r*np.cos(theta), r*np.sin(theta), lw=2)

plt.show()
raw_input()

enter image description here

1 Comment

is there a less esoteric way to generate the axis? I can understand the part that plots the function but the axis generation is too complicated for me.
1

If you want a square plot like Mathematica gave you, the standard plot function just takes an array of x values and an array of y values. Here, f(theta) is the radius, and cos and sin give the x and y directions, so

plt.plot(f(theta)*cos(theta), f(theta)*sin(theta))

should do the job. This will show all of the data, rather than a cleverly chosen subset like in Mathematica, so you might want to limit it. For example:

plt.xlim((-0.35,0.43))
plt.ylim((-0.23,0.45))

gives me the ranges in your version.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.