ggplot and python with conda

Update: ggplot in python is not regularly being maintained, so best to work with the R version in RStudio.

SUMMARY – ggplot (python) within conda

This is a guide to setting up the ggplot package for use with python in a conda virtual environment.

This description uses the ‘conda’ application. Conda is not Anaconda, but usually comes with the Anaconda distribution.

Step 1: install anaconda or just ‘pip install conda’ to get conda directly.

Step 2: create a new conda environment

Step 3: install any other python packages you need

Once a conda environment is set up (e.g. if test1 is your conda environment you’ve set up before), you can type this at command prompt:

source activate test1

and it will have you running python with ggplot package using python 3.5 straight off, regardless of what python version is on your system.

Conda virtual environments

Conda is a package manager AND an environment manager. It comes with the Anaconda distribution, so there may be some confusion, but conda is a program on its own, which will independently help you take care of the installs of packages yourself in each environment you create.

Conda has been around since 2012. For its history see http://technicaldiscovery.blogspot.com/2013/12/why-i-promote-conda.html and https://jakevdp.github.io/blog/2016/08/25/conda-myths-and-misconceptions/

Think of conda as a git for setting up programming environments, where each environment is like a git branch (or apt/yum) that you can switch between, to use different package combinations and versions.   It is not python specific in that regard.

(nb: ‘pip’ is python’s own package manager but conda can install python packages too, and will do so from pre-compiled binaries not source. If you use conda, you must install pip inside whatever your active environment is to use it as well.)

Create conda environment

To create and activate a specific environment:
conda create -n newenv
source activate newenv

The current environment appears in parentheses before your command prompt
e.g. (newenv)

conda info –envs or conda env list

(tells you what environments are set up for you)

Refs:
https://conda.io/docs/user-guide/getting-started.html
https://conda.io/docs/_downloads/conda-cheatsheet.pdf

Create a python 3.5 environment for ggplot

The conda-forge version of ggplot is /is not compatible with Python 3.6.: https://anaconda.org/conda-forge/ggplot/files.   Be aware that running ‘conda create –name test python=3’ will probably create a Python 3.6 environment by default, and so you will need to force it to use an earlier version.   First create an environment and specify the python version to use:

conda create –name test1 python=3.5
source activate test1

To see all the packages installed in the current conda environment:

conda list (or conda list –explicit)

You can pipe this list out to a file in PWD with:

conda list –explicit > specs.txt

Refs:
https://conda.io/docs/user-guide/tasks/manage-environments.html

Now install ggplot into the conda environment

Now you can  install a ggplot version into that conda environment:

conda install -c conda-forge ggplot=0.11.5

Since you’ve already set up an environment for python 3.5 conda will restrict the version that can be installed for you.  You can just type this:

conda install -c conda-forge ggplot

Conda will take of downgrading any installed numpy, pandas to the requisite version. It might be better to install ggplot in the conda env before numpy and pandas, because some of the latest numpy, pandas are suited to python 3.6 and will be downgraded to work with ggplot. It also loads in matplotlib-2.0, qt, cairo, patsy, pyparsing, icu, cycler, libxml, pyqt, scipy (18MB), statsmodels-0.9 (15.8MB) etc. ggplot is a little 1.2MB at end, ad well as pixman, glib,jpeg, gettext,

Using a Jupyter notebook in a conda environment

If you are using the command line and want to run a jupyter notebook in a conda environment, type this:

conda install jupyter
jupityer-notebook

ps – this will install quite a few packages, but may downgrade openssl in the environment.   It also installs ‘pandoc 2.2.3.2’

If you’ve already setup a conda environment and run jupyter from within it, it will open a token and your browser will be directed to:
http://localhost:8888/tree

with your current directory loaded up, ready to go.

To run one of your prepared python script files through the notebook:
%load filename
%run filename

(press shift Enter to process each line in the notebook)
(no need for .py at end of filename – Jupyter will understand)

Note that in Jupyter, if you change the script in its cell, you can run the modified script by pressing the Run (>|) button that appears to the left of the cell when you put your mouse near the In [] label. However, until you resave the file the %run command will just run the script from the file, without the changes.

The % commands are magic commands

Ref:
https://ipython.org/ipython-doc/3/interactive/magics.html

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top