如何在 pandas 中订购用于绘制3D条形图的数据框 [英] How to order dataframe for plotting 3d bar in pandas

查看:46
本文介绍了如何在 pandas 中订购用于绘制3D条形图的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个图表,其中包含来自熊猫的3d中的多个条形图.回顾网络上的一些示例,我发现完成此操作的最佳方法是获取像这样的数据框:

I am trying to create a chart with multiple bars in 3d from pandas. Reviewing some examples on the web, I see that the best way to accomplish this is to get a dataframe like this:

data

Variable         A         B          C          D
date
2000-01-03  0.469112  -1.135632   0.119209   -2.104569
2000-01-04 -0.282863   1.212112  -1.044236   -0.494929
2000-01-05 -1.509059  -0.173215  -0.861849    1.071804

我的数据框是:

df

       Date_inicio             Date_Fin             Date_Max       Clase
0   2004-04-09 23:00:00  2004-04-10 04:00:00  2004-04-10 02:00:00  MBCCM
1   2004-04-12 23:00:00  2004-04-13 04:00:00  2004-04-13 00:00:00  MBSCL
2   2004-04-24 04:00:00  2004-04-24 12:00:00  2004-04-24 09:00:00    SCL
3   2004-05-02 07:00:00  2004-05-02 14:00:00  2004-05-02 11:00:00    SCL
4   2004-05-30 05:00:00  2004-05-30 08:00:00  2004-05-30 07:00:00  MBCCM
5   2004-05-31 03:00:00  2004-05-31 07:00:00  2004-05-31 05:00:00  MBCCM   
6   2004-06-08 00:00:00  2004-06-08 05:00:00  2004-06-08 03:00:00  MBSCL
7   2004-06-12 22:00:00  2004-06-13 12:00:00  2004-06-13 06:00:00    CCM
8   2004-06-13 03:00:00  2004-06-13 08:00:00  2004-06-13 06:00:00  MBCCM
9   2004-06-14 00:00:00  2004-06-14 03:00:00  2004-06-14 02:00:00  MBSCL
10  2004-06-14 03:00:00  2004-06-14 09:00:00  2004-06-14 07:00:00  MBSCL
11  2004-06-17 08:00:00  2004-06-17 14:00:00  2004-06-17 11:00:00  MBCCM
12  2004-06-17 12:00:00  2004-06-17 17:00:00  2004-06-17 14:00:00  MBCCM
13  2004-06-22 00:00:00  2004-06-22 08:00:00  2004-06-22 06:00:00    SCL
14  2004-06-22 08:00:00  2004-06-22 14:00:00  2004-06-22 11:00:00  MBCCM 
15  2004-06-22 23:00:00  2004-06-23 09:00:00  2004-06-23 06:00:00    CCM
16  2004-07-01 05:00:00  2004-07-01 09:00:00  2004-07-01 06:00:00  MBCCM
17  2004-07-02 00:00:00  2004-07-02 04:00:00  2004-07-02 02:00:00  MBSCL
18  2004-07-04 12:00:00  2004-07-04 15:00:00  2004-07-04 13:00:00  MBCCM
19  2004-07-06 04:00:00  2004-07-06 13:00:00  2004-07-06 07:00:00    SCL
20  2004-07-07 04:00:00  2004-07-07 12:00:00  2004-07-07 10:00:00    CCM
21  2004-07-08 03:00:00  2004-07-08 06:00:00  2004-07-08 05:00:00  MBCCM
22  2004-07-08 12:00:00  2004-07-08 17:00:00  2004-07-08 13:00:00  MBCCM
23  2004-07-08 02:00:00  2004-07-08 06:00:00  2004-07-08 04:00:00  MBCCM
24  2004-07-09 05:00:00  2004-07-09 12:00:00  2004-07-09 08:00:00    CCM
25  2004-07-11 18:00:00  2004-07-12 12:00:00  2004-07-11 21:00:00  MBSCL
26  2004-07-11 23:00:00  2004-07-12 05:00:00  2004-07-12 02:00:00  MBSCL
27  2004-07-15 11:00:00  2004-07-15 19:00:00  2004-07-15 12:00:00    CCM
28  2004-07-16 12:00:00  2004-07-16 16:00:00  2004-07-16 14:00:00  MBCCM
29  2004-07-17 02:00:00  2004-07-17 06:00:00  2004-07-17 05:00:00  MBCCM

