7

I've decided to setup a custom matplotlibrc, and thus I need to pick a backend for matplotlib. What is the best one in terms of not needing to install other additional packages?

I tend to use matplotlib via ipython --pylab, so I want interactive window based plots (when I don't I can switch to Agg).

I've tried WXAgg, but this requires (sensibly enough) WX.

9

Both Agg and TkAgg do not require any dependencies beyond Python's standard library.

I have had some issues with TkAgg and multi-threading, so if you only need to save to files (and not plt.show()), I would recommend using Agg instead (just replace it where TkAgg appears below).

Either add the following line to your ~/.config/matplotlib/matplotlibrc:

backend: TkAgg

Or the following lines to your python file:

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
0
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
import numpy as np


x = np.linspace(0,1,100)
y = np.sin(x)

plt.plot(x,y)
plt.show()

PyQt5 is the best backend for Matplotlib from my knowledge. It allows all the edits even after you run your program. To use it,

import matplotlib
matplotlib.use('Qt5Agg')

put this first before calling

import matplotlib.pyplot as plt

N.B. you need to install PyQt5

for installation via pip, run command

pip install PyQt5
0

I prefer inline plots with matplotlib to plots in a separate window because it makes it easier to see what I'm doing when I'm fine tuning the Python code.

Python IDE for scientists (spyder) can do inline plotting. To try out this feature open the terminal and type:

sudo apt install python2.7 python-matplotlib spyder ipython # Python 2.x in Ubuntu 18.04 and earlier 

or

sudo apt install python3 python3-matplotlib spyder3 ipython3 # Python 3.x  

After the ipython prompt in the IPython console copy/paste the below code and press the Enter key to run it.

import matplotlib.pyplot as plt   
x, y = [-1, 12], [1, 4]  
plt.plot(x, y, marker = 'o')  

enter image description here

Atom text editor with the Hydrogen package for Atom installed can also do inline plotting. I find the Atom interface more complicated to configure than Spyder in which inline plotting with matplotlib works out of the box. I got started with inline plotting in Atom because I wanted to use Atom to make inline plots with other programming languages too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.