skip to Main Content

I’m following a Python tutorial book I have (Python Crash Course 2nd Edition) and I’m currently doing Project 2, where they teach about visually representing data with things such as Matplotlib. Unfortunately, the code I run (which is the same as the code in the book) doesn’t make a graph show up anywhere.

The book I’m following expects you to be running the code in Sublime Text, while I’m running it in VS Code, so I originally thought that maybe Sublime would show the graph automatically, but it doesn’t when I tried to run the code over there.

This is my code:

import matplotlib.pyplot as plt

squares = [1, 4, 9, 16, 25]

fig, ax = plt.subplots()
ax.plot(squares)

plt.show()

And I have installed matplotlib in the terminal, using this command:

$ python -m pip install --user matplotlib

The book I’m following says that, "The function plt.show() opens Matplotlib’s viewer and displays the plot as shown in Figure 15-1. The viewer allows you to zoom and navigate the plot, and when you click the disk icon, you can save any plot images you like." Figure 15-1 shows a graph with the various different points connected with lines. And remember, even running the code on Sublime doesn’t make the Matplotlib viewer show up.

Thanks for any help!

3

Answers


  1. I tested your code and it works on my computer in VS Code. Could you please try the following, to find out, what version of python your are using?

    1. Open the terminal in visual studio code and type

      python

    followed by enter.

    1. Something like the following should show up inside the terminal:

      Python 3.8.5 (default, Sep 4 2020, 02:22:02)
      [Clang 10.0.0 ] :: Anaconda, Inc. on darwin
      Type "help", "copyright", "credits" or "license" for more information.

    2. Check if you have a different version of python on your PC as well. Open a second terminal and type

      python3

    followed by enter.

    Are the versions of python the same?

    Maybe you installed matplotlib not for all version. I can not recommend multiple versions of python as a beginner. If you haven’t installed matplotlib in the version of python you are using, you should get an error from your code in VS Code. But this is not the case or?

    Another try:
    You can test your code directly in the terminal:

    1. Open python in the terminal via "python3" or "python".

    2. Paste all your code from your question into the terminal and press enter. Does this work?

    I would like to help, if you could give more information.

    Login or Signup to reply.
  2. Do you mind using Jupyter to run your Matplotlib Python code.

    You can install jupyter extension in extension store.

    Then create a .ipynb file and choose the python kernel. Running your python codes in the Jupyter cell may be more intuitive and convenient.

    enter image description here

    Login or Signup to reply.
  3. blank_issues_enabled: false
    contact_links:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search