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.

Bowen Liu
  • 819
  • 1
  • 7
  • 18
  • 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. – Brian Larsen Jun 25 '18 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. – Bowen Liu Jun 25 '18 at 19:30

1 Answers1

1

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.

Daniel
  • 178
  • 1
  • 1
  • 9
  • 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. – Bowen Liu Jun 25 '18 at 19:27
  • What if you try `%%capture` as mentioned here [link](https://stackoverflow.com/questions/23692950/how-do-you-suppress-output-in-ipython-notebook) or here [link](https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/) – Daniel Jun 25 '18 at 19:33
  • 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. – Bowen Liu Jun 25 '18 at 19:46
  • Perfect, so that's a go. – Daniel Jun 25 '18 at 19:54