Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Python Matplotlib with OSx and virtualenv?

I just started learning Python's libraries for data analysis (Numpy, Pandas and Matplotlib) but have already stumbled across my first problem - getting the Matplotlib to work with virtualenv.

When working with Python I always create a new virtual environment, get my desired Python version and libraries/dependancies into this environment. This time, the course required that I use Python3, so I installed it via HomeBrew.

The challenge:

  • Matplotlib needs to interact with OS
  • to make this happen it needs the framework build of Python (the system one)
  • ...which is not possible if it's inside of a virtualenv, which makes it use the virtualenv build of Python

The workaround that Is supposed to be common is described at this link but I am unsure how to use this (the OSX section).

My understanding of the solution:

  1. get Python version that I wish to use, install it system wide, NOT in a virtualenv
  2. create a virtualenv, get dependencies that I need, this creates the virtualenv Python build
  3. somehow trick the system into using virtualenv dependancies with system build of Python
  4. this is done with the shell script(?) which seems to modify certain variables in shell/terminal config file(s)

Questions:

  1. am I correct with the above "explanation to myself"?
  2. what is the correct way to do this? from within the virtualenv, from outside of it...?
  3. after this is done, how do I execute my Python scripts? with my virtualenv activated or not?

Many thanks!

like image 427
Alexander Starbuck Avatar asked Sep 07 '16 10:09

Alexander Starbuck


1 Answers

If you are using Python 2.x then, use these commands in virtual environment:

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

This makes the matplotlib work in the virtual environment too.

I used this steps to make the matplotlib running in the virtual environment.

like image 60
sufi Avatar answered Nov 15 '22 12:11

sufi