1

I have a function f(x,t) = cos(t)*t + x and i want to display the change of the result over the width x and time t at discretised time steps t_i and discretised width steps x_j.

Now I am a while here on SX and feel really embarrassed to only can post such little code or in other words nothing (since nothing worked I have done...): Nevertheless if someone has the time to help, I`d appreciate it.

from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as pyplot
from astropy.io.ascii.latex import AASTex

def func(xi, ti):
    res = np.cos(ti)*ti + xi
    return res

timeSpacing = 100
timeStart = 0
timeEnd = 1
time = np.linspace(timeStart, timeEnd, timeSpacing)

widthSpacing = 300
widthStart = 0
widthEnd = 3
width = np.linspace(widthStart, widthEnd, widthSpacing)

resultList = [None]*timeSpacing
resultListInner = [None]*widthSpacing

for i, ithTime in enumerate(time):
    for j, jthWidth in enumerate(width):
        aas = np.zeros_like(width)
        aas.fill(ithTime)
        resultListInner[j] = ithTime, jthWidth, func(jthWidth, aas)
    resultList[i] = resultListInner

So how do I correctly index the list and array and plot my data using matplotlib?


My plot should look like this:

plot

where in my case the aperature should be the width x, the sky annulus is my time t and the RMS is my func(x,t).

4
  • I've read this through a few times and can't figure out what you're trying to do. Here's what I see: a plot y vs t of y=cos(t)*t will be an oscillation within a wedge envelope, and adding x with y=cos(t)*t+x will just add an offset. The offset is so obvious that I can't see why you'd want to plot 300 of these: it will cause cause a whole lot of clutter for very little information. And why do you call x a "width"? Or, maybe easier, can you show a sketch of what you'd like the plot to look like? Commented Jun 20, 2015 at 15:32
  • 1
    Yes this function is trivial for the showcase. The real function is (I think I can say more than) complex and about 4000 lines of code, but in the end only has (a little simplified said) an dependency of the width( x) and the time (t). I will add a example picture what I want my plot to look like. Commented Jun 20, 2015 at 15:38
  • And what are you getting from this code? Commented Jun 20, 2015 at 16:42
  • nothing - I always, no matter how, index the list/array wrong and get different errors. Commented Jun 20, 2015 at 19:14

1 Answer 1

1

A couple of points:

  • Numpy provides a very nice function for doing differences of array elements: diff

  • Matplotlib uses plot_wireframe for creating a plot that you would want (also using Numpy's meshgrid)

Now, combining these into what you may want would look something like this.

from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

def func(xi, ti):
    res = np.cos(ti)*np.sin(xi)
    return res

timeSpacing = 20
timeStart = 0
timeEnd = 1
time = np.linspace(timeStart, timeEnd, timeSpacing)

widthSpacing = 50
widthStart = 0
widthEnd = 3
width = np.linspace(widthStart, widthEnd, widthSpacing)
 

X,T = np.meshgrid(width,time)
F = func(X,T)

DF = np.diff(np.diff(F,axis=0),axis=1)

fig = plt.figure()
ax  = fig.add_subplot(111,projection='3d')

ax.plot_wireframe(X[:-1,:-1],T[:-1,:-1],DF)

plt.show()

Note that diff is applied twice: once in each dimension axis= . I have also changed the toy function you provided to something that actually looks decent in this case.

For your more general use, it seems that you would want to just collect all of your F data into a 2D array, then proceed from the DF = line.

3
  • This looks like a good approach, but why are you using diff; that is, what in the question looks like a diff? Commented Jun 20, 2015 at 20:33
  • "display the change of the result over the width x and time t " -- So interpreting "the result" as F, computing the changes in x and t between each step is exactly what diff will do. Commented Jun 20, 2015 at 20:51
  • Yes this is nearly what I want. But as tom10 suggested I didnt want to plot the change/diff, but the absolut positions ;) I may not have expressed myself very well. Nevertheless thanks ;) Commented Jun 21, 2015 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.