简体   繁体   中英

polar plot in python

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

Mathematica中的极地情节

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:

放大

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

在此输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM