【问题标题】:Plotting a histogram from lists using pylab使用 pylab 从列表中绘制直方图
【发布时间】:2015-12-23 04:24:43
【问题描述】:

我正在努力通过 pylab 模块(我需要使用它)绘制带有两个列表的直方图

第一个列表,totalTime,填充了程序中计算的 7 个浮点值。

第二个列表,raceTrack,填充了 7 个字符串值,代表赛道的名称。

totalTime[0] 是在raceTrack[0] 上花费的时间,totalTime[3] 是在raceTrack[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']

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

任何帮助将不胜感激,我对此感到很迷茫。

【问题讨论】:

  • 好像是条形图而不是直方图?

标签: python matplotlib histogram


【解决方案1】:

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

看起来像,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 2014-03-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多