2015-12-23 158 views
0

我奮力第w通過pylab模塊繪製與兩個列表的直方圖(其我需要使用)繪製使用pylab

的第一列表,TOTALTIME,被填充有7個浮點值列表的直方圖在節目內計算。

第二個列表raceTrack填充了7個表示賽道名稱的字符串值。

TOTALTIME [0]是在跑道[0]所用的時間,TOTALTIME [3]是[3],等等...

我整理出陣列並四捨五入的值,以在跑道所花費的時間2小數位

totalTimes.sort() 
myFormattedTotalTimes = ['%.2f' % elem for elem in totalTimes] 

myFormattedTotalTimes'輸出(當輸入值是100)是

['68.17', '71.43', '71.53', '84.23', '84.55', '87.20', '102.85'] 

我需要在裏使用的值st創建一個直方圖,其中x軸將顯示賽道的名稱,並且y軸將顯示該特定軌道上的時間。 Ive made quickly an excel histogram to help understand.

I have attempted but to no avail

for i in range (7): 
    pylab.hist([myFormattedTotalTimes[i]],7,[0,120]) 
pylab.show() 

任何幫助將是非常讚賞,我完全迷失在這一個。

+0

看來,這是一個條形圖,但不是一個直方圖? –

回答

0

正如@John Doe所述,我想你想要一個條形圖。從matplotlib example,下面的你想要做什麼,

import matplotlib.pyplot as plt 
import numpy as np 

myFormattedTotalTimes = ['68.17', '71.43', '71.53', '84.23', '84.55', '87.20', '102.85'] 

#Setup track names 
raceTrack = ["track " + str(i+1) for i in range(7)] 

#Convert to float 
racetime = [float(i) for i in myFormattedTotalTimes] 

#Plot a bar chart (not a histogram) 
width = 0.35  # the width of the bars 
ind = np.arange(7)  #Bar indices 

fig, ax = plt.subplots(1,1) 
ax.bar(ind,racetime, width) 
ax.set_xticks(ind + width) 
ax.set_xticklabels(raceTrack) 
plt.show() 

它看起來像, enter image description here