Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7499317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:47:09+00:00 2026-05-29T19:47:09+00:00

I am trying to plot a variable number of lines in matplotlib where the

  • 0

I am trying to plot a variable number of lines in matplotlib where the X, Y data and colors are stored in numpy arrays, as shown below. Is there a way to pass an array of colors into the plot function, so I don’t have to take an extra step to assign a color to each line individually? Should I be translating the RGB color arrays to another color format for this to work, such as HSV or other?

import numpy as np
X = np.arange(1990, 1994)
Y = [[  1.50615936e+08   5.88252480e+07   2.60363587e+08]
     [  1.53193798e+08   5.91663430e+07   2.63123995e+08]
     [  1.55704596e+08   5.94899260e+07   2.65840188e+08]
     [  1.58175186e+08   5.97843680e+07   2.68559452e+08]]
colors = [(0.99609375, 0.3984375, 0.3984375) (0.796875, 0.0, 0.99609375)
          (0.59765625, 0.99609375, 0.0)]
#current way
ax.plot(X, Y)
[ax.lines[i].set_color(color) for i, color in enumerate(colors)]
#way I feel it can be done, but doesn't work currently
ax.plot(X, Y, color=colors)
plt.show()

Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:47:11+00:00Added an answer on May 29, 2026 at 7:47 pm

    I think you want to use the Axes method set_color_cycle. As you can imagine, it sets the list of colors that are cycled through when colors are assigned by default, i.e. when no color keyword is provided to the plot call. Here’s an extended version of your example:

    import matplotlib.pyplot as plt
    import numpy as np
    
    X = np.arange(1990, 1994)
    Y = [[  1.50615936e+08,   5.88252480e+07,   2.60363587e+08],
         [  1.53193798e+08,   5.91663430e+07,   2.63123995e+08],
         [  1.55704596e+08,   5.94899260e+07,   2.65840188e+08],
         [  1.58175186e+08,   5.97843680e+07,   2.68559452e+08]]
    colors = [(0.99609375, 0.3984375, 0.3984375), 
              (0.796875, 0.0, 0.99609375),
              (0.59765625, 0.99609375, 0.0)]
    
    fig = plt.figure()
    ax1 = fig.add_subplot(211)
    ax1.set_title('old way')
    ax1.plot(X, Y)
    [ax1.lines[i].set_color(color) for i, color in enumerate(colors)]
    
    ax2 = fig.add_subplot(212)
    ax2.set_title('new way')
    ax2.set_color_cycle(colors)
    ax2.plot(X, Y)
    
    fig.savefig('manycolors.py')
    plt.show()
    

    This results in two subplots with the identically colored lines:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to plot some prices vs dates. There are about 10k lines of
I'm trying to group one variable of my data by another and then plot
I am trying plot data sets consisting of 3 coordinates: X-coordinate, x-coordinate and the
I am trying to plot the following ! from numpy import * from pylab
I have the following data set that I am trying to plot with ggplot2,
I'm trying to annotate a polar plot with data tips labelled with 'R:...,Theta:...' where
I have a plot (sample code pasted below) that I am trying to add
I have one y variable, which I am trying to plot against two related
I am trying to plot side by side the following datasets dataset1=data.frame(obs=runif(20,min=1,max=10)) dataset2=data.frame(obs=runif(20,min=1,max=20)) dataset3=data.frame(obs=runif(20,min=5,max=10))
I am trying to make a tiled plot with ggplot2 with a single variable.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.