1

当我使用 python 2.5 时,我遇到了 PolyCollection matplotlib 的问题。在随机模式下,它向我显示以下错误:数组维度必须一致,但 d_0 除外(file:collection.py - xy = np.concatenate([xy, np.zeros((1,2))]))。这是我的代码:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
from matplotlib.colors import colorConverter
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager as fm
from matplotlib.patches import Rectangle
import matplotlib.cm as cm

colors = ['#be1e2d',
        '#666699',
        '#92d5ea',
        '#ee8310',
        '#8d10ee',
        '#5a3b16',
        '#26a4ed',
        '#f45a90',
        '#e9e744']

row_names = ['2005','2006','2007']
data = [[1,1,1,1,1,1],[2,2,2,2,2,2],[4,4,4,4,4,4],[5,5,5,5,5,5],[7,7,7,7,7,7],[8,8,8,8,8,8]]
column_names = ['Ri','Pe']

#0 to start and end list
i=0
for i in range(len(data)):
    data[i].append(0)

for i in range(len(data)):
    data[i].insert(0,0)

dpi = 50.0
width = 460
height = 440
fig = plt.figure(1, figsize=(width/dpi,height/dpi),facecolor='w')
ax = fig.gca(projection='3d')#,azim=40, elev=0)
#Build axes
size = len(row_names) * len(data[0])
zs = np.arange(len(data))
# Setto le properties dei font
fp = fm.FontProperties()
fp.set_size('xx-small')
#Build Graph
verts = []
step = 1.0/len(data[0])
vertsColor = []
#Verify Single series or not
if len(column_names) > 1:
    idx = 0
    xs = np.arange(0, size, step)
    change_color = len(column_names) - 1
    for z in zs:
        verts.append(zip(xs, data[z]))
        vertsColor.append(colors[idx])
        if idx == change_color:
            idx = 0
        else:
            idx = idx + 1
    ################################################
    # I THINK THE PROBLEM IS HERE
    poly = PolyCollection(verts,facecolors=vertsColor)
    ax.add_collection3d(poly, zs=zs, zdir='y')
    ################################################
    ax.set_ylim3d(0, len(row_names)*len(column_names))
    zs = np.arange(0,len(row_names) * len(column_names), len(column_names))
    ax.set_yticks(zs)
    lim = ((size*step)-step) - (len(row_names) - 1) 
    ax.set_xlim3d(0, lim)
    rect = []
    serie = []
    #Build legend
    for i in range(len(column_names)):
        rect.insert(i,Rectangle((0,0), 1,1, facecolor=colors[i]))
        serie.insert(i,column_names[i])
    ax.legend((rect), (serie),   loc=3, ncol=3,  prop=fp)
else:
    xs = np.arange(0, size, step)
    for z in zs:
        verts.append(zip(xs, data[z]))
    poly = PolyCollection(verts,facecolors=colors)  #[:len(data)])
    poly.set_alpha(0.6)
    ax.add_collection3d(poly, zs=zs, zdir='y')
    ax.set_xlabel('Rec')
    lim = ((size*step)-step) - (len(row_names) - 1) 
    ax.set_xlim3d(0, lim)
    ax.set_yticks(zs)
    ax.set_ylim3d(0, len(row_names))

#Find Max Value
max_value = 0
i=0
for i in data:
    mass = max(i)
    if mass > max_value:
        max_value = mass

#Font Label X,Y,Z
for label in ax.get_xticklabels():
    label.set_fontproperties(fp)
for label in ax.get_yticklabels():
    label.set_fontproperties(fp)
for label in ax.get_zticklabels():
    label.set_fontproperties(fp)

ax.set_xticklabels('')
ax.set_ylabel('Years')
ax.set_yticklabels(row_names, fontproperties = fp)
ax.set_zlabel('Values')
ax.set_zlim3d(0, max_value)
ax.set_title('Test',x=0.5, y=1)
plt.show()

谢谢。

4

0 回答 0