Matplotlib is great! I created all my figures in this paper [0] using 
matplotlib. I am impressed with the quality you get. I attach the plotting 
script for fig. 9, which does exactly what you want, I believe. There is a lot 
of other junk in there, so look for the "legend" function, it all happens in 
one line.

Cheers,
Paul.

[0] http://arxiv.org/abs/1011.3399, the paper should appear in phys. rev. B 
soon.

#! /usr/bin/env python
"""
Generate a plot of dipole moments as function of frequency for different 
substrates.
"""

import sys
import numpy
import matplotlib
matplotlib.use('pdf')
from matplotlib import pyplot

def setparams():
    'Adjust the rcparams before plotting.'
    matplotlib.rcParams['figure.figsize'] = [3.0, 5.0]
    #matplotlib.rcParams['legend.borderpad'] = 0.0
    matplotlib.rcParams['figure.subplot.left'] = 0.12
    matplotlib.rcParams['figure.subplot.right'] = 0.96
    matplotlib.rcParams['figure.subplot.bottom'] = 0.08
    matplotlib.rcParams['figure.subplot.top'] = 0.94
    matplotlib.rcParams['legend.fontsize'] = 10.0
    matplotlib.rcParams['font.size'] = 10.0

def readfiles():
    #eps_1 = numpy.loadtxt('Drude-dimer-on-vacuum.dat')
    #eps_2 = numpy.loadtxt('Drude-dimer-on-2.25.dat')
    #eps_3 = numpy.loadtxt('Drude-dimer-on-3.0.dat')
    #eps_5 = numpy.loadtxt('Drude-dimer-on-5.0.dat')
    #eps_10 = numpy.loadtxt('Drude-dimer-on-10.0.dat')
    #eps_20 = numpy.loadtxt('Drude-dimer-on-20.0.dat')
    filenames = ['dipole_1_pos-0000_eps_01.dat',
            'dipole_1_pos-0000_eps_02.dat',
            'dipole_1_pos-0000_eps_10.dat']
    eps_1, eps_2, eps_10 = map(numpy.loadtxt, filenames)
    freqs = eps_1[:, 0]
    #return [freqs, eps_1[:, 1:], eps_2[:, 1:], eps_3[:, 1:], eps_5[:, 1:],
            #eps_10[:, 1:], eps_20[:, 1:]]
    return [freqs, eps_1[:, 1:], eps_2[:, 1:], eps_10[:, 1:]]

def main():
    setparams()

    tmp = readfiles()
    freqs = tmp[0]
    ps = tmp[1:]

    #fig = pyplot.figure()
    pyplot.subplots_adjust(hspace=0.0001)
    axes = []
    for i, p in enumerate(ps):
        ax = pyplot.subplot(310 + (1 + i))
        for j in range(3):
            labeltext = r'$\bm{E}_{0}\parallel\bm{\hat{%s}}$'
            style = ['b-', 'g--', 'r:'][j]
            plot = ax.plot(freqs, p[:, j], style, label=labeltext % 'xyz'[j])
        ax.set_xlim(freqs[0], freqs[-1])
        ax.set_ylim(0, 10)
        ax.set_yticks(numpy.arange(1, 9 + 1, 2))
        ax.text(1.55, 8.0, r'$\mathrm{%s} \,\,\, \varepsilon_- = %.1f$' %
                (['(a)', '(b)', '(c)'][i], [1.0, 2.0, 10.0][i]))
        if i == 1:
            pyplot.ylabel(r'$\bar{p}(\omega)$')
        axes.append(ax)
    
    legend = axes[0].legend(loc=(-0.17, 1.03), ncol=3)
    legend.draw_frame(False)
    for ax in axes[:-1]:
        pyplot.setp(ax.get_xticklabels(), visible=False)
    pyplot.xlabel('$\hbar \omega \,\mathrm{[eV]}$')
    pyplot.savefig('Ag-dimer-dipole')

if __name__ == '__main__':
    main()

On 11. feb. 2011, at 00.20, Jeff Layton wrote:

>  Good evening,
> 
> I've been trying to find a way to move the legend outside
> the plot so it doesn't cover it up. I've seen some things
> online but I can quite get them to work (probably just my
> lack of knowledge about matplotlib).
> 
> The section of code creating the plot looks like,
> 
>       fig = plt.figure()
>       ax = fig.add_subplot(1,1,1);
> 
>       p1 = ax.bar(ind, IO_Time_Plot, width, color="r", align='center');
>       p2 = ax.bar(ind, Diff_Plot, width, color="y", 
> bottom=IO_Time_Plot, align='center');
> 
>       ax.set_ylabel('Time (secs)');
>       ax.set_title('Elapsed Time and IO Time',fontstyle='italic');
>       ax.set_xticks(ind);
> 
>       group_labels = [];
>       for item in names:
>          group_labels.append(item);
> 
>       ax.set_xticklabels(group_labels);
> 
>       fig.autofmt_xdate();
> 
>       #ax.legend( bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.);
>       ax.legend( (p1[0], p2[0]), ('IO Time', 'Total Elapsed Time'));
> 
> 
> 
> You can see my attempt at moving the legend outside the bounding box
> but when I try this, the legend never appears.
> 
> TIA!
> 
> Jeff
> 
> 
> 
> ------------------------------------------------------------------------------
> The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
> Pinpoint memory and threading errors before they happen.
> Find and fix more than 250 security defects in the development cycle.
> Locate bottlenecks in serial and parallel code that limit performance.
> http://p.sf.net/sfu/intel-dev2devfeb
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users

------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to