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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:10:43+00:00 2026-05-20T17:10:43+00:00

I have an array of floats that I have normalised to one (i.e. the

  • 0

I have an array of floats that I have normalised to one (i.e. the largest number in the array is 1), and I wanted to use it as colour indices for a graph. In using matplotlib to use grayscale, this requires using strings between 0 and 1, so I wanted to convert the array of floats to an array of strings. I was attempting to do this by using “astype(‘str’)”, but this appears to create some values that are not the same (or even close) to the originals.

I notice this because matplotlib complains about finding the number 8 in the array, which is odd as it was normalised to one!

In short, I have an array phis, of float64, such that:

numpy.where(phis.astype('str').astype('float64') != phis)

is non empty. This is puzzling as (hopefully naively) it appears to be a bug in numpy, is there anything that I could have done wrong to cause this?

Edit: after investigation this appears to be due to the way the string function handles high precision floats. Using a vectorized toString function (as from robbles answer), this is also the case, however if the lambda function is:

lambda x: "%.2f" % x

Then the graphing works – curiouser and curiouser. (Obviously the arrays are no longer equal however!)

  • 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-20T17:10:44+00:00Added an answer on May 20, 2026 at 5:10 pm

    You seem a bit confused as to how numpy arrays work behind the scenes. Each item in an array must be the same size.

    The string representation of a float doesn’t work this way. For example, repr(1.3) yields '1.3', but repr(1.33) yields '1.3300000000000001'.

    A accurate string representation of a floating point number produces a variable length string.

    Because numpy arrays consist of elements that are all the same size, numpy requires you to specify the length of the strings within the array when you’re using string arrays.

    If you use x.astype('str'), it will always convert things to an array of strings of length 1.

    For example, using x = np.array(1.344566), x.astype('str') yields '1'!

    You need to be more explict and use the '|Sx' dtype syntax, where x is the length of the string for each element of the array.

    For example, use x.astype('|S10') to convert the array to strings of length 10.

    Even better, just avoid using numpy arrays of strings altogether. It’s usually a bad idea, and there’s no reason I can see from your description of your problem to use them in the first place…

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

Sidebar

Related Questions

I am not that experienced with Java/Android programming. I have an array of floats
I have an array of Floats that need to be converted to a byte
I currently have a large array of floats that I process in my OpenCL
I ran some CUDA code that updated an array of floats. I have a
I have an array in Perl: my @my_array = (one,two,three,two,three); How do I remove
I have an array of numbers that potentially have up to 8 decimal places
I have an array of floats where data are stored with varying decimal points
Right, I have an array for floats which stores only 1s and 0s. I'm
I want a function that creates an array of increasing floats. I run into
That is, on disk, if I have an array of std::complex , is it

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.