Home  /  Resources & support  /  FAQs  /  Using Matplotlib with Stata

How do I use Matplotlib with Stata?

Title   Using Matplotlib with Stata
Author Zhao Xu, StataCorp

Matplotlib is a plotting library for Python which allows you to produce publication quality charts easily and export to a variety of formats. Because of it popularity and long history, a few other plotting libraries are built on top of it.

You can use Matplotlib to create charts that are displayed in an interactive window or you can use Matplotlib script to generate images in various format without output to the screen. Matplotlib uses different rendering engines for different purposes and for different platforms with some overlap. Examples of renderings engines are agg, GDK, Cairo, etc, with backends based on those engines. There are two types of backends, interactive and non-interactive. The interactive backends are for displaying to the screen, while the non-interactive backends are mainly for generating images and saving them to disk.

When you install Matplotlib within Python, a default backend is automatically set. The backend may be different on different operating systems or enviroments. You can find the backend that is currently used by your Python installation in Stata by typing:

. python
python (type end to exit)
>>> import matplotlib
>>> matplotlib.get_backend()
>>> end
.

Depending on your environment, you may want to pay attention to the backend that is used. For example when using Stata for Linux, GTK-based backends may not work properly because Stata's GUI is also implemented using the GTK library. When both Python and Stata use GTK it may cause a conflict that can result in a crash. If this happens, you will need to change the backend to an alternative such as TkAgg. You would also likely need to install system dependencies for an alternative backend. TkAgg requires tkinter, so assuming that you are using Python 3, you would need to install python3-tk. For example, on Ubuntu systems, it can be installed using sudo apt-get install python3-tk.

The following example changes the default backend to TkAgg and outputs a simple chart on the screen from within Stata for Linux.

python:
import matplotlib
matplotlib.use('TkAgg')

import matplotlib.pyplot as plt
plt.plot([2,4,6,8])
plt.show()
end

Notice that we set the backend before importing matplotlib.pyplot. In addition, the show() function will block Stata from executing additional commands until you manually close the window containing the plot. This behavior is different from Stata's graph window but is how Matplotlib behaves when displaying a graph on the screen.

If you only want to export the plot to a file, you can use a non-interactive backend, such as agg, and use the savefig() function:

python:
import matplotlib
matplotlib.use('agg')

import matplotlib.pyplot as plt
plt.plot([2,4,6,8])
plt.savefig("myplot.png")
end

Similarly, when using Matplotlib with Stata through Anaconda for Windows, Qt-based backends may not work properly. This can cause a crash if Python fails to find or load the Qt platform plugin. If this happens, you will need to change the backend to an alternative such as TkAgg, as shown above. You can also tell Stata where to load the plugin. For example, for using Python 3 through Anaconda with Stata, you can set the following in Stata:

python:
import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = "Your\\path\\to\\Anaconda3\\Library\\plugins"
end

Here "Your\\path\\to\\Anaconda3" stands for your installation paths for Anaconda.

See Matplotlib backends for more discussion about backends.