Python Forum
Plotting axes help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Plotting axes help (/thread-23903.html)

Pages: 1 2


Plotting axes help - StillAnotherDave - Jan-22-2020

Hello,

The question states:

As we will later be acting on the coordinate axes with passive transformations, we will need to have our axes stored in arrays. We can represent the x-axis as two arrays: one containing the x-values ranging from, say, -1 to 1 (you can take 100 equally spaced values) and another containing the y-values (of the x-axis), which are all 0. In a code cell, introduce two NumPy arrays for the x and y values of the x-axis,and similarly two more NumPy arrays for the x and y values of the y-axis. Type code to create a figure of appropriate size (i.e. large enough to look good) and plot the two axes as blue lines. Plot a point with coordinates (x; y) = (0:5; 0:4) as a blue circle.

import numpy as np
import matplotlib.pyplot as plt

x1=np.linspace(-1, 1, 100)
y1=np.linspace(-1, 1, 100)
Any help to move forward with this question ...


RE: Plotting axes help - jefsummers - Jan-23-2020

You need an array of zeros as well, such as z=np.zeros(100)
Then, use plt.plot(x1,z) and plt.plot(z,y1) to plot your axes.
Then use plt.plot() to plot the blue circle.


RE: Plotting axes help - StillAnotherDave - Jan-23-2020

(Jan-23-2020, 01:26 AM)jefsummers Wrote: You need an array of zeros as well, such as z=np.zeros(100)
Then, use plt.plot(x1,z) and plt.plot(z,y1) to plot your axes.
Then use plt.plot() to plot the blue circle.

Ah great! I've used the above and continued onto the next part of the question (see below):

x1=np.linspace(-1, 1, 100)
x2=np.zeros(100)
y1=np.linspace(-1, 1, 100)
y2=np.zeros(100)

plt.plot(x1, x2)
plt.plot(y2, y1)
plt.plot(0.5, 0.4, 'bo:')

xprime=0.5*np.cos(np.pi/6)-0.4*np.sin(np.pi/6)
yprime=0.5*np.sin(np.pi/6)+0.4*np.cos(np.pi/6)
plt.plot(xprime, yprime, 'ro:')
The above code gives the initial coordinate (x,y) = (0.5,04) as an "active rotation" of the point by 30 degrees.

I next need to be able to produce the same result by a "passive rotation" of the coordinate axes instead of moving the point ....?

The question reads:

Let us now consider a passive rotation. We will leave our point alone and just rotate the axes. You still apply the same formula as above, but now not on a single point, but on all points that make our axes. Thus your x and y are now the arrays containing the x-values and y-values of the axis you are rotating. Copy the previous code and add new code to compute the new x-axis and the new y-axis that result from a rotation by 30 degrees. Plot the new axes as red lines. You should now have a plot with the original axes in blue, the original point in blue, and the new axes in red.


RE: Plotting axes help - jefsummers - Jan-23-2020

Looks to me like you need to copy the arrays, then apply the formula to all the points in the arrays, and plot the new arrays (axes)


RE: Plotting axes help - StillAnotherDave - Jan-23-2020

(Jan-23-2020, 01:04 PM)jefsummers Wrote: Looks to me like you need to copy the arrays, then apply the formula to all the points in the arrays, and plot the new arrays (axes)

Hmm ... I think I understand. Let me try that. Thanks!


RE: Plotting axes help - StillAnotherDave - Jan-23-2020

In the original code, the x and y axes are produced from four arrays:

x1=np.linspace(-1, 1, 100)
x2=np.zeros(100)
y1=np.linspace(-1, 1, 100)
y2=np.zeros(100)

plt.plot(x1, x2, 'b')
plt.plot(y2, y1, 'b')
plt.plot(0.5, 0.4, 'bo:')
Is there a way to apply the formula to these arrays in two lines? i.e. rotating x1 and x2 in one step and y1 and y2 in one step ...?

I've tried the below. It gives me the two points and the blue (x, y) axes ... but it only gets me the x axis rotated in red:

x1=np.linspace(-1, 1, 100)
x2=np.zeros(100)
y1=np.linspace(-1, 1, 100)
y2=np.zeros(100)

