簡體   English   中英

在 matplotlib 中使用透明度和多個 colors 顯示 3D 條形圖

[英]Display a 3D bar graph using transparency and multiple colors in matplotlib

我有一個 dataframe ,其中行代表一天中的幾個小時,列代表時間頻率。 目的是創建一個 3D 條形圖,每列代表不同的顏色。 我的dataframe如下

frec=pd.read_csv('tiempo.csv', parse_dates='Horas',index_col='Horas')
frec.index=[date.strftime('%H:%M') for date in frec.index]

frec
         Inicio  MaxExt  Fin
18:00       1       1    1
19:00       0       0    3
20:00       1       1    1
21:00       1       1    0
22:00       3       1    0
23:00       9       1    0
00:00       8       3    2
01:00       2       0    1
02:00       3       8    1
03:00       5       3    2
04:00       6       2    6
05:00       6       6    5
06:00       5       6    4
07:00       5       7    2
08:00       2       4    5
09:00       1       6    6
10:00       0       3    2
11:00       2       5    5
12:00       4       1    9
13:00       2       4    2
15:00       0       2    3
14:00       3       2    4
15:00       0       2    3
16:00       1       1    3
17:00       0       2    3 

以下代碼行嘗試創建 plot

xpos=np.arange(frec.shape[0])
ypos=np.arange(frec.shape[1])
yposM, xposM = np.meshgrid(ypos+0.5, xpos+0.5)
zpos=np.zeros(frec.shape).flatten()

dx = 0.5 * np.ones_like(zpos)
dy= 0.1 * np.ones_like(zpos)
dz=frec.values.ravel()

fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(111, projection='3d')

values = np.linspace(0.2, 1., xposM.ravel().shape[0])
colors = cm.rainbow(values)

ax.bar3d(xposM.ravel(),yposM.ravel(),zpos,dx,dy,dz,color=colors, alpha=0.5)


ticks_x = np.arange(0.5, 24, 1)
ax.set_xticks(ticks_x)
ticks_y=np.arange(0.6,3,1)
ax.set_yticks(ticks_y)

ax.w_xaxis.set_ticklabels(frec.index)
ax.w_yaxis.set_ticklabels(frec.columns)

ax.set_xlabel('Hora')
ax.set_ylabel('B')
ax.set_zlabel('Occurrence')
plt.xticks(ticks_x ['1PM','2PM','3PM','4PM','5PM','6PM','7PM','8PM','9PM','1OPM','11PM','12AM','1AM','2AM','3AM','4AM','5AM','6AM','7AM','9AM','10AM','11AM','12PM'])
fig.autofmt_xdate()
plt.show()

在此處輸入圖像描述

我如何得到一個 plot ,其中每列都用不同的顏色繪制? 即,Inicio 列的條形為藍色,MaxExt 列的條形為紅色,Fin 列的條形為黃色

通過以下方法創建colors

values = np.linspace(0.2, 1., frec.shape[0])
cmaps = [cm.Blues, cm.Reds, cm.Greens]
colors = np.hstack([c(values) for c in cmaps]).reshape(-1, 4)

這是 output:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM