繁体   English   中英

Matplotlib 使用 plot 绘制自定义颜色图

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM