我正在做一個個人專案,我想在排位賽期間繪制每個一級方程式車手的最佳時間(分為 3 個部分:Q1、Q2 和 Q3)
該圖將在 y 軸上顯示司機的姓名(按他們的最終結果排序:第一個在頂部,最后一個在底部)和他們的時間在 x 軸上,如下所示,用 Excel 表示 4 個司機:

使用 Python,我可以收集他們在每個會話(Q1、Q2 和 Q3)中的最佳時間,并以串列(t_Q1、t_Q2、t_Q3)的形式存盤在串列中,以及他們的最終位置。這是代碼的開頭:
import matplotlib.pyplot as plt
import numpy as np
Drivers_name=['VER','HAM','VET','MSC'] #for the label of the Y-axis
Drivers_position=[1,2,12,20]
Drivers_timings=[[94.352,93.464,92.91],[94.579,93.797,93.119],[95.281,95.5],[96.119]]
fig,ax = plt.subplots()
y=np.arange(len(Drivers_name))
for i in range(len(Drivers_name)):
if len(Drivers_timings[i])==3:
eps=0.2
ax.barh(y-eps,Drivers_timings[i][0],height=eps,color='g', label='Q1')
ax.barh(y,Drivers_timings[i][1],height=eps,color='r', label='Q2')
ax.barh(y eps,Drivers_timings[i][1],height=eps,color='b', label='Q3')
elif len(Drivers_timings[i])==2:
eps=0.2
ax.barh(y-eps/2,Drivers_timings[i][0],height=eps,color='g', label='Q1')
ax.barh(y eps/2,Drivers_timings[i][1],height=eps,color='r', label='Q2')
elif len(Drivers_timings[i])==1:
ax.barh(y,Drivers_timings[i][0],height=0.2,color='g', label='Q1')
plt.yticks(y,Drivers_name)
plt.legend()
plt.show()
但這就是我執行它時得到的:

我們可以注意到它并沒有真正代表我正在尋找的東西,并且圖例多次顯示相同的元素(這是正常的,因為label引數在for回圈中,但我不能有任何其他想法)
評論: 總而言之,這是我發帖的目的
- 獲得預期的圖(我用 Excel 得到的圖)
- 修復圖例框
uj5u.com熱心網友回復:
通過使用ax.barh(y-eps, ...)where y=np.arange(len(Drivers_name)),在每個步驟中為所有 y 值創建回圈條。例如,i=0將在周圍創建三個條形y=0,但也會創建3 個周圍y=1、3 個更多的周圍y=2和 3 個周圍y=3。替換y為i創建所需的效果。
圖例包含每次使用時的一行label=。在完整的情節中,ax.barh將被呼叫 9 次,每次都帶有label=. 一種解決方案是從虛擬矩形創建圖例。
import matplotlib.pyplot as plt
import numpy as np
Drivers_name = ['VER', 'HAM', 'VET', 'MSC'] # for the label of the Y-axis
Drivers_position = [1, 2, 12, 20]
Drivers_timings = [[94.352, 93.464, 92.91], [94.579, 93.797, 93.119], [95.281, 95.5], [96.119]]
colors = ['g', 'r', 'b']
labels = ['Q1', 'Q2', 'Q3']
fig, ax = plt.subplots()
for i in range(len(Drivers_name)):
eps = 0.2
if len(Drivers_timings[i]) == 3:
ax.barh(i - eps, Drivers_timings[i][0], height=eps, color=colors[0])
ax.barh(i, Drivers_timings[i][1], height=eps, color=colors[1])
ax.barh(i eps, Drivers_timings[i][1], height=eps, color=colors[2])
elif len(Drivers_timings[i]) == 2:
ax.barh(i - eps / 2, Drivers_timings[i][0], height=eps, color=colors[0])
ax.barh(i eps / 2, Drivers_timings[i][1], height=eps, color=colors[1])
elif len(Drivers_timings[i]) == 1:
ax.barh(i, Drivers_timings[i][0], height=eps, color=colors[0])
plt.yticks(np.arange(len(Drivers_name)), Drivers_name)
handles = [plt.Rectangle((0, 0), 0, 0, color=color) for color in colors]
plt.legend(handles=handles, labels=labels, bbox_to_anchor=(1.01, 0.99), loc='upper left')
plt.tight_layout()
plt.show()

可以進一步縮短代碼,同時通過在自己的回圈中繪制單個條形圖使代碼更加通用:
colors = ['g', 'r', 'b']
labels = ['Q1', 'Q2', 'Q3']
fig, ax = plt.subplots()
eps = 0.2
for i, driver_timings in enumerate(Drivers_timings):
num_bars = len(driver_timings)
first_bar = i - eps * (num_bars - 1) / 2
for j, (driver_timings_j, color) in enumerate(zip(driver_timings, colors)):
ax.barh(first_bar j * eps, driver_timings_j, height=eps, color=color)
plt.yticks(np.arange(len(Drivers_name)), Drivers_name)
handles = [plt.Rectangle((0, 0), 0, 0, color=color) for color in colors]
plt.legend(handles=handles, labels=labels, bbox_to_anchor=(1.01, 0.99), loc='upper left')
plt.tight_layout()
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/357836.html
標籤:Python matplotlib
上一篇:如何將特定范圍之外的點設定為0
下一篇:圖未顯示在Jupyter實驗室中
