1

I'm looking to be able to change the colours of lines to ones that I specify on a plot using matplotlib.

Below is the relevant code (it's part of a function) that I'm using. The variable avg_rel_trackis a '2D array' and each column corresponds to a 'blade'. Each 'blade' plots a separate line.

I would like each blade/line to be a colour I specify. I'm struggling to find the relevant documentation, and I'm sorry if it's obvious.

 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)

Below is an example of the plot (Very rough)I would like to change the plots to what I specify. It is a crop out of 5 subplots

This is

4

1 回答 1

3

Problem is that plot function accepts only one color as a parameter. So you'll have to iterate over the columns and plot each one separately.

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)
于 2013-09-16T09:31:15.597 回答