如何更改subp中不同线条的线条颜色

2024-06-13 09:52:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望能够将线条的颜色更改为使用matplotlib在绘图上指定的颜色。在

下面是我正在使用的相关代码(它是函数的一部分)。变量avg_rel_track是一个“2D数组”,每列对应一个“blade”。每个“刀片”都会绘制一条单独的线。在

我希望每个刀片/线条是我指定的颜色。我正在努力寻找相关文件,如果很明显的话,我很抱歉。在

 def plot_data(avg_rel_track, sd_rel_track_sum, shade):

    fig = plt.figure(figsize=(18,10))

    gs  = gridspec.GridSpec(5, 1, height_ratios=[1.75, 1 ,1, 1])
    ax0 = plt.subplot(gs[0])
    ax1 = plt.subplot(gs[1])
    ax2 = plt.subplot(gs[2])
    ax3 = plt.subplot(gs[3])
    ax4 = plt.subplot(gs[4])

    fig.subplots_adjust(top=0.93)

    #The following plot has 5 plots within it.
    lineObjects = ax0.plot(avg_rel_track_nan)
    ax0.set_title('Averaged Relative Track',fontsize=11)
    ax0.legend(lineObjects, (1,2,3,4,5),loc='lower center', bbox_to_anchor=(0.82, 1),
      fancybox=True, shadow=True, ncol=5)

下面是一个绘图的例子(非常粗略),我想把绘图改成我指定的。这是5个子批次中的一个

This is


Tags: gstrue绘图plot颜色figplttrack
1条回答
网友
1楼 · 发布于 2024-06-13 09:52:36

问题是plot函数只接受一种颜色作为参数。因此,您必须迭代这些列并分别绘制每个列。在

columns = [[1,2,3],[1,4,5],[6,4,2]]
colors = ['green', 'pink', 'blue']
labels = ['foo', 'bar', 'baz']

fig, ax = plt.subplots(1, 1)
for i, column in enumerate(columns):
    ax.plot(column, color=colors[i], label=labels[i])
ax.legend(loc='lower center', bbox_to_anchor=(0.82, 1),
          fancybox=True, shadow=True, ncol=3)

相关问题 更多 >