Plotting a matrix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dunnomuch
    New Member
    • May 2010
    • 5

    Plotting a matrix

    Hi,

    I've got a little problem in plotting a matrix that is build up in a class. Cause I've build it up in a class, I can't just plot the different arrays out of which it would consist when you build it up like this:
    [[...],[...], ... , [...]]

    I've tried to plot it by making a loop that consistently plots the rows which have f1, f2 and t in it (Respectively being the first function, the second function and the time). The problem I've got is that when I use this method, I plotting a part of my graph twice (you can see 2 lines that indicate that de functions go over this part twice). This is not what I should get.
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    Hi

    I'm afraid your question doesn't make much sense. Can you clarify what you mean by plot, and maybe post some code that helps explain the problem? What should you get? Unfortunately computer programmes have an annoying habit of doing what they're told...;P

    Comment

    • Dunnomuch
      New Member
      • May 2010
      • 5

      #3
      Hi,

      The matrix I mean looks like this:

      Code:
      class Matrix(object): 
          def __init__(self, kolommen, rijen): 
              self.kolommen = kolommen 
              self.rijen = rijen 
              # Creeer matrix met nullen
              self.matrix = [] 
              for i in range(rijen): 
                  ea_rijen = [] 
                  for j in range(kolommen): 
                      ea_rijen.append(0) 
                  self.matrix.append(ea_rijen) 
        
          def plaatselement(self, kolom, rij, v):   #Zet element v op plaats [kolom, rij]
              self.matrix[kolom-1][rij-1] = v 
        
          def neemelement(self, kolom, rij):  #Neemt element op plaats [kolom, rij]
              return self.matrix[kolom-1][rij-1] 
        
          def __repr__(self): #Dient om matrix te printen
              outStr = "" 
              for i in range(self.rijen): 
                  outStr += 'Rij %s = %s\n' % (i+1, self.matrix[i]) 
              return outStr
      I just fill it with numbers which are the solution of an N-dimensional ODE. The solving method I use is Forward Euler, but it shouldn't matter what's in the matrix. I now want to plot 2 collums out of this matrix. Is there a way to do this?

      Comment

      • Glenton
        Recognized Expert Contributor
        • Nov 2008
        • 391

        #4
        Assuming you've got matplotlib installed (which you should do, along with numpy), the following will use your class (unadjusted) and plot one column against another graphically:

        Code:
        class Matrix(object): 
        
            def __init__(self, kolommen, rijen): 
        
                self.kolommen = kolommen 
        
                self.rijen = rijen 
        
                # Creeer matrix met nullen
        
                self.matrix = [] 
        
                for i in range(rijen): 
        
                    ea_rijen = [] 
        
                    for j in range(kolommen): 
        
                        ea_rijen.append(0) 
        
                    self.matrix.append(ea_rijen) 
        
         
        
            def plaatselement(self, kolom, rij, v):   #Zet element v op plaats [kolom, rij]
        
                self.matrix[kolom-1][rij-1] = v 
        
         
        
            def neemelement(self, kolom, rij):  #Neemt element op plaats [kolom, rij]
        
                return self.matrix[kolom-1][rij-1] 
        
         
        
            def __repr__(self): #Dient om matrix te printen
        
                outStr = "" 
        
                for i in range(self.rijen): 
        
                    outStr += 'Rij %s = %s\n' % (i+1, self.matrix[i]) 
        
                return outStr 
        
        
        
        #Create matrix
        
        M=Matrix(10,10)
        
        for i in range(10):
        
            for j in range(10):
        
                M.plaatselement(i,j,i+j)
        
        
        
        #Plot 4th column vs 6th column
        
        import pylab
        
        pylab.plot(M.matrix[:][3],M.matrix[:][5])
        
        pylab.show()

        Comment

        Working...