4

I have a 2D numpy array (y_array) with 3 columns (and common x values as a list, x_list) and I want to create a plot with each column plotted as a line. I can do this by simply doing matplotlib.pyplot.plot(x_list, y_array) and it works just fine.

However I am struggeling with the colors. I need to assign custom colors to each line. I tried by handing a list of my custom colors to the color=key argument, but apparently it does not take a List and throws a ValueError. I find this particularly odd since giving a list for labels actually does work.

I also thought about creating a custom colormap from my choosen colors, but I do not know how to switch to this colormap when plotting...

What can I do to specify the colors to be used when plotting the array? I would like to avoid iterating over the array columns in a for loop.

Thanks in advance!

Edit: minimal working example: This throws the mentioned ValueError

import matplotlib.pyplot as plt
import numpy as np


if __name__ == '__main__':

    x_list = np.linspace(0, 9, 10)
    y1 = np.random.uniform(10, 19, 10)
    y2 = np.random.uniform(20, 29, 10)
    y3 = np.random.uniform(30, 39, 10)
    y_array = np.column_stack([y1, y2, y3])

    labels = ['A', 'B', 'C']
    # plt.plot(x_list, y_array, label=labels)  # this works just fine
    my_colors = ['steelblue', 'seagreen', 'firebrick']
    plt.plot(x_list, y_array, label=labels, color=my_colors)  # throws ValueError
    plt.legend()

    plt.show()
4
  • what's the problem with iterating over 3 columns? Commented Jul 15, 2022 at 14:14
  • could you show your code? It will be easier than explaining what you did. Commented Jul 15, 2022 at 14:27
  • 1
    @Stef well since plot() supports plotting all array columns from just one line of code i somehow expected/hope there would be away to specify colors without giving the plot command for each line individually. Would be cleaner somehow in my opinion... Commented Jul 15, 2022 at 15:18
  • 2
    if you don't want to go to the trouble of creating your own color cycler, you can just adjust the colors after plotting: lines = plt.plot(x_list, y_array, label=labels) followed by for line, color in zip(lines, my_colors): line.set_color(color). Commented Jul 15, 2022 at 15:52

1 Answer 1

3

You can create a custom cycler and use it to define your colors.

You can get more information here

import matplotlib.pyplot as plt
import numpy as np
from cycler import cycler


if __name__ == '__main__':
    my_colors = ['steelblue', 'seagreen', 'firebrick']
    custom_cycler = cycler(color=my_colors)
                  
    x_list = np.linspace(0, 9, 10)
    y1 = np.random.uniform(10, 19, 10)
    y2 = np.random.uniform(20, 29, 10)
    y3 = np.random.uniform(30, 39, 10)
    y_array = np.column_stack([y1, y2, y3])
    
    fig, ax = plt.subplots()
    ax.set_prop_cycle(custom_cycler)
    ax.plot(x_list, y_array)    
    plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Ah yes! That's kind of what I had in mind when talking about changing the color map. I will wait a bit with marking this as the answer whether anyone comes up with a magic way to hand the list of colors directly to plot(). I just don't get why it works vor label= but not for color=
the cycler is an element of the axes, not the plot. you can call plot again on the same axes and it will cycle the next color. I don't think you can pass your cycler directly to the plot call

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.