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

  • Home
  • SEARCH
  • 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 8310175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:21:56+00:00 2026-06-08T19:21:56+00:00

I am creating a distance matrix in numpy, with an out put as such:

  • 0

I am creating a distance matrix in numpy, with an out put as such:

  ['H', 'B', 'D', 'A', 'I', 'C', 'F']
[[ 0.   2.4  6.1  3.2  5.2  3.9  7.1]
 [ 2.4  0.   4.1  1.2  3.2  1.9  5.1]
 [ 6.1  4.1  0.   3.1  6.9  2.8  5.2]
 [ 3.2  1.2  3.1  0.   4.   0.9  4.1]
 [ 5.2  3.2  6.9  4.   0.   4.7  7.9]
 [ 3.9  1.9  2.8  0.9  4.7  0.   3.8]
 [ 7.1  5.1  5.2  4.1  7.9  3.8  0. ]]

I am printing that x axis by just printing a list before I print the actual matrix, a:

print" ", names
print a

I need the axis in that order, as the list ‘names’ properly orders the variables with their value in the matrix. But how would i be able to get a similar y axis in numpy?

  • 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-08T19:22:00+00:00Added an answer on June 8, 2026 at 7:22 pm

    It is not so pretty, but this pretty table prints works:

    import numpy as np
    
    names=np.array(['H', 'B', 'D', 'A', 'I', 'C', 'F'])
    a=np.array([[ 0.,   2.4,  6.1,  3.2,  5.2,  3.9,  7.1],
     [2.4,  0.,   4.1,  1.2,  3.2,  1.9,  5.1],
     [6.1,  4.1,  0.,   3.1,  6.9,  2.8,  5.2],
     [3.2,  1.2,  3.1,  0.,   4.,   0.9,  4.1],
     [5.2,  3.2,  6.9,  4.,   0.,   4.7,  7.9],
     [3.9,  1.9 , 2.8,  0.9,  4.7,  0.,   3.8],
     [7.1,  5.1,  5.2,  4.1,  7.9,  3.8,  0. ]])
    
    def pptable(x_axis,y_axis,table):
        def format_field(field, fmt='{:,.2f}'):
            if type(field) is str: return field
            if type(field) is tuple: return field[1].format(field[0])
            return fmt.format(field)     
    
        def get_max_col_w(table, index):
            return max([len(format_field(row[index])) for row in table])         
    
        for i,l in enumerate(table):
            l.insert(0,y_axis[i])
    
        x_axis.insert(0,' ')    
        table.insert(0,x_axis)    
        col_paddings=[get_max_col_w(table, i) for i in range(len(table[0]))]
        for i,row in enumerate(table):
            # left col
            row_tab=[str(row[0]).ljust(col_paddings[0])]
            # rest of the cols
            row_tab+=[format_field(row[j]).rjust(col_paddings[j]) 
                    for j in range(1,len(row))]
            print(' '.join(row_tab))         
    
    x_axis=['x{}'.format(c) for c in names]
    y_axis=['y{}'.format(c) for c in names]
    
    pptable(x_axis,y_axis,a.tolist()) 
    

    Prints:

         xH   xB   xD   xA   xI   xC   xF
    yH 0.00 2.40 6.10 3.20 5.20 3.90 7.10
    yB 2.40 0.00 4.10 1.20 3.20 1.90 5.10
    yD 6.10 4.10 0.00 3.10 6.90 2.80 5.20
    yA 3.20 1.20 3.10 0.00 4.00 0.90 4.10
    yI 5.20 3.20 6.90 4.00 0.00 4.70 7.90
    yC 3.90 1.90 2.80 0.90 4.70 0.00 3.80
    yF 7.10 5.10 5.20 4.10 7.90 3.80 0.00
    

    If you want the X and Y axis to be the same, just call it with two lists of the same labels.

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

Sidebar

Related Questions

I'm just learning C++ and programming. I'm creating a class called Distance . I
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
I'm creating an application that will tell a user how far away a large
I'm playing with an idea of creating client that would use the torrent protocol
I am creating an application that highlights user messages from a stream based on
I'm trying to figure out the best method of getting/displaying the shortest distance between
I am creating an application where i have a list of images. On clicking
I'm thinking about creating a location-aware iPhone app that could work offline by coming
I'm creating a plot of a robot's belief of its distance to a landmark.
Hi im working through the Creating a store locator tutorial on google that can

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.