Reputation: 4416
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
Upvotes: 2
Views: 20901
Reputation: 2555
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()
Upvotes: 3
Reputation: 20226
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.
Upvotes: 1
Reputation: 46578
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))
and it looks even nicer if you zoom in:
Upvotes: 9