rohan kundu
rohan kundu

Reputation: 15

Plotting a function using Python where the x and y values are represented in terms of variables

I have a function of the form y = a*cos(2*pi*xc)

Now I would like to plot this from 0 to 10c and get the output in terms of a. This means I would not like to give a specific value to a,b,c rather let the function plot be in terms of a,b,c.

To give you an idea..say,

x = [0, c/4, c/2, 3c/4....10c]

correspondingly

y = [a, 0, -a, 0, ....a]

So the plot will have the x-axis in terms of multiples of c and y -axis will be in multiples of a

Is there a name for plotting in terms of variables like this?

Upvotes: 0

Views: 1306

Answers (1)

fferri
fferri

Reputation: 18940

Not exactly.

The plot contents are always numeric.

But you can multiply x by c, divide y by a (so that your function becomes cos(2*pi*x)), and change the xticks and yticks labels:

import numpy as np
import matplotlib.pyplot as plt

def xtick(x):
    if x == 0: return 0
    if x == int(x): return '%dc' % x
    return ''

x = np.arange(0, 10, 0.25)
y = np.cos(2*np.pi*x)
plt.plot(x, y, 'ro')
plt.xticks(x, map(xtick, x))
plt.yticks([-1, 0, 1], ['-a', '0', 'a'])
plt.show()

Screenshot

Upvotes: 1

Related Questions