-1

My goal is to animate the hyper-specific (canonical) diffusion equation by being able to "tell" Python to increment the "t" variable existing in said equation. I have easily done this in Mathematica but need to use Python for my assigned research project. The equation is structured/defined as c(x,y,t), and obviously my question applies for any type of function that c(x,y,t) is set to equal. Every answer related to my question ether:

1) Does not include a function that is not a PDE

2) Consists of not incrementing a time variable (t)

Furthermore, I cannot find any method to graph a 3D equation on Python that is for 2 variables.

EDIT: I have figured out a way to do this.

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

def fun(x, t):
  return x+t #Any equation
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-20.0, 20.0, 0.05)
t = np.arange(0.0,50.0,1)
X, Y = np.meshgrid(x, t)
zs = np.array([fun(x,t) for x,t in zip(np.ravel(X), np.ravel(Y))])
Z = zs.reshape(X.shape)

ax.plot_surface(X, Y, Z)

ax.set_xlabel('X Position')
ax.set_ylabel('Time')
ax.set_zlabel('Concentration')

plt.show()

Credit: Wim I want to use matplotlib to make a 3d plot given a z function

Any help or simple code of an animation procedure would mean a lot, as my research project deals with 7D mathematics and this is essentially the most basic example of a non-trivial representation of what I am trying to do. So expect more questions to come (regardless of an answer).

  • What exactly is the problem? What hinders you to adapt any of the matplotlib animation examples you find around for your case? The code you show neither has any `c(x,y,z,t)` in it. nor does it attempt to animate anything. In this sense this is probably too broad to be answered here. Also note that Stackoverflow cannot do your research project for you (else why would it be *your* project and not the SO community's one?). – ImportanceOfBeingErnest Nov 05 '17 at 22:16
  • @ImportanceOfBeingErnest Indeed, this is not even the beginning of the mathematical creativity that is needed to scratch the surface of the problem. Rather, this is needed for mere presentation of the experimental setup. Digressing, the code I had appended is of the solution of graphing a 3D equation. It is just used to show that I just need to animate that equation. Actually, c(x,y,z,t) is inaccurate: *I would like to view c(x,y,t) as an animation by being able to manipulate the t variable that is already in the equation (refer to code)*. Apologies for the inaccuracy. I will edit that now. – Bobby Sanchez Nov 05 '17 at 22:22
  • So you essentially want to animate a `plot_surface` plot. If you google that, you'll find [How to animate 3d plot_surface in matplotlib](https://stackoverflow.com/questions/17299917/how-to-animate-3d-plot-surface-in-matplotlib). This is hence a duplicate of the already present question. – ImportanceOfBeingErnest Nov 05 '17 at 22:25
  • @ImportanceOfBeingErnest I had looked at that feed previously. The issue with that code is that the time variable is not being incremented, nor is there an equation which involves x, y, and t. So my question is, how do you get that result for a specific equation which already has the time variable in it? That code does not answer this question, because the variable in the equation has its own dependence on the "c" output value (z-coordinates). – Bobby Sanchez Nov 05 '17 at 22:28
  • Of course it is your duty to introduce the `t` variable into the code instead of what is called `framenumber` there. The point is you need to have tried something; so if you implement the code in question and then find a problem, you can ask about it here, but you cannot ask here for just "gimme the codz". – ImportanceOfBeingErnest Nov 05 '17 at 22:47
  • @ImportanceOfBeingErnest I have tried everything but all of my attempts don't seem to work. I tried to run that code and there was an error about defining "size." So I defined it to be say 3. Then there was an error about "ax" not existing. So I changed his/her code to say "from mpl_toolkits.mplot3d import Axes3D as ax". Then, it says that the positional variable "Z" isn't defined. I don't even really understand what the specificities of code are as I am not a heavy coder and I don't get why there are arrays, and where you input or define the equation being graphed. It's all too vague. pls hlp – Bobby Sanchez Nov 05 '17 at 23:01

1 Answers1

0

Ok so let's take the example from this answer. We can easily modify it to use a function c(x,y,t) instead of f(x,y,sig) (those are just variable names).

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

def update_plot(frame_number, zarray, plot):
    plot[0].remove()
    plot[0] = ax.plot_surface(x, y, zarray[:,:,frame_number], cmap="magma")

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

N = 14
nmax=20
x = np.linspace(-4,4,N+1)
x, y = np.meshgrid(x, x)
zarray = np.zeros((N+1, N+1, nmax))

sig = lambda t: 1.5+np.sin(t*2*np.pi/nmax)
c = lambda x,y,t : 1/np.sqrt(sig(t))*np.exp(-(x**2+y**2)/sig(t)**2)

for t in range(nmax):
    zarray[:,:,t] = c(x,y,t)

plot = [ax.plot_surface(x, y, zarray[:,:,0], color='0.75', rstride=1, cstride=1)]
ax.set_zlim(0,1.5)
animate = animation.FuncAnimation(fig, update_plot, nmax, fargs=(zarray, plot))
plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • I think I understand now, thank you very much! Need to do more reading on arrays in Python! Apologies for you having to put up with my being a neophyte/ignoramus! – Bobby Sanchez Nov 06 '17 at 12:35