matplotlib 3D带状图

时间:2013-04-10 15:56:06

标签: python matplotlib

我正在使用NumPy 1.6.2,SciPy 0.11.0,Matplotlib 1.1.1。我可以在图片中绘制色带吗?

enter image description here

3 个答案:

答案 0 :(得分:3)

这是完整的代码。

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

data=np.genfromtxt('fluorescence_2.txt')
x=data[:,0]
fig=plt.figure()
ax=fig.gca(projection='3d')

for i in range(1,17,2):
    y=data[:,i]
    z=data[:,i+1]
    xi=np.linspace(min(x),max(x))
    yi=np.linspace(min(y),max(y))
    X,Y=np.meshgrid(xi,yi)
    Z=griddata(x,y,z,xi,yi)
    ax.plot_surface(X,Y,Z,rstride=50,cstride=1,cmap='RdYlBu')
    ax.set_zlim3d(np.min(Z),np.max(Z))

ax.set_title('Fluorescence spectra (WL ex = 350 nm)')
ax.set_xlabel('WL em (nm)')
ax.set_ylabel('Spectrum')
ax.set_yticks([])
ax.set_zlabel('Emission')
plt.show()

答案 1 :(得分:1)

在我之前的版本中,必须在加载到脚本之前更改数据表结构。以下版本是我的最后一个版本,它直接从原始数据绘制色带,这是一个简单的吸光度表。

import itertools
import numpy as np
from matplotlib.mlab import griddata
from mpl_toolkits.mplot3d import Axes3D
from pylab import *
matplotlib.rcParams.update({'font.size':10})
spectra=loadtxt('C:/.../absorbance.txt')
fig=figure()
ax=fig.gca(projection='3d')
for i in range(0,7+1):
    y=spectra[:,i]
    x=sorted(range(1,len(y)+1)*2)
    a=[i,i+1]*len(y)
    b=list(itertools.chain(*zip(y,y)))
    xi=np.linspace(min(x),max(x))
    yi=np.linspace(min(a),max(a))
    X,Y=np.meshgrid(xi,yi)
    Z=griddata(x,a,b,xi,yi)
    ax.plot_surface(X,Y,Z,rstride=50,cstride=1,cmap='Spectral')
    ax.set_zlim3d(np.min(Z),np.max(Z))

ax.grid(False)
ax.w_xaxis.pane.set_visible(False)
ax.w_yaxis.pane.set_visible(False)
ax.w_zaxis.pane.set_color('gainsboro')
ax.set_title('Molecular spectra')
ax.set_xlim3d(0,23)
ax.set_xticks([1.6735,6.8367,12.0000,17.1633,22.3265])
ax.set_xticklabels(['350','400','450','500','550'])
ax.set_xlabel('Wavelength (nm)')
ax.set_yticks([0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5])
ax.set_yticklabels(['1','2','3','4','5','6','7','8'])
ax.set_ylabel('Spectrum')
ax.set_zlim3d(0,2)
ax.set_zlabel('Absorbance')
show()

Absorbance

答案 2 :(得分:0)

以下是创建功能区图的工作代码。它基于mplot3d示例代码:surface3d_demo.py,然后进行修改以创建色带。我的代码不是最有效的方法,但它确实有效。

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

#create data
x = np.linspace(-10,5,200)
y = np.linspace(-5,5,40)
xGrid, yGrid = np.meshgrid(y, x)
z = np.sin(np.sqrt(xGrid**2 + yGrid**2))

numPts = x.shape[0]
numSets = y.shape[0]

fig = plt.figure()
ax = fig.gca(projection='3d')

#plot each "ribbon" as a surface plot with a certain width
ribbonWidth = 0.75
for i in np.arange(0,numSets-1):
    X = np.vstack((x,x)).T
    Y = np.ones((numPts,2))*i
    Y[:,1] = Y[:,0]+ribbonWidth
    Z = np.vstack((z[:,i],z[:,i])).T
    surf = ax.plot_surface(X,Y,Z, rstride=1, cstride=1, cmap=cm.jet,
                           linewidth=0, vmin=-1, vmax=1)

ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
ax.set_xlabel('Data Points')
ax.set_ylabel('Data Set Number')
ax.set_ylim((0,numSets))
ax.set_zlabel('Z')
ax.set_zlim((-1, 1))
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()