30

When I use ipython terminal and want to print a numpy.ndarray which has many columns, the lines are automatically broken somewhere around 80 characters (i.e. the width of the lines is cca 80 chars):

z = zeros((2,20))
print z

Presumably, ipython expects that my terminal has 80 columns. In fact however, my terminal has width of 176 characters and I would like to use the full width.

I have tried changing the following parameter, but this has no effect:

c.PlainTextFormatter.max_width = 160

How can I tell ipython to use full width of my terminal ?

I am using ipython 1.2.1 on Debian Wheezy

3
  • 2
    By the way, this isn't iPython-specific, so you may want to update your tags to reflect this. The behavior is specific to numpy, and is the same whether you use iPython or any other Python interpreter.
    – Dolda2000
    Sep 2, 2014 at 8:38
  • possible duplicate of Expand width of shell in Python's IDLE
    – simonzack
    Oct 4, 2014 at 3:49
  • The 1980s called. They want their terminal width back. :(
    – Ron Jensen
    Jan 8, 2020 at 3:08

4 Answers 4

38

You can see your current line width with

numpy.get_printoptions()['linewidth']

and set it with

numpy.set_printoptions(linewidth=160)

Automatically set printing width

If you'd like the terminal width to be set automatically, you can have Python execute a startup script. So create a file ~/.python_startup.py or whatever you want to call it, with this inside it:

# Set the printing width to the current terminal width for NumPy.
#
# Note: if you change the terminal's width after starting Python,
#       it will not update the printing width.
from os import getenv
terminal_width = getenv('COLUMNS')
try:
    terminal_width = int(terminal_width)
except (ValueError, TypeError):
    print('Sorry, I was unable to read your COLUMNS environment variable')
    terminal_width = None

if terminal_width is not None and terminal_width > 0:
    from numpy import set_printoptions
    set_printoptions(linewidth = terminal_width)

del terminal_width

and to have Python execute this every time, open your ~/.bashrc file, and add

# Instruct Python to execute a start up script
export PYTHONSTARTUP=$HOME/.python_startup.py
# Ensure that the startup script will be able to access COLUMNS
export COLUMNS
1
  • 2
    I think this should be the accepted answer. While the answer provided by @Dolda2000 works, this method is cleaner and simpler. The extra info on setting up a Python startup script is also useful.
    – damjan
    May 10, 2018 at 16:34
20
+50

After some digging through the code, it appears that the variable you're looking for is numpy.core.arrayprint._line_width, which is 75 by default. Setting it to 160 worked for me:

>>> numpy.zeros((2, 20))
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0., 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

The function used by default for array formatting is numpy.core.numeric.array_repr, although you can change this with numpy.core.numeric.set_string_function.

7
  • 2
    there is no numpy.core.arrayprint._line_width in my $HOME/.config/ipython/profile_default/ipython_config.py. Should I just ad the line there, or does numpy have it's own config file somewhere else? Sep 2, 2014 at 10:53
  • @MartinVegter: I'm not speaking about config files, and I doubt numpy even has one. It's an ordinary Python variable, and you can set it simply by executing numpy.core.arrayprint._line_width = 160 after having imported numpy. If you want to make it default somehow, then I'm afraid I can't help you, since I don't know how iPython does its startup sequence and what kind of rc-files it may or may not run. Depending on your setup, you may want to consider simply editing numpy's source directly.
    – Dolda2000
    Sep 2, 2014 at 15:42
  • If this $HOME/.config/ipython/profile_default/ipython_config.py that you mention is a Python file that iPython simply executes at startup, you may want to consider just putting import numpy and then numpy.core.arrayprint._line_width = 160 in it.
    – Dolda2000
    Sep 2, 2014 at 15:44
  • how can I access numpy.core.arrayprint._line_width, when I import numpy on start using from numpy import *? The variable numpy.core.arrayprint._line_width does not exist. Sep 2, 2014 at 19:36
  • 8
    The variable _line_width seems to be nonexistent (np.__version__ == '1.15.1'). It's now _format_options["linewidth"] (with 75 as default)
    – Daniel
    Oct 18, 2018 at 18:17
7

To automatically resize both numpy and IPython whenever your window size changes, add the following to your ipython_config.py:

import IPython
import signal
import shutil
import sys
try:
    import numpy as np
except ImportError:
    pass

c = get_config()

def update_terminal_width(*ignored):
    """Resize the IPython and numpy printing width to match the terminal."""
    w, h = shutil.get_terminal_size()

    config = IPython.get_ipython().config
    config.PlainTextFormatter.max_width = w - 1
    shell = IPython.core.interactiveshell.InteractiveShell.instance()
    shell.init_display_formatter()

    if 'numpy' in sys.modules:
        import numpy as np
        np.set_printoptions(linewidth=w - 5)

# We need to configure IPython here differently because get_ipython() does not
# yet exist.
w, h = shutil.get_terminal_size()
c.PlainTextFormatter.max_width = w - 1
if 'numpy' in sys.modules:
    import numpy as np
    np.set_printoptions(linewidth=w - 5)

signal.signal(signal.SIGWINCH, update_terminal_width)

If you want to delay loading numpy until necessary, look at Post import hooks in Python 3 for a solution.

If you're not using IPython, put the above in your PYTHONSTARTUP file and remove the IPython-specific lines.

3
  • What is the object c?
    – wim
    Dec 24, 2016 at 0:51
  • @wim thanks for pointing that out. Fixed (it is get_config()). Dec 26, 2016 at 5:17
  • This is nice but seems to work only with Python 3.3 or later. Earlier versions don't seem to have shutil.get_terminal_size()
    – damjan
    May 10, 2018 at 16:59
0

use this method :

np.set_printoptions(linewidth=300)

and widen your iphyton view using this:

from IPython.core.display import display, HTML
display(HTML("<style>.container { width:95% !important; }</style>"))

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.