如何在Python中制作3d条形图

时间:2017-05-15 07:59:59

标签: python numpy matplotlib bar-chart scientific-computing

我有一个如下数据文件:

         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

第一列需要进入x轴,而第二列到结尾的标题是y轴,第二列到最后一列的值是条形的高度(即z轴)。

我需要在Python中这样做,我是一个新手。你能帮忙吗?

我尝试了以下代码,但没有用:

#!/usr/bin/python

import numpy as nm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D


data=nm.loadtxt('contrib_pol.dat')
fig=plt.figure()
ax=fig.add_subplot(111,projection='3d')



ax.bar(data[:,0],data[0,:],data[:,1:124])
plt.show()


del data

它正在闪烁此错误消息:

Traceback (most recent call last):
  File "./barplot.py", line 14, in <module>
    ax.add_collection3d(data[:,0],data[0,:],data[:,1:124])
  File "/usr/lib/python2.7/dist-packages/mpl_toolkits/mplot3d/axes3d.py", line 2210, in add_collection3d
    Axes.add_collection(self, col)
  File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 1669, in add_collection
    label = collection.get_label()
AttributeError: 'numpy.ndarray' object has no attribute 'get_label'

另外,我认为我没有根据要求正确选择列。请帮忙。

1 个答案:

答案 0 :(得分:0)

以下内容应该有助于您入门:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Extract the header from the data
#import csv
#with open('contrib_pol.dat', 'rb') as f_data:
#    header = next(csv.reader(f_data, delimiter='\t'))[1:]

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

x = []
y = []
dz = []

cols = ['red', 'blue', 'yellow', 'green']
colours = []

for r, row in enumerate(data):
    for c, col in enumerate(row[1:], start=1):
        x.append(row[0])
        y.append(c)
        dz.append(col)
        colours.append(cols[r])

z = np.zeros(len(y))
dx = np.ones(len(y))
dy = np.ones(len(y))

ax3d = plt.figure().gca(projection='3d')
ax3d.bar3d(x, y, z, dx, dy, dz, color=colours)

ax3d.set_xlabel('Time')
ax3d.set_ylabel('Col')
ax3d.set_zlabel('Value')

plt.show()

这将为您提供如下输出图:

3d bar chart

我确信numpy可以更好地用于提取所需的数据。