8

I have been using the code below to plot the time spent to run 4 functions. The x axis represents the number of executions whereas the y axis represents the time spent running a function.

I was wondering if you could help me accomplish the following:

1) set the limits of the x axis so that only positive values are shown (x represents the number of times each function was executed and is, therefore, always positive)

2) create a legend for the 4 functions

Thank you,

Mark

import matplotlib
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.mlab as mlab


r = mlab.csv2rec('performance.csv')

fig = Figure(figsize=(9,6))

canvas = FigureCanvas(fig)

ax = fig.add_subplot(111)

ax.set_title("Function performance",fontsize=14)

ax.set_xlabel("code executions",fontsize=12)

ax.set_ylabel("time(s)",fontsize=12)

ax.grid(True,linestyle='-',color='0.75')

ax.scatter(r.run,r.function1,s=10,color='tomato');
ax.scatter(r.run,r.function2,s=10,color='violet');
ax.scatter(r.run,r.function3,s=10,color='blue');
ax.scatter(r.run,r.function4,s=10,color='green');

canvas.print_figure('performance.png',dpi=700)
5
  • 2
    What problem are you having when you try to do it yourself? Do you not know how to set the axis limits or to create a legend? (both questions are easily answered by searching the documentation) Have you tried it and gotten an error message?
    – David Z
    Sep 11, 2011 at 5:23
  • 1
    Hi David! Thanks for your message. I have been trying to set the x axis limit by using ax.xlim([0,150]) but that results in the following error message: AttributeError: 'AxesSubplot' object has no attribute 'xlim'. I am not sure if using scatter() would give me flexibility to work on the axes limits.
    – Mark
    Sep 11, 2011 at 15:17
  • As for legend, I used this: ax.scatter(r.run,r.time,s=10,color='tomato', marker='o',label='function1') but nothing appeared in the png file.
    – Mark
    Sep 11, 2011 at 15:29
  • 4
    @Mark - You want ax.set_xlim(..), rather than ax.xlim. Also, you don't need scatter for what you're doing in this particular case. plot would make more sense. Scatter is intended to vary marker colors and/or sizes by a 3rd and/or 4th variable. In your case, ax.plot(r.run, r.function1, 'o', color='whatever') would make more sense. Sep 11, 2011 at 16:27
  • 1
    You should thank him by marking his answer correct. ;)
    – Carl F.
    Sep 18, 2011 at 1:37

1 Answer 1

25

You need to call legend for the legend to appear. The label kwarg only sets the _label attribute on the artist object in question. It's there for convenience, so that the label in the legend can be clearly associated with the plotting command. It won't add the legend to the plot without explicitly calling ax.legend(...). Also, you want ax.set_xlim, not ax.xlim to adjust the xaxis limits. Have a look at ax.axis as well.

It sounds like you want something like this:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

# Generate some data
x = np.arange(0, 22, 2)
f1, f2, f3, f4 = np.cumsum(np.random.random((4, x.size)) - 0.5, axis=1)

# It's much more convenient to just use pyplot's factory functions...
fig, ax = plt.subplots()

ax.set_title("Function performance",fontsize=14)
ax.set_xlabel("code executions",fontsize=12)
ax.set_ylabel("time(s)",fontsize=12)
ax.grid(True,linestyle='-',color='0.75')

colors = ['tomato', 'violet', 'blue', 'green']
labels = ['Thing One', 'Thing Two', 'Thing Three', 'Thing Four']
for func, color, label in zip([f1, f2, f3, f4], colors, labels):
    ax.plot(x, func, 'o', color=color, markersize=10, label=label)

ax.legend(numpoints=1, loc='upper left')
ax.set_xlim([0, x.max() + 1])

fig.savefig('performance.png', dpi=100)

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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