[英]Matplotlib plotting custom colormap with the plot
我一直在關注在電路上繪制 F1 數據的教程,使用fastf1
庫進行顏色編碼。 我想在腳本中添加一些額外的內容以利用官方團隊 colors。 它可以工作,但最終結果顯示了具有覆蓋n bins 100
的電路的顏色圖。 在上圖中,我使用了與教程'winter'
中相同的顏色圖,所以我的代碼肯定有問題。
有問題的教程使用來自 matplotlib 'winter'
的默認顏色圖。 為了讓 colors 團隊工作,我必須從從 api 獲取的 2 個 colors 創建自定義顏色圖。
讓我們進入代碼,我已經嘗試了很多並且到處搜索都沒有成功......
自定義顏色圖是使用我從 matplotlib 文檔中獲得的代碼序列構建的。
# Create custom colormap
teamcolor1 = to_rgb('{}'.format(team1_color))
teamcolor2 = to_rgb('{}'.format(team2_color))
colors = [teamcolor1, teamcolor2]
n_bins = [3, 6, 10, 100]
cmap_name = 'colors'
fig, axs = plt.subplots(2, 2, figsize=(6, 9))
fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
x = np.arange(0, np.pi, 0.1)
y = np.arange(0, 2 * np.pi, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.cos(X) * np.sin(Y) * 10
for n_bin, ax in zip(n_bins, axs.ravel()):
colormap = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bin)
im = ax.imshow(Z, interpolation='nearest', origin='lower', cmap=colormap)
ax.set_title("N bins: %s" % n_bin)
fig.colorbar(im, ax=ax)
cm.register_cmap(cmap_name, colormap)
我注冊了顏色圖,以便稍后在腳本中使用get_cmap
輕松調用它。
電路的最終繪圖是在這段代碼中完成的:
x = np.array(telemetry['X'].values)
y = np.array(telemetry['Y'].values)
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
fastest_driver_array = telemetry['Fastest_driver_int'].to_numpy().astype(float)
cmap = cm.get_cmap('winter', 2)
lc_comp = LineCollection(segments, norm=plt.Normalize(1, cmap.N+1), cmap=cmap)
lc_comp.set_array(fastest_driver_array)
lc_comp.set_linewidth(5)
plt.rcParams['figure.figsize'] = [18, 10]
plt.gca().add_collection(lc_comp)
plt.axis('equal')
plt.tick_params(labelleft=False, left=False, labelbottom=False, bottom=False)
cbar = plt.colorbar(mappable=lc_comp, boundaries=np.arange(1, 4))
cbar.set_ticks(np.arange(1.5, 9.5))
cbar.set_ticklabels(['{}'.format(driver1), '{}'.format(driver2)])
plt.savefig(
'{}_'.format(year) + '{}_'.format(driver1) + '{}_'.format(driver2) + '{}_'.format(circuit) + '{}.png'.format(
session), dpi=300)
plt.show()
這是我認為 go 錯誤的地方,但我不確定出了什么問題。 我想這與我如何使用顏色圖有關。 但是我改變的一切都破壞了整個劇本。 由於我對 matplotlib 沒有太多經驗,因此變得非常復雜。
因為我不希望這個問題太長,所以可以在這里閱讀整個代碼: https://gist.github.com/platinaCoder/7b5be22405f2003bd577189692a2b6
我沒有創建一個完整的客戶 cmap,而是擺脫了這段代碼:
# Create custom colormap
teamcolor1 = to_rgb('{}'.format(team1_color))
teamcolor2 = to_rgb('{}'.format(team2_color))
colors = [teamcolor1, teamcolor2]
n_bins = [3, 6, 10, 100]
cmap_name = 'colors'
fig, axs = plt.subplots(2, 2, figsize=(6, 9))
fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
x = np.arange(0, np.pi, 0.1)
y = np.arange(0, 2 * np.pi, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.cos(X) * np.sin(Y) * 10
for n_bin, ax in zip(n_bins, axs.ravel()):
colormap = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bin)
im = ax.imshow(Z, interpolation='nearest', origin='lower', cmap=colormap)
ax.set_title("N bins: %s" % n_bin)
fig.colorbar(im, ax=ax)
cm.register_cmap(cmap_name, colormap)
並將cmap = cm.get_cmap('colors', 2)
替換為cmap = cm.colors.ListedColormap(['{}'.format(team1_color), '{}'.format(team2_color)])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.