使用pylab从列表绘制直方图

2024-09-27 07:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力通过pylab模块绘制一个包含两个列表的柱状图(我需要使用它)

第一个列表,totalTime,由程序内计算的7个浮点值填充。在

第二个列表,raceTrack,由7个字符串值填充,这些值表示比赛跑道的名称。在

totalTime[0]是在赛道[0]上花费的时间,totalTime[3]是在赛道[3]上花费的时间,等等。。。在

我整理了数组并将值四舍五入到小数点后2位

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

myFormattedTotalTimes'输出(当输入的值为100时)为

^{pr2}$

我需要使用列表中的值来创建一个直方图,其中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()

任何帮助都将不胜感激,我对这一点很迷茫。在


Tags: 模块toin名称列表for时间绘制
1条回答
网友
1楼 · 发布于 2024-09-27 07:27:06

正如@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

相关问题 更多 >

    热门问题