Matplotlib: slicing a 3D Mesh grid to 2D and do countor plot

Viewed 52

I have a function that has a dependency on the x and y position and on time.

I am using using sympy for doing some symbolic computations and afterwards using the lamdify function to plot the results.

The function I have is:

T = sym.cos(x**2 + y**2 + t**2)

I would like to have 2 2D plots plotting x vs time -> colored by T and y vs time -> colored by T

Currently I have,

import numpy as sp
import numpy as np
from matplotlib import pyplot as plt
x, y, t =sym.symbols("x y t")
T = 0.5*sym.cos(x**2 + y**2 + t**2)

# Function for plotting
def myContourPlot(x_list, y_list, time_list, f):
    x_value, y_value, time_value = np.meshgrid(x_list, y_list, time_list)
    
    fig,ax=plt.subplots(1,1)
    z = f(x_value, y_value, time_value)
        
    cp = ax.contourf(x_value[0, :, :], time_value[0, :, :], z[0, :, :], np.linspace(np.min(z[0, :, :]), np.max(z[0, :, :]),100) )
    fig.colorbar(cp) # Add a colorbar to a plot
    ax.set_title('Contour plot')
    ax.set_xlabel('x [m]')
    ax.set_ylabel('time [s]')
    plt.show()
    
    fig2,ax2=plt.subplots(1,1)
    cp2 = ax2.contourf(y_value[:, :, 0], time_value[:, :, 0], z[:, :, 0], np.linspace(np.min(z[:, :, 0]), np.max(z[:, :, 0]),100) )
    fig2.colorbar(cp2) # Add a colorbar to a plot
    ax2.set_title('Contour plot')
    ax2.set_xlabel('y')
    ax2.set_ylabel('time')
    plt.show()

x_list = np.linspace(0,1, 100 )
y_list = np.linspace(0,1.5, 100 )
time_list = np.linspace(0,3, 100 )
f = sym.lambdify((x, y, t), T)

myContourPlot(x_list, y_list, time_list, f)

I would like to know how to slice the meshgrid array to get x vs time colored by the corresponding T value and the same for y vs time.

Currently, the x vs time is colored but the y vs time is not.

Any help would be appreciated!

0 Answers
Related