AsukaMinato
AsukaMinato

Reputation: 1432

How to draw parametric region with matplotlib?

For example: if I want to draw x= cos(t), y=sin(t) t=0..2pi

x=cos(linspace(0,2*pi,100))
y=sin(linspace(0,2*pi,100))
plot(x,y)

but if I want to plot

x= r*cos(t), y=r* sin(t) t=0..2pi , r=0..3

How to do it?

what I want is this:

enter image description here

Since there are two vars: t and r, so the graph is shadow instead of curve. Now I plot it like this:

from pylab import *
t = linspace(0, 3, 100)

for r in linspace(0, 2*pi, 100):
    x = r*sin(t)
    y = r*cos(t)
    plot(x, y)
show()

I have to write a loop to plot each curve, which I feel not so elegant.

Maybe it's the only way to plot it?

Upvotes: 0

Views: 216

Answers (1)

Ynjxsjmh
Ynjxsjmh

Reputation: 30032

Just use a for loop:

import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize=(9, 3))

axes1 = plt.subplot()

t = np.linspace(0, 2*np.pi, 100)

for r in range(1, 3):
    x = r * np.cos(t)
    y = r * np.sin(t)
    axes1.plot(x, y)

plt.tight_layout()
plt.show()

enter image description here

You can also define the step(default is 1) from start to stop in range function. -> range(start, stop[, step])

Note: range(start, stop) will generate numbers in [start, stop).

Upvotes: 1

Related Questions