现在,我想获取一个小时内所有课程的发生情况.例如,某时间在Date_inicio,Date_fin和Date_max中出现了不同的类多少次.我从df获取下一个汇率表

Now I want to get the occurrence of all classes for hour. For example, how many times different classes occur at some time in Date_inicio, Date_fin and Date_max. From df i obtain the next frecuencies table,

frec

       Frec_Inicio  Frec_Max  Frec_Fin
Horas                           
1           2             0    1
2           3             8    1
3           5             3    2
4           6             2    6
5           6             6    5
6           5             6    4
7           5             7    2
8           2             4    5
9           1             6    6 
10          0             3    2
11          2             5    5
12          4             1    9
13          2             4    2
14          3             2    4
15          0             2    3
16          1             1    3
17          0             2    3 
18          1             1    1
19          0             0    3
20          1             1    1
21          1             1    0
22          3             1    0
23          9             1    0
24          8             3    2

现在,我的目标是绘制一个如下图所示的3D条形图

Now, my goal is to plot a 3D bar like the figure below

为此,我编写了以下代码

To achieve this, i write the following code

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
xpos=np.arange(frec.shape[0])
ypos=np.arange(frec.shape[1])
xpos, ypos = np.meshgrid(xpos+0.25, ypos+0.25)
xpos = xpos.flatten()
ypos = ypos.flatten()
zpos=np.zeros(frec.shape).flatten()

dx=0.5 * np.ones_like(zpos)
dy=0.5 * np.ones_like(zpos)
dz=frec.values.ravel()
dz[np.isnan(dz)]=0.

ax.bar3d(xpos,ypos,zpos,dx,dy,dz,color='b', alpha=0.5)
ax.set_xticks([.5,1.5,2.5])
ax.set_yticks([.5,1.5,2.5,3.5])
ax.w_yaxis.set_ticklabels(frec.columns)
ax.w_xaxis.set_ticklabels(frec.index)
ax.set_xlabel('Time')
ax.set_ylabel('B')
ax.set_zlabel('Occurrence')
plt.show() 

如何获得与上图相似的更好的图?

How I get a better plot, similar to the previous figure?

推荐答案

这是要计数的代码:

import pandas as pd
text="""Date_inicio,             Date_Fin,             Date_Max,       Clase
2004-04-09 23:00:00,  2004-04-10 04:00:00,  2004-04-10 02:00:00,  MBCCM
2004-04-12 23:00:00,  2004-04-13 04:00:00,  2004-04-13 00:00:00,  MBSCL
2004-04-24 04:00:00,  2004-04-24 12:00:00,  2004-04-24 09:00:00,    SCL
2004-05-02 07:00:00,  2004-05-02 14:00:00,  2004-05-02 11:00:00,    SCL
2004-05-30 05:00:00,  2004-05-30 08:00:00,  2004-05-30 07:00:00,  MBCCM
2004-05-31 03:00:00,  2004-05-31 07:00:00,  2004-05-31 05:00:00,  MBCCM   
2004-06-08 00:00:00,  2004-06-08 05:00:00,  2004-06-08 03:00:00,  MBSCL
2004-06-12 22:00:00,  2004-06-13 12:00:00,  2004-06-13 06:00:00,    CCM
2004-06-13 03:00:00,  2004-06-13 08:00:00,  2004-06-13 06:00:00,  MBCCM
2004-06-14 00:00:00,  2004-06-14 03:00:00,  2004-06-14 02:00:00,  MBSCL
2004-06-14 03:00:00,  2004-06-14 09:00:00,  2004-06-14 07:00:00,  MBSCL
2004-06-17 08:00:00,  2004-06-17 14:00:00,  2004-06-17 11:00:00,  MBCCM
2004-06-17 12:00:00,  2004-06-17 17:00:00,  2004-06-17 14:00:00,  MBCCM
2004-06-22 00:00:00,  2004-06-22 08:00:00,  2004-06-22 06:00:00,    SCL
2004-06-22 08:00:00,  2004-06-22 14:00:00,  2004-06-22 11:00:00,  MBCCM 
2004-06-22 23:00:00,  2004-06-23 09:00:00,  2004-06-23 06:00:00,    CCM
2004-07-01 05:00:00,  2004-07-01 09:00:00,  2004-07-01 06:00:00,  MBCCM
2004-07-02 00:00:00,  2004-07-02 04:00:00,  2004-07-02 02:00:00,  MBSCL
2004-07-04 12:00:00,  2004-07-04 15:00:00,  2004-07-04 13:00:00,  MBCCM
2004-07-06 04:00:00,  2004-07-06 13:00:00,  2004-07-06 07:00:00,    SCL
2004-07-07 04:00:00,  2004-07-07 12:00:00,  2004-07-07 10:00:00,    CCM
2004-07-08 03:00:00,  2004-07-08 06:00:00,  2004-07-08 05:00:00,  MBCCM
2004-07-08 12:00:00,  2004-07-08 17:00:00,  2004-07-08 13:00:00,  MBCCM
2004-07-08 02:00:00,  2004-07-08 06:00:00,  2004-07-08 04:00:00,  MBCCM
2004-07-09 05:00:00,  2004-07-09 12:00:00,  2004-07-09 08:00:00,    CCM
2004-07-11 18:00:00,  2004-07-12 12:00:00,  2004-07-11 21:00:00,  MBSCL
2004-07-11 23:00:00,  2004-07-12 05:00:00,  2004-07-12 02:00:00,  MBSCL
2004-07-15 11:00:00,  2004-07-15 19:00:00,  2004-07-15 12:00:00,    CCM
2004-07-16 12:00:00,  2004-07-16 16:00:00,  2004-07-16 14:00:00,  MBCCM
2004-07-17 02:00:00,  2004-07-17 06:00:00,  2004-07-17 05:00:00,  MBCCM"""
import io
df = pd.read_csv(io.BytesIO(text), skipinitialspace=True)
df.drop(["Clase"], axis=1, inplace=True)
df = df.apply(lambda s:s.str[11:13]).convert_objects(convert_numeric=True)
df2 = df.apply(lambda s:s.value_counts())
print df2

以下是绘制3d条形的代码:

Here is the code that draw 3d bars:

import pandas as pd
text="""Horas Frec_Inicio  Frec_Max  Frec_Fin
1           2             0    1
2           3             8    1
3           5             3    2
4           6             2    6
5           6             6    5
6           5             6    4
7           5             7    2
8           2             4    5
9           1             6    6 
10          0             3    2
11          2             5    5
12          4             1    9
13          2             4    2
14          3             2    4
15          0             2    3
16          1             1    3
17          0             2    3 
18          1             1    1
19          0             0    3
20          1             1    1
21          1             1    0
22          3             1    0
23          9             1    0
24          8             3    2"""
import io
df = pd.read_csv(io.BytesIO(text), skipinitialspace=True, delim_whitespace=True)
df.set_index("Horas", inplace=True)
columns_name = [x.replace("_", " ") for x in df.columns]
df.columns = [0, 2, 4]
x, y, z = df.stack().reset_index().values.T

import visvis as vv
app = vv.use()

f = vv.clf()
a = vv.cla()

bar =vv.bar3(x, y, z, width=0.8)
bar.colors = ["r","g","b"] * 24
a.axis.yTicks = dict(zip(df.columns, columns_name))
app.Run()

输出:

这篇关于如何在 pandas 中订购用于绘制3D条形图的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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