Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

python - How to set max output width in numpy?

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.

question from:https://stackoverflow.com/questions/37149933/how-to-set-max-output-width-in-numpy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...