54

I am using a Jupyter notebook. I have a pretty wide screen, but the displayed output (say, when I print a numpy array) is formatted as if the screen was narrow.

I found a way of increasing the width of the cells, with

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

but this seems to influence the input only, not the output (see screenshots):

short input longer input

I've tried setting the linewidth option in numpy.set_printoptions, I've tried setting numpy.core.arrayprint._line_width, nothing...

EDIT: Using matplotlib I can set the width of plots (that I plot in the notebook with the magic %matplotlib inline) with the command plt.rcParams['figure.figsize']=[X,Y]. It turns out that I can increase X to have plots fill the output cell horizontally all the way. This means (I think) that the original problem it's a numpy thing.

Ziofil
  • 1,815
  • 1
  • 20
  • 30
  • [Have you seen this question?](https://stackoverflow.com/questions/21971449/how-do-i-increase-the-cell-width-of-the-jupyter-ipython-notebook-in-my-browser) – Shtut Jun 05 '17 at 17:29
  • For the reader that arrived from searching about *Pandas*, you can use these two lines: `pd.set_option('display.max_columns', 50) pd.set_option('display.width', 120)` – Rudolf Real Aug 19 '20 at 13:08
  • 1
    Quick one-liner to have longgggggg lines and remove the scientific notation: `np.set_printoptions(suppress=True, linewidth=100000)` – Basj Nov 16 '22 at 11:52

2 Answers2

77

I found this answer helpful in creating my own:

import numpy as np
np.set_printoptions(edgeitems=30, linewidth=100000, 
    formatter=dict(float=lambda x: "%.3g" % x))

The absurd linewidth means only edgeitems and the window's width will determine when newlines/wrapping occurs.

enter image description here

If I shrink the window a bit, it looks like this, so you may still need to play with the edgeitems or formatting:

enter image description here

Here are the docs for set_printoptions, of which the following are relevant:

  • edgeitems : Number of array items in summary at beginning and end of each dimension (default 3).

  • linewidth : The number of characters per line for the purpose of inserting line breaks (default 75).

DharmaTurtle
  • 6,858
  • 6
  • 38
  • 52
  • 2
    To temporarily change the option, use `with np.printoptions(youroption=):`. See https://numpy.org/doc/stable/reference/generated/numpy.set_printoptions.html#numpy.set_printoptions – user8491363 Mar 18 '21 at 09:27
  • the `formatter` is the critical part that makes the other two work. so nice – Marc Compere Mar 19 '21 at 00:24
28

This is a year old now but maybe the answer will help someone else.

The way numpy-arrays are displayed depends on a number of things. With this code, you can show more items and use the full width of your screen:

This is the default

import numpy as np
np.set_printoptions(edgeitems=3)
np.core.arrayprint._line_width = 80

>>> array([[[0, 0, 0, ..., 0, 0, 0],
>>>    [0, 0, 0, ..., 0, 0, 0],
>>>    [0, 0, 0, ..., 0, 0, 0],
>>>    ..., 

With the following code you increase the items shown at the edge of each array (start and end) as well as the line width:

import numpy as np
np.set_printoptions(edgeitems=10)
np.core.arrayprint._line_width = 180

>>> 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],
>>>         [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ...,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
Frank Herfert
  • 460
  • 5
  • 6
  • 12
    numpy version 1.16.2 on python 3.7.3: `np.set_printoptions(edgeitems=10,linewidth=180)` – mvds Apr 28 '21 at 14:51