【问题标题】:Matplotlib plotting custom colormap with the plotMatplotlib 用绘图绘制自定义颜色图
【发布时间】:2022-01-20 07:53:45
【问题描述】:

我一直在学习在电路上绘制 F1 数据的教程,使用 fastf1 库进行颜色编码。 我想在脚本中添加一些额外的内容以使用官方团队颜色。 它可以工作,但最终结果显示电路覆盖n bins 100 的颜色图。 在上图中,我使用了与教程'winter' 中相同的颜色图,所以我的代码肯定有问题。

但是,原始教程得到了更清晰的最终结果,只有电路显示如下:

相关教程使用来自 matplotlib 'winter' 的默认颜色图。为了让团队颜色发挥作用,我必须使用从 api 获取的 2 种颜色创建自定义颜色图。

让我们进入代码,我尝试了这么多,到处搜索都没有成功......

自定义颜色图是使用我从 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()

这是我认为出了问题的地方,但我不确定出了什么问题。我想这与我如何使用颜色图有关。但是我改变的一切都破坏了整个剧本。 由于我对 matplotlib 没有太多经验,所以它变得非常复杂。

因为我不希望这个问题过长,所以可以在这里阅读整个代码: https://gist.github.com/platinaCoder/7b5be22405f2003bd577189692a2b36b

【问题讨论】:

  • 你查看过official reference中关于彩色地图的教程吗?您可以在此处使用mpl.colors.ListedColormap() 指定任何颜色。
  • 我有,问题是我从一个 API 中获得了 2 种颜色,它们是相关 F1 车队的官方颜色 HEX 代码。一切都很好,因为颜色被绘制在电路上,除了颜色映射中包含的导出。
  • 十六进制代码可以转换为 rgba 格式并用于 ListedColormap()。看到这个:https://matplotlib.org/stable/api/_as_gen/matplotlib.colors.to_rgba.html#matplotlib.colors.to_rgba
  • 颜色不是问题,我已经在这里将 HEX 转换为 RGB 元组:teamcolor1 = to_rgb('{}'.format(team1_color))。问题是最终结果。我得到一个可以看到整个颜色图的 png,我只想要带有驱动程序所在侧栏的电路。我不想将这 3 个 cmap 放入我的最终图片中。
  • 在代码的第一部分,您创建了 4 个颜色条(每个子图一个)。在代码的第二部分中,您从行集合中的数据创建第 5 个颜色条。由于您没有指定ax,因此它被放置在最后创建的子图旁边(右下角)。您可以省略创建不需要的彩条。顺便说一句,mpl.colors.ListedColormap(['#0000FF', '#00FF80']) 使用十六进制颜色没有问题,不需要转换。

标签: python matplotlib


【解决方案1】:

我没有创建一个完整的客户 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)])

【讨论】:

    猜你喜欢
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多