将 Pandas 交叉表数据框绘制成 3D 条形图 [英] Plotting Pandas Crosstab Dataframe into 3D bar chart

查看:69
本文介绍了将 Pandas 交叉表数据框绘制成 3D 条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  EG语言 C C++ Java Python Perl国家美国3222 343 2112 10110 89法国5432 323 1019 678 789日本7878 467 767 8788 40

我尝试使用gca函数和3d投影.我也试过

I have a dataframe that has rownames of country, colnames of programming language and values showing the count respectively generated my pandas crosstab operation.

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

I have tried using gca function and projection 3d. I also tried this but couldnt get it to work.

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()

I want to display a 3D bar chart where two axis could be country name and language and the other their count.

解决方案

Try this:

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()

Output:

这篇关于将 Pandas 交叉表数据框绘制成 3D 条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