Python中的3D Surface Colormap

时间:2017-05-17 07:59:18

标签: python numpy surface colormap mplot3d

我现在面临另一个问题。我有一个如下数据文件:

# Time  THR-1   ALA-2   PRO-3   VAL-4   PRO-5   MET-6   PRO-7   ASP-8   LEU-9   LYS-10  ASN-11  VAL-12  LYS-13  SER-14  LYS-15  ILE-16  GLY-17  SER-18  THR-19  GLU-20  ASN-21  LEU-22  LYS-23  HIS-24  GLN-25  PRO-26  GLY-27  GLY-28  GLY-29  LYS-30  VAL-31  GLN-32  ILE-33  ILE-34  ASN-35  LYS-36  LYS-37  LEU-38  ASP-39  LEU-40  SER-41  ASN-42  VAL-43  GLN-44  SER-45  LYS-46  CYS-47  GLY-48  SER-49  LYS-50  ASP-51  ASN-52  ILE-53  LYS-54  HIS-55  VAL-56  PRO-57  GLY-58  GLY-59  GLY-60  SER-61  VAL-62  GLN-63  ILE-64  VAL-65  TYR-66  LYS-67  PRO-68  VAL-69  ASP-70  LEU-71  SER-72  LYS-73  VAL-74  THR-75  SER-76  LYS-77  CYS-78  GLY-79  SER-80  LEU-81  GLY-82  ASN-83  ILE-84  HIS-85  HIS-86  LYS-87  PRO-88  GLY-89  GLY-90  GLY-91  GLN-92  VAL-93  GLU-94  VAL-95  LYS-96  SER-97  GLU-98  LYS-99  LEU-100 ASP-101 PHE-102 LYS-103 ASP-104 ARG-105 VAL-106 GLN-107 SER-108 LYS-109 ILE-110 GLY-111 SER-112 LEU-113 ASP-114 ASN-115 ILE-116 THR-117 HIS-118 VAL-119 PRO-120 GLY-121 GLY-122 GLY-123 ASN-124 DA-1    DA-2    DA-3    DC-4    DA-5    DT-6    DG-7    DT-8    DT-9    DA-10   DA-11   DA-12   DC-13   DA-14   DT-15   DG-16   DT-17   DT-18   DT-19   DA-1    DA-2    DA-3    DC-4    DA-5    DT-6    DG-7    DT-8    DT-9    DT-10   DA-11   DA-12   DC-13   DA-14   DT-15   DG-16   DT-17   DT-18   DT-19   
          0.000         84.841          0.274          8.595         -4.939          1.713         -1.704          0.768       -127.825          5.554        108.207          5.297          8.390        212.124          2.830         39.479          8.168          0.458          8.848          6.897        -83.882         29.016          9.647        308.856          6.400         32.481         11.327         10.372          0.247         -3.669         45.391          7.648         -6.990         16.870         11.946         18.778         29.161        127.841         -1.885        -49.943          4.716          6.552         16.029          4.803          7.307          5.423         35.449         -1.362          0.703          0.817          5.544        -14.168         -2.450          0.138         10.984          2.680         -0.238         -0.204         -1.814         -0.273          0.971         -0.256          2.553         -1.172          0.337          0.659         -3.890          8.570          1.180          2.319        -10.711          0.433          0.320          7.904         -0.021          1.672         -0.895         -1.804         -0.317          0.233          0.013          1.462         -1.310         -3.139         -1.453         -4.536          0.559         59.050        -10.891          3.089          5.579          9.818          6.599         -1.635        -34.622          2.576         14.145          9.062        -82.518         51.319         -5.944        -42.734         -0.065          5.200        -18.819         -1.670          0.354         -0.142         -0.938         -4.108         -0.582         -0.511         -0.452          0.763        -21.291          2.587         -5.088         -0.458          5.958         -0.746         -0.587          0.600          6.134          9.432        -47.476          0.517         -0.958         -1.246          0.005         -1.422         -5.105         -2.815         -6.459         -1.618         56.055        117.408         92.845         60.554         -6.065         -9.293         -3.752         -5.407         -1.491         -4.924         -0.944         13.894         32.688         15.937          2.866         -0.934         25.169          1.291         -5.292         -8.727          5.852         -8.092        -40.334        -18.542          0.468         -6.011         -2.043         -1.305         -0.959
         10.000        127.315          0.993         15.230         12.627          0.804          0.642         -2.810       -101.634          5.500        114.097          3.368          9.100        162.819        -10.033         39.935          6.920          9.887          9.732          4.997        -79.368         25.134         -5.714        307.359          5.781         34.996          8.885          7.234         -5.875         -0.094         31.674          3.963         -8.064         14.720         12.726         25.431         25.011        108.108         -0.293        -63.815          4.442          1.071         12.768          2.871          1.451          2.179         30.666         -2.066          0.995          1.496          3.384         -1.398         -0.776         -0.101          5.159          1.092         -0.829         -0.205         -0.125          1.054          0.574         -0.291          1.106          0.875         -1.106         -1.955          1.153          4.273          0.628          1.305         -5.547          0.755          0.126          3.704          0.925          0.074         -0.516          3.643         -0.133         -0.064          0.717          0.547          0.197         -0.408         -0.912         -1.296          0.508         35.027         -3.056         10.216          5.885          8.755         -0.792         -1.442        -28.498          2.122          6.803          1.344        -58.583         47.395         -2.332        -32.863         -2.826          5.311        -23.087          6.478         -0.205          0.288         -0.373          4.358          0.362         -1.010         -0.352          2.271        -13.406         -2.747         -4.616         -2.275          3.943         -4.391         -7.063         -0.599          3.081         12.778        -40.043          0.327         -1.940         -2.012          2.592          2.909          1.041          0.658         -0.868         -3.206         16.355        109.843        107.372         63.801          8.499          0.931          2.639         -0.884          0.214          1.880         -2.379          8.408         12.583         10.883         23.083          7.955         31.277          0.539          3.992         -0.887         12.925         -4.248        -31.420         -4.812          1.125          3.287         -0.532         -0.438          0.291
         20.000         84.636          5.538         15.954         10.437          0.439          1.773         -1.913        -96.625          5.704        132.598         -0.572          6.877        174.628         -9.400         32.417         -0.264          3.812          6.175          5.056        -62.617         25.479         -1.171        288.031          8.114         37.636         10.461          4.612         -3.521         -0.335         37.957          6.596        -11.250         12.510         11.557         21.128         37.344        135.293         -2.163        -80.896          0.912          1.963          1.101          2.815          6.051          5.374         28.443          0.905          1.734          0.813          5.060         -1.365          1.653         -0.415          4.862          1.758         -0.572         -0.339          0.423          0.759          1.036         -0.543          0.783          0.102         -0.971         -1.529         -1.595          5.519          0.587          1.306         -2.813          0.605          0.761          4.542          0.698          0.767         -0.050          2.201         -0.084          0.563          0.357          0.422          0.642          0.588         -1.426         -1.375          1.455         31.332         -3.390         16.696         15.616         13.449          0.096         -2.711        -24.804          1.969          4.095          2.078        -58.303         47.776         -1.047        -22.013         -2.270          4.204        -11.059          3.952          0.382         -0.863          0.010          3.473          0.375         -1.301         -0.037          1.396        -14.392         -2.887         -5.915         -2.315          5.888         -3.365         -5.950         -2.439          4.814          7.125        -46.399          4.393          5.939         -0.508          2.461          2.562         -0.717          4.225          3.642          4.664         27.859        104.835        114.077         74.730          8.410          1.862          0.061         -1.288         -1.181          2.106          4.346          9.017         29.050         -5.088         14.618          4.149          5.062          1.369         15.083          9.537         18.306         -1.165         -8.966          3.864          3.523          7.232          4.275          1.888          4.708
         30.000         91.953         11.008         15.794         12.043          0.596          4.611          1.048        -70.764          7.475         72.100          1.360          6.891        150.455         -7.180         11.932          4.845          9.519          6.184          4.684        -57.283         24.797          0.393        275.626         14.021         22.233         10.877          0.934         -7.551         -2.439         27.929          5.098         -6.797         12.784         12.140         19.698         25.762        108.882          0.267        -54.801          1.470          2.139          1.302          1.996          2.021          3.090         22.690          0.669          1.347          0.113          5.378         -1.570          0.585         -0.143          1.156         -0.050         -1.086          0.148         -0.017         -0.417         -0.201         -1.304          0.808         -0.950         -0.958         -1.741          0.200          2.846          0.633          1.279         -3.693          0.338         -1.058          3.651          0.009          0.202         -1.009          0.037         -0.245         -0.183         -0.615          0.192         -0.386          0.426         -1.800         -2.009          0.496         33.517         -4.213         15.421         16.942         14.559          0.109         -2.553        -25.113          1.199          2.074         -0.265        -56.399         40.657         -0.746        -24.020         -1.986          3.400         -9.631          1.384          0.502         -1.001          0.547          2.622         -0.201         -1.062         -0.916          0.493        -14.621         -2.660         -4.459         -1.066          3.788         -4.289         -7.086          2.460          5.341          8.759        -39.474         -0.051          2.116          0.498          1.267          0.728          1.071          1.155          0.824          3.214         32.413        124.028        144.011         80.795         11.199          5.365          1.969          0.659          2.780          2.311          1.671         14.244         33.170         -6.859         -6.106         13.690          4.742          0.645         17.301         12.245         15.829        -11.976        -22.289          3.100          1.725          5.538          5.041          3.517         -0.205
         40.000        149.956         11.453         22.603         13.125          1.909          5.563          1.533        -90.126          5.479         90.590          4.141          6.652        173.681         -3.703         24.551          3.012         10.247         12.607          7.241        -64.707         21.636         -0.285        276.445          6.223         29.727          8.346          5.092         -5.591         -2.969         27.881          3.581         -6.824         13.884         11.709         21.034         25.732        104.610         -0.237        -54.221          1.960          1.674          2.394          1.727          6.499          3.453         25.335          0.636          0.754         -0.591          5.789         -3.344          1.182         -0.366          0.810          0.901         -0.625         -0.997         -0.241          0.214          0.311         -0.312          0.498         -1.336         -0.911         -1.210         -2.459          3.182          0.599          0.713         -4.273          0.326          0.522          3.207          0.312          0.830         -0.558          1.351         -0.017          0.569         -0.367          0.966         -0.637         -2.392         -2.722         -3.405          0.818         39.708         -2.537         16.297         14.229         10.427          0.837         -1.855        -24.033          0.996          5.579         -1.055        -65.068         48.891         -2.411        -21.785         -2.094          1.285         -3.668          1.264          0.463          0.070         -0.034          2.779          0.115         -0.947          1.107          0.337        -16.009         -3.881         -5.203         -1.503          0.358         -4.410         -8.007         -1.383         10.872         17.390        -47.147          1.140         -2.218         -0.597         -0.312          0.685          1.781          5.662          1.917          1.504         32.806        123.230        132.991         68.245         11.523          3.048          0.389         -0.890          0.170          2.100          1.166         11.693         31.756          2.595         19.844         24.565         30.414         11.828         18.563         22.426         20.596        -13.383        -18.574         -2.142          4.737          1.680          0.071          3.983         -0.001

