Issue
I'm getting strange results when attempting a bar plot:
labels = [f'{i+1}/{3}' for i in range(3)]
main_team_values = ['0:05:01', '0:06:54', '0:05:41']
other_team_values = ['0:07:56', '0:07:06', '0:07:04']
X = np.arange(3)
fig, ax = plt.subplots()
ax.bar(X, main_team_values, color = 'k', width = 0.35)
ax.bar(X + width, other_team_values, color = 'r', width = 0.35)
plt.show()
As you can see main_team_values[0]
is omitted from the plot? Does anyone know why?
Also: is there a way for me to label the x-axis with 1/3, 2/3, 3/3 (what I've produced in labels)?
Solution
the list you are plotting is string, either convert to timestamps or duration in second and plot
main_team_values = [sum(map(lambda x: int(x[0]) * x[1], zip(d.split(':'), [3600, 60, 1]))) for d in main_team_values]
other_team_values = [sum(map(lambda x: int(x[0]) * x[1], zip(d.split(':'), [3600, 60, 1]))) for d in other_team_values]
ax.bar(X, main_team_values, color = 'k', width = 0.35)
ax.bar(X + width, other_team_values, color = 'r', width = 0.35)
plt.ylabel('Time')
fmt = lambda x, y: f"{int(x // 3600):02d}:{int(x%3600) // 60:02d}:{int(x)%60:02d}"
ax.yaxis.set_major_formatter(fmt)
plt.show()
Answered By - amirhm
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.