1

I already referred to 'How to print the full NumPy array without wrapping (in Jupyter Notebook)'.

However, my array size is 256x256.

Therefore, it is automatically wrapping in jupyter as below.

enter image description here

My array is based on image and only consisting of 0 and 1.

I just want to see my image shape with 0 and 1 such as below.

I also tried below code.

np.set_printoptions(threshold = np.inf)
np.set_printoptions(linewidth = np.inf)

enter image description here Reference for that post is here.

5
  • it works in my jupyter notebook with np.set_printoptions(linewidth=256*3, threshold=np.inf) maybe some special configure in your jupyter? Commented Apr 19, 2024 at 5:57
  • Have you tried imshow? e.g. import matplotlib.pyplot as plt; plt.imshow(np.squeeze(image)) That can show even very large arrays. Commented Apr 19, 2024 at 6:04
  • There are also nice ways to print arrays that work with Jupyter because it supports MathJax/LaTex. See here, which links to numpyarray_to_latex and examples illustrating using that and related approaches here under 'Rendering Matrices' in 'Subject Matter Authoring Using Jupyter Notebooks;. Commented Apr 19, 2024 at 15:37
  • What is the bottom screenshot you just tacked on? First, just include links and not a screenshot that isn't clickable or searchable, or that allows StackOverflow's system to populate the side bars with related questions. You'll note in How do I ask a good question? it stresses 'DO NOT post images'. Your code is also a screenshot?!?! In fact it says, "DO NOT post images of code, data, error messages, etc." under 'Help others reproduce the problem'. Second, it looks like it should go up around where you are using related approaches in your code. Commented Apr 19, 2024 at 15:42
  • I meant to include along with my links above, this code gist for 'Displaying an array as matrix in LaTeX in a Jupyter notebook'. Commented Apr 19, 2024 at 16:11

1 Answer 1

1

Two problems seem to mix here:

  1. The line breaks introduced by Numpy:

    You can fix these, as already commented and as you already tried, by

    import numpy as np
    np.set_printoptions(linewidth=np.inf)
    
  2. The line breaks (or rather, line wraps) introduced by Jupyter:

    You can fix these, following this answer, by

    from IPython.display import display, HTML
    display(HTML("<style>div.jp-OutputArea-output pre {white-space: pre;}</style>"))
    

Together, we have

import numpy as np
from IPython.display import display, HTML

# Avoid line breaks by Jupyter (following https://stackoverflow.com/a/70433850/7395592)
display(HTML("<style>div.jp-OutputArea-output pre {white-space: pre;}</style>"))
# Avoid premature line breaks by Numpy and show all array entries
np.set_printoptions(linewidth=np.inf, threshold=np.inf)
image = np.zeros((256, 256), dtype=np.uint8)
image

This, for me, produces the following output: image of resulting Jupyter notebook

Alternatively, as you wrote that your image consists of 0s and 1s only (and thus exclusively of single-digit numbers): you can condense the output even more, by suppressing the commas and spaces, for example like so:

import numpy as np
from IPython.display import display, HTML
# Avoid line breaks by Jupyter (following https://stackoverflow.com/a/70433850/7395592)
display(HTML("<style>div.jp-OutputArea-output pre {white-space: pre;}</style>"))
image = np.zeros((256, 256), dtype=np.uint8)
image_vals = "\n".join(f"[{''.join(str(val) for val in row)}]" for row in image)
print(image_vals)

Which produces the following output (note that I had to use print() in this case): image of alternative output

The only inconvenience that I could find in both cases is that a horizontal scrollbar only appears in the output cell if one scrolls to its very bottom (I tried with Firefox and Chrome).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.