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 7820485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:18:52+00:00 2026-06-02T07:18:52+00:00

I am trying to graph families of curves using Matplotlib. I am graphing the

  • 0

I am trying to graph families of curves using Matplotlib. I am graphing the data directly using scatter() and then plotting a fit line (least squares from scipy) using plot(). I do not know how many sets of data there will be beforehand, or the limits, etc.

I need to be able to cycle the colors of these lines and points so everything from one set of data matches. Plot rotates colors using some internal default and scatter is coming out as all one color. The data sets could get close together, so just going with the assumption that it will be clear from which points are close to which fit line is not good enough, and since I don’t know how many curves there will be manually making a color choice is not scalable.

Further, because these are families of curves (think transistor plots), I need to be able to show the relevant labeling with the curve. What I would like to do is to write the information on the fit line itself.

Does anyone know of a good way to either of these?

  • 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-06-02T07:18:53+00:00Added an answer on June 2, 2026 at 7:18 am

    This tries to answer all your questions.
    The code below cycles a maximum of 7 colors. If you need more you should create a more sofisticated generator, as that shown in another answer.

    import numpy as np
    from matplotlib import pyplot as plt
    
    def get_color():
        for item in ['r', 'g', 'b', 'c', 'm', 'y', 'k']:
            yield item
    
    x = 0.3 * np.array(range(40))
    
    color = get_color()
    
    for group in range(5):
        # generates a collection of points
        y = np.exp2(x + 0.5 * group)
        # fit to a polynomial
        z = np.polyfit(x, y, 6)
        p = np.poly1d(z)
    
        acolor = next(color)
        
        plt.scatter(x, y, color=acolor, marker='o')
        plt.plot(x, p(x), acolor + '-', label=str(group))
    
    plt.legend()
    plt.xlim((0, 15))
    plt.show() 
    

    enter image description here

    The generator in the above code is a bit of an overkilling for the example, but it gives the structure for a more complex calculation. If you only need a few colors you could use a simple iterator

    >>> color = iter(list_of_colors)
    >>> acolor = next(color)
    

    and if you need to cycle endlessly, you can use itertools.cycle:

    >>> from itertools import cycle
    >>> color = cycle(['r', 'g', 'b', 'c', 'm', 'y', 'k'])
    >>> next(color)
    'r'
    >>> 
    

    Edit: You have several options to get n diferent colors. As I indicated before you can use a generator using the method indicated in other answer. For example, replacing get_color with a different generator:

    import colorsys
    import numpy as np
    from matplotlib import pyplot as plt
    
    def get_color(color):
        for hue in range(color):
            hue = 1. * hue / color
            col = [int(x) for x in colorsys.hsv_to_rgb(hue, 1.0, 230)]
            yield "#{0:02x}{1:02x}{2:02x}".format(*col)
        
    x = 0.3 * np.array(range(40))
    
    color = get_color(15)
    
    for group in range(15):
        # generates a collection of points
        y = np.exp2(x + 0.5 * group)
        # fit to a polynomial
        z = np.polyfit(x, y, 6)
        p = np.poly1d(z)
    
        acolor = next(color)
    
        plt.scatter(x, y, color=acolor, marker='o')
        plt.plot(x, p(x), color=acolor, linestyle='dashed', label=str(group))
    
    plt.legend()
    plt.xlim((0, 15))
    plt.show() 
    

    You get 15 different colors.

    enter image description here

    Similar colors are however contiguous not giving a good resolution/contrast. You can increase contrast by skipping hue values with:

    for hue in range(0, color*3, 3):
    

    The other problem when drawing many lines is the legend…

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

Sidebar

Related Questions

I'm trying to show an intercept on a line graph using the ggplot vline
I am trying to display a real time graph using the data which is
I'm trying to plot a graph in matplotlib using numpy and meshgrid. I want
I'm currently trying to graph a sphere in a tkinter window using matplotlib. How
I'm trying to display an svg graph with associated javascript in my application using
I am trying to draw a graph showing search prefixes using twopi. I have
I am trying to represent a graph using disjoint union and record. The following
I am trying to retrieve information through Facebook graph api, using the below code
I'm trying to make a force directed graph using d3.layout.force , and I need
I am trying to print a report that contains a bar graph using the

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.