Matplotlib 3D条形图:轴问题

use*_*319 3 python plot numpy matplotlib mplot3d

我遇到了在x,y,z轴上获取数据的问题.我的代码是我的代码.我为不同的轴定义范围(dx,dy.dz)的方式是否有任何问题.

result=[['122', '109', '2343', '220', '19'],
 ['15', '407', '37', '10', '102'],
 ['100', '100', '100', '100', '100'],
 ['113', '25', '19', '31', '112'],
 ['43', '219', '35', '33', '14'],
 ['132', '108', '256', '119', '14'],
 ['22', '48', '352', '51', '438']]

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.dates as dates

def format_date(x, pos=None):
     return dates.num2date(x).strftime('%m/%d/%Y')

fig=plt.figure()
ax1=fig.add_subplot(111,projection='3d')

xpos=[10/11/2013,10/12/2013,10/13/2013,10/14/2013,10/15/2013]
ypos=['A1','C1','G1','M1','M2','M3','P1']
zpos=result

dx=[5]
dy=[7]
dz=[7]
ax1.w_xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
ax1.bar3d(xpos,ypos,zpos,dx,dy,dz,color='#00ceaa')
plt.show()
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:


TypeError                                 Traceback (most recent call last)
<ipython-input-45-02cd8e7ee228> in <module>()
     18 dz=[17]
     19 ax1.w_xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
---> 20 ax1.bar3d(xpos,ypos,zpos,dx,dy,dz,color='#00ceaa')
     21 plt.show()

C:\Users\Andalib\Anaconda\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py in bar3d(self, x, y, z, dx, dy, dz, color, zsort, *args, **kwargs)
   2316             maxx = max(xi + dxi, maxx)
   2317             miny = min(yi, miny)
-> 2318             maxy = max(yi + dyi, maxy)
   2319             minz = min(zi, minz)
   2320             maxz = max(zi + dzi, maxz)

TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud)

Sau*_*tro 6

您的代码中存在以下问题:

  • xpos并且ypos通常是带有3D条底部位置的带丝网格
  • zpos给出了沿着z轴线的底座位置,除非你想让它们看起来像飞行一样,否则通常为零
  • xpos,ypos并且zpos必须具有相同的扁平形状,并且必须都是1-D数组

既然你知道每个值的位置,你可以使用np.arange()创建的位置xposypos,事后设置刻度标记.

示例代码是:

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

result=[['122', '109', '2343', '220', '19'],
 ['15', '407', '37', '10', '102'],
 ['100', '100', '100', '100', '100'],
 ['113', '25', '19', '31', '112'],
 ['43', '219', '35', '33', '14'],
 ['132', '108', '256', '119', '14'],
 ['22', '48', '352', '51', '438']]

result = np.array(result, dtype=np.int)

fig=plt.figure(figsize=(5, 5), dpi=150)
ax1=fig.add_subplot(111, projection='3d')

xlabels = np.array(['10/11/2013', '10/12/2013', '10/13/2013',
                    '10/14/2013', '10/15/2013'])
xpos = np.arange(xlabels.shape[0])
ylabels = np.array(['A1','C1','G1','M1','M2','M3','P1'])
ypos = np.arange(ylabels.shape[0])

xposM, yposM = np.meshgrid(xpos, ypos, copy=False)

zpos=result
zpos = zpos.ravel()

dx=0.5
dy=0.5
dz=zpos

ax1.w_xaxis.set_ticks(xpos + dx/2.)
ax1.w_xaxis.set_ticklabels(xlabels)

ax1.w_yaxis.set_ticks(ypos + dy/2.)
ax1.w_yaxis.set_ticklabels(ylabels)

values = np.linspace(0.2, 1., xposM.ravel().shape[0])
colors = cm.rainbow(values)
ax1.bar3d(xposM.ravel(), yposM.ravel(), dz*0, dx, dy, dz, color=colors)
plt.show()
Run Code Online (Sandbox Code Playgroud)

这使:

在此输入图像描述

你也可以使用与以下数据values成比例的数组dz:

values = (dz-dz.min())/np.float_(dz.max()-dz.min())
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述