Pandas 将交叉表数据框绘制为三维条形图

Pandas 将交叉表数据框绘制为三维条形图,pandas,matplotlib,Pandas,Matplotlib,我有一个数据框,其中包含国家的行名、编程语言的列名和分别显示交叉表操作生成的计数的值 EG Language C C++ Java Python Perl Country USA 3222 343 2112 10110 89 France 5432 323 1019 678 789 Japan 7878 467 767 8788

我有一个数据框,其中包含国家的行名、编程语言的列名和分别显示交叉表操作生成的计数的值

EG 

Language    C     C++     Java    Python    Perl

Country

USA          3222   343     2112   10110      89

France      5432   323     1019     678        789

Japan       7878   467       767     8788       40
我试过使用gca函数和3d投影。我也试过了,但没能成功

threedee = plt.figure().gca(projection='3d') <br/>
threedee.bar(repo_lang, repo_loc, repo_values) <br/>
threedee.set_xlabel('Index')<br/>
threedee.set_ylabel('H-L')<br/>
threedee.set_zlabel('Close')<br/>
plt.show()
threedee=plt.figure().gca(projection='3d')
三个基准条(回购价格、回购价格、回购价格)
threedee.set_xlabel('Index')
三个dee.set_ylabel('H-L')
threedee.set_zlabel('Close')
plt.show()
我想显示一个三维条形图,其中两个轴可以是国家名称和语言,另一个轴可以是它们的计数。

试试这个:

from mpl_toolkits.mplot3d import Axes3D

# thickness of the bars
dx, dy = .8, .8

# prepare 3d axes
fig = plt.figure(figsize=(10,6))
ax = Axes3D(fig)

# set up positions for the bars 
xpos=np.arange(eg.shape[0])
ypos=np.arange(eg.shape[1])

# set the ticks in the middle of the bars
ax.set_xticks(xpos + dx/2)
ax.set_yticks(ypos + dy/2)

# create meshgrid 
# print xpos before and after this block if not clear
xpos, ypos = np.meshgrid(xpos, ypos)
xpos = xpos.flatten()
ypos = ypos.flatten()

# the bars starts from 0 attitude
zpos=np.zeros(eg.shape).flatten()

# the bars' heights
dz = eg.values.ravel()

# plot 
ax.bar3d(xpos,ypos,zpos,dx,dy,dz)

# put the column / index labels
ax.w_yaxis.set_ticklabels(eg.columns)
ax.w_xaxis.set_ticklabels(eg.index)

# name the axes
ax.set_xlabel('Country')
ax.set_ylabel('Language')
ax.set_zlabel('Count')

plt.show()
输出:

试试这个:

from mpl_toolkits.mplot3d import Axes3D

# thickness of the bars
dx, dy = .8, .8

# prepare 3d axes
fig = plt.figure(figsize=(10,6))
ax = Axes3D(fig)

# set up positions for the bars 
xpos=np.arange(eg.shape[0])
ypos=np.arange(eg.shape[1])

# set the ticks in the middle of the bars
ax.set_xticks(xpos + dx/2)
ax.set_yticks(ypos + dy/2)

# create meshgrid 
# print xpos before and after this block if not clear
xpos, ypos = np.meshgrid(xpos, ypos)
xpos = xpos.flatten()
ypos = ypos.flatten()

# the bars starts from 0 attitude
zpos=np.zeros(eg.shape).flatten()

# the bars' heights
dz = eg.values.ravel()

# plot 
ax.bar3d(xpos,ypos,zpos,dx,dy,dz)

# put the column / index labels
ax.w_yaxis.set_ticklabels(eg.columns)
ax.w_xaxis.set_ticklabels(eg.index)

# name the axes
ax.set_xlabel('Country')
ax.set_ylabel('Language')
ax.set_zlabel('Count')

plt.show()
输出:


这对我很有用,你能解释一下你的代码吗?这对我很有用,你能解释一下你的代码吗。