views:

86

answers:

1

What happened is I followed this demo, I modified it to suit my needs had it working, changed it to use a function to draw two graphs but now it doesn't work at all using plt.show() or plt.savefig()

here's my code

import csv
import numpy as np

import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

# I converted excel to a csv file
data = [x for x in csv.reader(open('ASS1_Q1.csv'))]

question1 = {}
question1['males'] = []
question1['females'] = []
for x in data:
    if x[0].lower() == "male":
        question1["males"].append(float(x[1]))
    elif x[0].lower() == "female":
        question1['females'].append(float(x[1]))
    else:
        print "Not a valid dataline", x

def plot_graph(data, filename):

    fig = plt.figure()
    ax = fig.add_subplot(111)

    n, bins, patches = ax.hist(np.array(data), bins=13, align='mid', facecolor='#888888')

    ax.set_xlabel('Speed in kph')
    ax.set_ylabel('Amount of Females')

    ax.set_xlim(min(data, max(data)))

    # plt.savefig(filename)
    plt.show()           

plot_graph(question1['males'], "ASS1Q1-males.eps")
#plot_graph(question1['females'], "ASSQ2-females.eps")
print summary(question1['males'])
print summary(question1['females'])

Can someone explain why this is happening? what am I doing wrong?

A: 

Try removing

import matplotlib
matplotlib.use('Agg')

The command

python -c 'import matplotlib; matplotlib.use("")'

will show you the valid string arguments that can be sent to matplotlib.use. On my machine, 'Agg' is listed as valid, though I get no output when this is set. If you are curious, you could just keep trying various options until you find one that works.

When you find the one that your prefer, you may also find it more convenient to set something like

backend      : GtkAgg

in your ~/.matplotlib/matplotlibrc instead of using matplotlib.use(...).

unutbu
thanks I'll try that now
Daniel Hill
oh I found the problem line 36, I have the brackets all wrong :/, don't know how that happened, should have explained my self better as It was drawing a white space for a graph instead
Daniel Hill
@Daniel, very glad to hear you've found the problem, even though my suggestion was off in the wrong direction. :)
unutbu
well thanks for your help anyway :)
Daniel Hill