0

I have a bunch of x and y values and wish to scatter plot them.

plt.scatter(x,y)
savefig('foo.png')

This returns the following error: no display name and no $DISPLAY environment variable

I have a feeling this has to do with the fact that I am running python remotely through my university, so the image cant just "appear". If I could save the image to a file, thatd work for me.

joaquin
  • 82,968
  • 29
  • 138
  • 152
user3259201
  • 99
  • 2
  • 11
  • Typically one should try and make examples runnable (and complete). But good question. – Veedrac Sep 25 '14 at 16:51
  • 1
    Very likely related: http://stackoverflow.com/questions/2801882/generating-a-png-with-matplotlib-when-display-is-undefined – AMacK Sep 25 '14 at 17:10

1 Answers1

0

Here is a complete working example.

import matplotlib.pyplot as plt

# Create your data
x_data = [1,2,3,4,5]
y_data = [5,4,2,3,1]

# Create a figure to hold the plot
fig = plt.figure(figsize=(5,4))     # if you don't define figsize, it should
                                    # still work, depending on your configs

# Create a set of set of axes on the plot (as you might have multiple subplots)
# This says put in an axis at position 1 as if the figure were a 1 by 1 grid
ax = fig.add_subplot(1,1,1)

# Make the scatter plot
ax.scatter(x,y)

# Save the plot
fig.savefig("foo.png")

If this doesn't work, what's probably happening is that your matplotlib is using X somewhere in its backend. You can force it to not by having the following at the top:

import matplotlib
# The important line!
matplotlib.use('Agg')
import matplotlib.pyplot as plt

And do it in that order.

davidlowryduda
  • 2,404
  • 1
  • 25
  • 29