0

I made a couple of plots before using Python 2.7 and everything is fine. Now I am trying to pick it up in Python 3 as I am trying to visualize some of the data output of the project I'm working on. So I tried to see if this works:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

# fake data:
a = np.random.normal(size=1000)
b = a*3 + np.random.normal(size=1000)

plt.hist2d(a, b, (50, 50), cmap=plt.cm.jet)
plt.colorbar()

The result is quite confusing for me: it shows the plot but before the plot it also shows the list of value of a and b, as shown in the picture below:

Part1 Part2

All I need is a clean graph of the plot. So what have I done wrong here? Haven't used matplotlib for a long time so I guess I have made some big mistakes here.

Thanks for your time in advance.

2
  • 1
    One hint here is to add a semicolon (;) to the end of the plot line. This will suppress the text output in the jupyter notebook. Commented Jun 25, 2018 at 19:15
  • Damn, I was trying to add ; to each plt. line. And apparently it only works if I added in another line : plt.show(). This is great. Thanks a lot. Exactly what I need. Commented Jun 25, 2018 at 19:30

1 Answer 1

2

I am not an expert but what happens is that you are getting as results the variables that make your plot. I have run it into Spyder and on the right (variables section) I also get your results. What you need to do however, is to write explicitly "show the plot":

....
plt.colorbar
plt.show()

This will plot automatically your plot in a new window without showing all the arrays. Here some explanation Previous post.

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

4 Comments

Thanks for the reply. I'm using the Jupyter Notebook. I add the "plt.show()" at the end but the output is still the same.
What if you try %%capture as mentioned here link or here link
Somehow this makes no difference, but the semicolon method mentioned in a comment in your first link works. I just put the ; at the end of "plt.show()" and it worked. Thanks.
Perfect, so that's a go.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.