Matplotlib For Mac



With matplotlib, you can generate histograms, bar charts, scatter plots, 3D plots, and many more types of figures. This even has a pyplot interface closely resembling MATLAB, if you use that and would like some simple plotting. Matplotlib is a low-level library that has incredible levels of customization for any type of user, especially if you. In this tutorial, we will learn about installing matplotlib on different operating systems like windows and mac os, we will also have a brief look at how to work with matplotlib in jupyter notebook. In order to start working with matplotlib, we need to make sure it’s installed first on our system.

I’ve got a weird problem with one of my Python scripts. It creates a chart with animation, and this animation works fine on Windows and Linux, but on Mac OS it just doesn’t show up. Here’s a video demonstration:

Python install matplotlib pip

If video doesn’t play in your browser, you can download it here

The full script is published here.

To run the script I installed only matplotlib and numpy packages, so the environment on all 3 systems (Windows, Linux, Mac OS) should be the same. Just in case, here they are:

I’ve tried other simple plot animation samples (for instance, the ones from matplotlib documentation), and all of them work fine on Mac OS, so there has to be something wrong with my particular script. However, the fact that this very same script works on Windows and Linux confused me a lot. As a side note, you might have noticed that animation speed is different between Windows and Linux (the latter is faster), and I’ve no idea what could be the reason of that either, though it’s not so important here.

I’ve also tested my script on 3 other Macs with different Mac OS and Python versions installed, just to make sure that it’s not my environment who messes things up, but it was the same on all of them - no animation showing up.

After submitting a question at Stack Overflow and also at matplotlib forum, I thought about reading the documentation (classic).

The right backend

There I soon enough discovered the backends section. That helped me to google some more ideas, for instance this answer shown what’s needed to make animation from my script work in Jupyter notebook - just a single line in the very beginning of the script:

And then animation started to work. Okay, so what backend does Jupyter uses in this case then:

It returned TkAgg - so that’s what can show animations in my script! Now I only need to set the same backend in my standalone script.

By the way, if you are curious, without %matplotlib Tk Jupyter reports module://ipykernel.pylab.backend_inline value for backend.

With pyplot.rcParams

Using the same print statement, I’ve discovered what backend is used by default when running my script in terminal: MacOSX. Right then, so this backend is incapable to run my animation for some reason. But since I now know the backend that is capable, I’ve set it like this:

But even though it reported a new backend in the output, the animation still wasn’t showing up:

So setting the backend via plt.rcParams['backend'] isn’t supported? That’s a bit unexpected, as according to the documentation, it should be.

With matplotlib.use

Anyway, after some more googling I’ve found this answer, and that was what finally worked:

Now my script runs and shows the animation. It also prints the following warning to the output:

…which is due to the fact that default Tcl/Tk on Mac OS is outdated (8.5 at the moment), and if you (like me) are getting Python from Homebrew, then it won’t be trivial to link it with the latest version of Tcl/Tk. But for now it works as it is, so it’s all good for the time being.

As it was pointed out in the bugreport, the line is added anew to the plot each time inside animate() function, instead of modifying existing line, and that’s what was causing the chart to go all rainbowy.

After fixing the script like this:

I now get the following result even with default MacOSX backend:

If video doesn’t play in your browser, you can download it here

Almost good, except for this horizontal line (by the way, it’s not present with TkAgg backend). Setting blit=False gets rid of this line, and also causes the animation to play considerably slower (but that’s not really a problem).

In this lesson, we will briefly discuss the imports and configurations required throughout the rest of this course.

Matplotlib

Setting Matplotlib Charts to Display Within Jupyter Notebooks

Every lesson in this course will assume that you have run the following code before creating any Python visualizations:

You probably have two questions:

  • What is the meaning of the % syntax?
  • What does this command do?

First, let's discuss the % syntax.

The % operator is used in iPython notebooks (of which the Jupyter Notebook is an example) to reference magic functions. These are special functions that only work in iPython notebooks (like Jupyter). Here is more information on magic functions from the iPython documentation:

Mac

'IPython has a set of predefined ‘magic functions’ that you can call with a command line style syntax. There are two kinds of magics, line-oriented and cell-oriented. Line magics are prefixed with the % character and work much like OS command-line calls: they get as an argument the rest of the line, where arguments are passed without parentheses or quotes. Lines magics can return results and can be used in the right hand side of an assignment. Cell magics are prefixed with a double %%, and they are functions that get as an argument not only the rest of the line, but also the lines below it in a separate argument.'

So what is the purpose of %matplotlib inline command? Let's again refer to iPython's documentation:

'With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.'

This means that the visualizations we produce will show directly in our Jupyter Notebook instead of opening in a separate window.

How To Import Matplotlib

Matplotlib has a variety of modules available for import. To begin this course, we will be using the pyplot module, which is typically imported under the alias plt.

The full command for importing this is below:

How To Test That Matplotlib Has Installed Correctly

Let's test that matplotlib has been installed correctly by creating a very basic line plot. First, create a basic dataset with the range function:

Next, plot this data using pyplot's plot method:

If your output looks like this, then you can proceed!

A Note For Mac Users

If you are working through this course on a Mac that has a Retina display, you may notice that the chart we just created is pretty blurry.

IPython has a built-in solution for this. You can run the following code to change your Jupyter Notebook to 'Retina Mode', which significantly improves the resolution of your visualizations:

Moving On

Install Matplotlib For Mac

In this lesson, we learned how to import matplotlib's pyplot module and how to set visualizations to display within a Jupyter Notebook using %matplotlib inline. Let's move on to creating some real charts in the next section!