How to make a 4d plot using Python with matplotlib

后端 未结 1 1289
粉色の甜心
粉色の甜心 2020-12-01 08:23

I am looking for a way to create four-dimensional plots (surface plus a color scale) using Python and matplotlib. I am able to generate the surface using the first three va

相关标签:
1条回答
  • 2020-12-01 09:06

    To create the plot you want, we need to use matplotlib's plot_surface to plot Z vs (X,Y) surface, and then use the keyword argument facecolors to pass in a new color for each patch.

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
    
    # create some fake data
    x = y = np.arange(-4.0, 4.0, 0.02)
    # here are the x,y and respective z values
    X, Y = np.meshgrid(x, y)
    Z = np.sinc(np.sqrt(X*X+Y*Y))
    # this is the value to use for the color
    V = np.sin(Y)
    
    # create the figure, add a 3d axis, set the viewing angle
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.view_init(45,60)
    
    # here we create the surface plot, but pass V through a colormap
    # to create a different color for each patch
    ax.plot_surface(X, Y, Z, facecolors=cm.Oranges(V))
    

    0 讨论(0)
提交回复
热议问题