Home > matplotlib, python > [matplotlib] create figures on a remote server

[matplotlib] create figures on a remote server

Problem
On a remote server of mine I wanted to create some nice figures with matplotlib. It worked well on localhost but it failed on the server. First, tkinter was missing. Second, there was a problem with $DISPLAY.

Solution
To install tkinter, check out this earlier post.

The second problem is caused by a missing X server. On a remote server usually there is no graphical interface. To solve it, just add these two lines to the top of your program:

import matplotlib as mpl
mpl.use('Agg')

Example
Before:

#!/usr/bin/env python3
# coding: utf-8

import pylab
import numpy as np

def main():
    x = np.arange(-3.14, 3.14, 0.01)
    y = np.sin(x)
    pylab.plot(x, y, "b")
    pylab.savefig("out.png")

##############################################################################

if __name__ == "__main__":
    main()

After:

#!/usr/bin/env python3
# coding: utf-8

import matplotlib as mpl
mpl.use('Agg')

import pylab
import numpy as np

def main():
    x = np.arange(-3.14, 3.14, 0.01)
    y = np.sin(x)
    pylab.plot(x, y, "b")
    pylab.savefig("out.png")

##############################################################################

if __name__ == "__main__":
    main()

Output:
sine

Categories: matplotlib, python Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: