我正在尝试用pandas在3d中创建一个包含多个条形图的图表。回顾网上的一些例子,我发现实现这一目标的最佳方法是获得这样的数据框:
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我获得下一个频率表,
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条形图
为实现此目的,我编写以下代码
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()
我如何获得更好的情节,类似于上图?
答案 0 :(得分:2)
以下是要计算的代码:
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条形码的代码:
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()
输出: