pythonmatrixoutputpycharmline-breaks

Prohibit automatic linebreaks in Pycharm Output when using large Matrices


I'm working in PyCharm on Windows. In the project I'm currently working on I have "large" matrices, but when i output them Pycharm automatically adds linebreaks so that one row occupys two lines instead of just one:

 [[ 3.         -1.73205081  0.          0.          0.          0.          0.
       0.          0.          0.        ]
     [-1.73205081  1.         -1.         -2.          0.          0.          0.
       0.          0.          0.        ]
     [ 0.         -1.          1.          0.         -1.41421356  0.          0.
       0.          0.          0.        ]
     [ 0.         -2.          0.          1.         -1.41421356  0.
      -1.73205081  0.          0.          0.        ]
     [ 0.          0.         -1.41421356 -1.41421356  0.         -1.41421356
       0.         -1.41421356  0.          0.        ]
     [ 0.          0.          0.          0.         -1.41421356  0.          0.
       0.         -1.          0.        ]
     [ 0.          0.          0.         -1.73205081  0.          0.          3.
      -1.73205081  0.          0.        ]
     [ 0.          0.          0.          0.         -1.41421356  0.
      -1.73205081  1.         -2.          0.        ]
     [ 0.          0.          0.          0.          0.         -1.          0.
      -2.          0.         -1.73205081]
     [ 0.          0.          0.          0.          0.          0.          0.
       0.         -1.73205081  0.        ]]

It make my results very hard to reed and to compare. The window is big enough so that everything should be displayed but it still breaks the rows. Is there any setting to prevent this?

Thanks in advance!


Solution

  • PyCharm default console width is set to 80 characters. Lines are printed without wrapping unless you set soft wrap in options: File -> Settings -> Editor -> General -> Console -> Use soft wraps in console.

    However both options make reading big matrices hard. You can fix this in few ways.

    With this test code:

    import random
    m = [[random.random() for a in range(10)] for b in range(10)]
    print(m)
    

    You can try one of these:

    Pretty print

    Use pprint module, and override line width:

    import pprint
    pprint.pprint(m, width=300)
    

    Numpy

    For numpy version 1.13 and lower:

    If you use numpy module, configure arrayprint option:

    import numpy
    numpy.core.arrayprint._line_width = 300
    print(numpy.matrix(m))
    

    For numpy version 1.14 and above (thanks to @Alex Johnson):

    import numpy
    numpy.set_printoptions(linewidth=300)
    print(numpy.matrix(m))
    

    Pandas

    If you use pandas module, configure display.width option:

    import pandas
    pandas.set_option('display.width', 300)
    print(pandas.DataFrame(m))