为此,我试图绘制一个3D色图,其中x轴为时间,y轴为列,z轴为各自的值。

我编写了以下代码来从文件中提取数据并绘制它:

#!/usr/bin/python

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

data = np.loadtxt('contrib_pol.dat', skiprows=1)

x = data[:,0]
y = range(1,len(data[0,:]))
z = []



fig=plt.figure()

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

for r, row in enumerate(data):
    for c, col in enumerate(row[1:], start=1):
        z.append(col)

surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

plt.show()

我遇到了错误:

Traceback (most recent call last):
  File "./barplot.py", line 31, in <module>
    linewidth=0, antialiased=False)
  File "/usr/lib/python2.7/dist-packages/mpl_toolkits/mplot3d/axes3d.py", line 1586, in plot_surface
    X, Y, Z = np.broadcast_arrays(X, Y, Z)
  File "/home/microbio/.local/lib/python2.7/site-packages/numpy/lib/stride_tricks.py", line 250, in broadcast_arrays
    shape = _broadcast_shape(*args)
  File "/home/microbio/.local/lib/python2.7/site-packages/numpy/lib/stride_tricks.py", line 185, in _broadcast_shape
    b = np.broadcast(*args[:32])
ValueError: shape mismatch: objects cannot be broadcast to a single shape
你能帮忙吗?

1 个答案:

答案 0 :(得分:2)

检查display:none的文档字符串,它表明您需要将数据提供为2D数组。使用另外两行代码,您可以使用plot_surface来获取基本网格,并使用numpy.meshgrid以正确的格式获取z值。

numpy.reshape

enter image description here