我有以秒为单位的救护车响应时间数据。例如 (32, 158, 36, 459, 830, 396)。我可以制作一个 Matplotlib 直方图,将数据分成 60 秒的增量。在 x 轴标签上,数据标记为 0 - 60 - 120 - 180 等。我希望 x 轴标签显示 00:00 - 01:00 - 02:00 - 03:00 等。换句话说,它应该显示格式为 mm:ss 的秒数,而不是整数值的秒数。下面的代码很接近,但我无法使其工作。下面的代码仅为所有箱返回 00:00。如何将直方图的 x 轴标记为 mm:ss 而不是整数?
# import the libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# generate a dataframe of values from 0 to 1800 (zero seconds to 1800 seconds which is 30 minutes)
df = pd.DataFrame(np.random.randint(0,1800, size=(1000, 1)), columns=list('A'))
start_value = 0 # the start value is zero seconds
end_value = 1800 # the end value is 1800 seconds (30 minutes)
increment_by_value = 60 # increment by one minute
# set up the nuts and bolts of the histogram
fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(8, 6.5)
ax = fig.add_subplot(111)
# generate a histogram
plt.hist(df.A, bins=np.arange(start_value, end_value, increment_by_value), color='Purple', label='Response Time', edgecolor = 'Gray', linewidth = 1.0)
# not sure how this works. I would hope that it formats 75 seconds to 01:15
myFmt = mdates.DateFormatter('%M:%S')
ax.xaxis.set_major_formatter(myFmt)
# the x ticks line up with the bars. In other words, every 60 seconds
plt.xticks(np.arange(start_value, end_value, increment_by_value), rotation='vertical')
# set up the title and the labels
plt.title("Cardiac Arrest Response Time" + '\n' + '(Interval in Seconds)')
plt.xlabel("Bins of 1 minute (in seconds)")
plt.ylabel("Calls")
plt.legend()
plt.show()