plt.plot(x1, x2, 'b')
plt.plot(y2, y1, 'b')
plt.plot(0.5, 0.4, 'bo:')
x1prime=x1*np.cos(np.pi/6)-y1*np.sin(np.pi/6)
y1prime=y1*np.sin(np.pi/6)+x1*np.cos(np.pi/6)

plt.plot(x1prime, y1prime, 'r')



RE: Plotting axes help - jefsummers - Jan-23-2020

That's because you haven't calculated and plotted x2prime and y2prime from x2 and y2. Do that too.


RE: Plotting axes help - StillAnotherDave - Jan-24-2020

Yep ... this seems to do the job:

x1=np.linspace(-1, 1, 100)
x2=np.zeros(100)
y1=np.linspace(-1, 1, 100)
y2=np.zeros(100)

plt.plot(x1, x2, 'b')
plt.plot(y2, y1, 'b')
plt.plot(0.5, 0.4, 'bo:')

x1prime=x1*np.cos(-np.pi/6)-x2*np.sin(-np.pi/6)
x2prime=x1*np.sin(-np.pi/6)+x2*np.cos(-np.pi/6)

y2prime=y2*np.cos(-np.pi/6)-y1*np.sin(-np.pi/6)
y1prime=y2*np.sin(-np.pi/6)+y1*np.cos(-np.pi/6)

plt.plot(x1prime, x2prime, 'r')
plt.plot(y2prime, y1prime, 'r')



RE: Plotting axes help - StillAnotherDave - Jan-24-2020

Almost there!

I have modified my code so that values in degrees can be used (and are converted to radians for the formulae):

x1=np.linspace(-1, 1, 100)
x2=np.zeros(100)
y1=np.linspace(-1, 1, 100)
y2=np.zeros(100)

plt.plot(x1, x2, 'b')
plt.plot(y2, y1, 'b')
plt.plot(0.5, 0.4, 'bo:')

#%% Active Rotation
xdeg=30
xrad=np.radians(xdeg)

xprime=0.5*np.cos(xrad)-0.4*np.sin(xrad)
yprime=0.5*np.sin(xrad)+0.4*np.cos(xrad)
plt.plot(xprime, yprime, 'ro:')

#%% Passive Rotation
x1prime=x1*np.cos(xrad)-x2*np.sin(xrad)
x2prime=x1*np.sin(xrad)+x2*np.cos(xrad)

y2prime=y2*np.cos(xrad)-y1*np.sin(xrad)
y1prime=y2*np.sin(xrad)+y1*np.cos(xrad)

plt.plot(x1prime, x2prime, 'r')
plt.plot(y2prime, y1prime, 'r')
Now, I need to use a float slider to be able to test different angle rotations (active and passive). This is my attempt:

import ipywidgets as widgets
xdeg1 = widgets.FloatSlider(min=-180, max=180, step=1)
xdeg2 = widgets.FloatSlider(min=-180, max=180, step=1)
display(xdeg1, xdeg2)

xrad1=np.radians(xdeg1.value)

xrad2=np.radians(xdeg2.value)

xprime=0.5*np.cos(xrad1)-0.4*np.sin(xrad1)
yprime=0.5*np.sin(xrad1)+0.4*np.cos(xrad1)
plt.plot(xprime, yprime, 'ro:')

x1prime=x1*np.cos(xrad2)-x2*np.sin(xrad2)
x2prime=x1*np.sin(xrad2)+x2*np.cos(xrad2)

y2prime=y2*np.cos(xrad2)-y1*np.sin(xrad2)
y1prime=y2*np.sin(xrad2)+y1*np.cos(xrad2)

plt.plot(x1prime, x2prime, 'g')
plt.plot(y2prime, y1prime, 'g')
I haven't used jupyter or sliders before ... how do I get the slider values (in degrees) to commute to the code below - i.e. convert to radians and then use in the formulae ...??

To complete this task:

Finally, compute the *trajectory* of our blue point under the active transformation, using
arrays for, say, 100 points, starting from the original blue point, and ending at the final
red point. Plot this trajectory as a dashed red line.


RE: Plotting axes help - StillAnotherDave - Jan-24-2020

Hi folks,

Will need to submit by 5pm this afternoon. So if anyone has any help on the above please do share. Many thanks.

Only this outstanding:

"Finally, compute the *trajectory* of our blue point under the active transformation, using arrays for, say, 100 points, starting from the original blue point, and ending at the final red point. Plot this trajectory as a dashed red line."