Matplotlib:颜色分配不正确

时间:2018-08-15 12:02:35

标签: python matplotlib colors colorbar

我已经在这里问了一个问题,并且得到了很大的帮助:Creating a Surface of Revolution

这是代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib import cm

#from matplotlib.patches import Circle

ri = 100
ra = 300
h=20

# input xy coordinates
xy = np.array([[ri,0],[ra,0],[ra,h],[ri,h],[ri,0]])
# radial component is x values of input
r = xy[:,0]
# angular component is one revolution of 30 steps
phi = np.linspace(0, 2*np.pi, 50)
# create grid
R,Phi = np.meshgrid(r,phi)
# transform to cartesian coordinates
X = R*np.cos(Phi)
Y = R*np.sin(Phi)
# Z values are y values, repeated 30 times
Z = np.tile(xy[:,1],len(Y)).reshape(Y.shape)


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

ax.set_zlim(0,200)
ax.plot_surface(X, Y, Z, alpha=0.5, color='grey', rstride=1, cstride=1, linewidth=0, edgecolor='none')

arr = np.array([[100, 15],
               [114.28, 17],
               [128.57, 18],
               [142.85, 24],
               [157.13, 26],
               [171.13, 28],
               [185.69, 29],
               [199.97, 30],
               [214.25, 31],
               [228.53, 32],
               [242.81, 35],
               [257.09, 36],
               [271.37, 37],
               [288.65, 40]])

#interpolating between the single values of the arrays
new_x = np.concatenate([np.linspace(arr[i,0],arr[i+1,0], num=20)
                        for i in range(len(arr)-1)])

new_y = np.interp(new_x, arr[:,0], arr[:,1])
t=np.arange(260)

tmp_phi = np.linspace(0,2*np.pi,20)[:,None] # angle data
linesurf_x = new_x*np.cos(tmp_phi)
linesurf_y = new_x*np.sin(tmp_phi)
linesurf_z = np.broadcast_to(new_y, linesurf_x.shape)

linesurf_c = np.broadcast_to(t, linesurf_x.shape) # color according to t
colors = cm.jet(linesurf_c/linesurf_c.max()) # grab actual colors for the surface
ax.plot_surface(linesurf_x, linesurf_y, 1.5*linesurf_z, facecolors=colors,
                rstride=1, cstride=3, linewidth=1, edgecolor='none')


cax, _ = mpl.colorbar.make_axes(plt.gca(), shrink=0.8)
cax.yaxis.set_ticks_position('right')
cbar = mpl.colorbar.ColorbarBase(cax, cmap='jet', label='test',
                       norm=mpl.colors.Normalize(vmin=15, vmax=41))
plt.show()

但是现在我意识到这是一个错误。我想为数组的每个值分配颜色(第二个条目):15,17,18,24,26,28,29 ..... 如果我现在将值29更改为15,除了会在半径185.69处获得深蓝色外,其他情况除外。但是我得到这个: enter image description here

0 个答案:

没有答案