Pandas printing in jupyter

Rustom Mody rustompmody at gmail.com
Tue Jan 16 07:33:39 EST 2018


On Tuesday, January 16, 2018 at 5:10:14 PM UTC+5:30, Rustom Mody wrote:
> On Sunday, January 14, 2018 at 3:28:02 AM UTC+5:30, bo... at questa.la.so wrote:
> > Rustom Mody  writes:
> > 
> > > Specifically and for starters, I want a numpy array — lets say 2D to
> > > start with — to be displayed(displayable) as elegantly as sympy does
> > > to (its) matrices
> > ########################################################################
> > import numpy as np
> > from IPython.display import Latex
> > 
> > def prmat(mat):
> >     return (r'\begin{bmatrix}' +
> >             r'\\'.join('&'.join('%f'%x for x in row) for row in mat) +
> >             r'\end{bmatrix}'
> > 
> > a = np.arange(12).reshape((3, 4))+1
> > display(Latex(prmat(a)))
> > ########################################################################
> > you could add optional arguments to modify the type of brackets and the
> > formatting string
> 
> Thanks
> 
> Well I had to tiny-tweak the code (import the display)
> ---------------
> 
> import numpy as np
> from IPython.display import Latex, display
> 
> def prmat(mat):
>     return (r'\begin{bmatrix}' +
>             r'\\'.join('&'.join('%f'%x for x in row) for row in mat) +
>             r'\end{bmatrix}' )
> 
> a = np.arange(12).reshape((3, 4))+1
> display(Latex(prmat(a)))
> ---------------
> 
> After that it works… for 5 seconds!!
> 
> ie it shows a nice centered matrix like a math-display in latex
> Then it goes away and I see a left aligned bunch of unicode boxes!

Inspired by this I tried this
It works… kinda… but the matrix columns dont align

def prmat(mat):
    return (r'\begin{bmatrix}' +
            r'\\'.join('&'.join('%d'%x for x in row) for row in mat) +
            r'\end{bmatrix}' )

matprefix = """
  <math>
    <mrow>
      <mo>[</mo>
      <mtable>
"""
rowprefix = """        <mtr>
"""
elemfmt = """            <mn>%d</mn>
"""
rowsuffix  = """        </mtr>
"""
matsuffix = """      </mtable>
      <mo>]</mo>
    </mrow>
  </math>
"""
def prmht(mat):
    return (matprefix + "".join(prmhtrow(r) for r in mat) + matsuffix)

def prmhtrow(row):
    return rowprefix + "".join(elemfmt%x for x in row) + rowsuffix

display(HTML(prmht(a)))



More information about the Python-list mailing list