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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:37:09+00:00 2026-05-31T15:37:09+00:00

I’m just starting with the scipy stack. I’m using the iris dataset, in a

  • 0

I’m just starting with the scipy stack. I’m using the iris dataset, in a CSV version. I can load it just fine using:

iris=numpy.recfromcsv("iris.csv")

and plot it:

pylab.scatter(iris.field(0), iris.field(1))
pylab.show()

Now I’d like to also plot the classes, which are stored in iris.field(4):

chararray(['setosa', ...], dtype='|S10')

What is an elegant way to map these strings to colors for plotting? scatter(iris.field(0), iris.field(1), c=iris.field(4)) does not work (from the docs it expect float values or a colormap). I havn’t found an elegant way of automatically generating a color map.

cols = {"versicolor": "blue", "virginica": "green", "setosa": "red"}
scatter(iris.field(0), iris.field(1), c=map(lambda x:cols[x], iris.field(4)))

does approximately what I want, but I don’t like the manual color specification too much.

Edit: slightly more elegant version of the last line:

scatter(iris.field(0), iris.field(1), c=map(cols.get, iris.field(4)))
  • 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-31T15:37:11+00:00Added an answer on May 31, 2026 at 3:37 pm

    For whatever it’s worth, you’d typically do something more like this in that case:

    import numpy as np
    import matplotlib.pyplot as plt
    
    iris = np.recfromcsv('iris.csv')
    names = set(iris['class'])
    
    x,y = iris['sepal_length'],  iris['sepal_width']
    
    for name in names:
        cond = iris['class'] == name
        plt.plot(x[cond], y[cond], linestyle='none', marker='o', label=name)
    
    plt.legend(numpoints=1)
    plt.show()
    

    enter image description here

    There’s nothing wrong with what @Yann suggested, but scatter is better suited for continuous data.

    It’s easier to rely on the axes color cycle and just call plot multiple times (you also get separate artists instead of a collection, which is a good thing for discrete data such as this).

    By default, the color cycle for an axes is: blue, green, red, cyan, magenta, yellow, black.

    After 7 calls to plot, it will cycle back over those colors, so if you have more items, you’ll need to set it manually (or just specify the color in each call to plot using an interpolated colorbar similar to what @Yann suggested above).

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.