Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
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? –  tom10 19 hours ago
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. –  user69453 19 hours ago
    
And what are you getting from this code? –  hd1 18 hours ago
    
nothing - I always, no matter how, index the list/array wrong and get different errors. –  user69453 15 hours ago

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
This looks like a good approach, but why are you using diff; that is, what in the question looks like a diff? –  tom10 14 hours ago
    
"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. –  KevinG 14 hours ago
    
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 ;) –  user69453 34 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.