Matplotlib忽略了给定的colourmap

时间:2016-09-13 12:22:57

标签: python matplotlib

这是我的情节:

enter image description here

这应该是一个表面图。正如你所看到的那样有些失败。特别是它忽略了传递的颜色图。

它被称为如此:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
# Set global options
plt.rc('text', usetex=True)
plt.rc('font', family='sans-serif')

from scipy.interpolate import griddata
from matplotlib import cm
import numpy as np

class testPlot(object):

    def __init__(self,trajDict):
        # Concat dictionary into (n_i x D) for all i in speeds.
        D = np.vstack(trajDict.values())
        # Grid the data: [time,angle,velocity]
        # time
        self.X = D[:,0]
        # angle
        self.Y = D[:,1]
        # velocity
        self.Z = D[:,2]
        # All vels
        self.vels = [1.42,1.11,0.81,0.50]
    def surfacePlot(self,intMethod,wire=False,surface=False):
        zi = np.linspace(self.Z.min(),self.Z.max(),250)
        xi = np.linspace(self.X.min(),self.X.max(),250)
        yi = griddata((self.X, self.Z), 
                      self.Y,
                      (xi[None,:], zi[:,None]),
                      method=intMethod)

        fig = plt.figure(figsize=(10,10))
        ax = fig.add_subplot(111, projection='3d') #fig.gca(projection='3d')
        zig, xig = np.meshgrid(xi, zi)
        if surface:
            surf = ax.plot_surface(zig, xig, yi, 
                                   cmap='Blues',
                                   alpha=0.5)

        ax.grid(False)
        ax.set_ylabel('Velocity $[m/s]$')
        ax.set_ylim([min(self.vels)-0.2, max(self.vels)+0.2])
        ax.set_axis_off
        ax.set_zlabel('Angle $[\circ]$')
        ax.set_zlim([min(self.Y)-5,max(self.Y)+5])
        ax.set_xlabel('Gait Cycle $[\%]$')
        ax.set_xlim([self.X[0]-10,self.X[-1]])    
        plt.gca().invert_yaxis()
        plt.show()
        # Close existing windows
        plt.close(fig)

传递此MWE数据进行测试:

OrderedDict([('a', array([[ 0.  ,  0.  ,  1.42],
                     [ 1.  ,  1.  ,  1.42],
                     [ 2.  ,  2.  ,  1.42],
                     [ 3.  ,  3.  ,  1.42],
                     [ 4.  ,  4.  ,  1.42]])),
             ('b', array([[ 0.  ,  1.  ,  1.11],
                     [ 1.  ,  2.  ,  1.11],
                     [ 2.  ,  3.  ,  1.11],
                     [ 3.  ,  4.  ,  1.11],
                     [ 4.  ,  5.  ,  1.11]])),
             ('c', array([[ 0.  ,  4.  ,  0.81],
                     [ 1.  ,  5.  ,  0.81],
                     [ 2.  ,  6.  ,  0.81],
                     [ 3.  ,  7.  ,  0.81],
                     [ 4.  ,  8.  ,  0.81]])),
             ('d', array([[  0. ,   9. ,   0.5],
                     [  1. ,  10. ,   0.5],
                     [  2. ,  11. ,   0.5],
                     [  3. ,  12. ,   0.5],
                     [  4. ,  13. ,   0.5]]))])

myTest = testPlot(data)
myTest.surfacePlot('linear',surface=True)

应该给出一个有效的MWE(注意:它不会重现上面显示的图表)。请注意,数据需要采用上述形式才能生效。

1 个答案:

答案 0 :(得分:0)

您的代码很好,显示的图表是曲面图。你可以说,因为背景中的线条位于蓝色区域后面。删除Alpha设置,考虑使用不同的cmap或指定颜色栏的自定义范围以适合您的数据范围。