Using MATLAB, Microsoft Excel, Gnuplot, IDL, and XMGrace for Plotting
Thee Example Plots in Five Packages
- A brief introduction.
- Description of Example Plots
- Description of the three example plots generated using each of the five packages.
- Plotting with MATLAB
- Working with MATLAB and examples.
- Plotting with Microsoft Excel
- Working with Excel and examples.
- Plotting with Gnuplot
- Working with Gnuplot and examples.
- Plotting with IDL
- Working with IDL and examples.
- Plotting with XMGrace
- Working with XMGrace and examples.
We present an introduction to five packages used for generating plots (line plots, scatter plots, bar charts, etc.) and provide a set of three example plots as plotted in each of the packages.
This tutorial is based on work developed by SCV staff members Aaron Fuegi, Robert Putnam, Kadin Tseng, and Ray Gasser for the October 2008 Workshop on Scientific Visualization section on Plotting Packages. It has here been expanded upon somewhat, including work by Erik Brisson on the IDL section.
Presented here are five widely used plotting packages. Three (MATLAB, Microsoft Excel, and IDL) are commercial while the other two (Gnuplot and XMGrace) are public domain packages. There are a mix of command line (MATLAB, Gnuplot, and IDL) and graphical interfaces (Microsoft Excel) to using the packages and XMGrace offers both to a degree.
We are not making any explicit recommendations regarding which package you should use since it very much depends on what kinds of things you are trying to plot and what sort of an interface you are most comfortable using. However, most users at least in our user community at Boston University will probably end up using either MATLAB or Microsoft Excel as these are the most widely available on the BU campus. Also, one or the other will probably feel like the most comfortable package to use for the majority of users.
A set of three fairly simple example plots are shown below (with the code [for command line packages] or a description of how to recreate them [for GUI-based packages]) to illustrate the use of and output for each of the packages. These also demonstrate to a degree the control which each package gives over a plot’s appearance. The three examples are all part of a demo released with Gnuplot called layout.dem. In the other four packages, we have worked to duplicate these three example plots.
The first example (Plot1) is a line plot of the mathematical function sin(x)/x. It therefore does not have an associated data file since most of the packages can directly call this function. The second example (Plot2) is a scatter plot of the first and second columns of the input file silver.dat plotted as XY coordinates and with a symbol (a red + sign) shown at each point. The third example (Plot3) is a stacked bar chart of the 2nd, 7th, 8th, and 11th columns of the data file immigration.dat, giving the number of people who immigrated to the United States by decade (during the period 1891-1970) for a number of countries. For some of the packages, the input data files have been slightly modified for easier data entry.
- Commercial package for Linux, Windows, and Mac – (Availability at Boston University)
- Command line + menu interface
- Good output quality
- Good control of plot appearance (with some effort)
- Excellent online documentation
We also have a general tutorial specifically on MATLAB which you may be interested in as it will give you a more in depth introduction to the capabilities of this package.
The code to generate the three example plots (all in one window) is shown below along with the Figure generated by it. The code is also available as a downloadable file (threeplots.m) and the input files referenced are here (for Plot2) and here (for Plot3).
function threeplots(layout) % function threepltos(layout) % layout = 'v' results in the 3 plots arranged top to bottom % = 'h' results in the 3 plots arranged side by side if layout == 'v' n = 3; m = 1; else n = 1; m = 3; end uicontrol('Style', 'text', 'String', 'Multiplot layout 3, 1', ... 'HorizontalAlignment', 'center', 'Units', 'normalized', ... 'Position', [0 .95 1 .05], 'BackgroundColor', [.8 .8 .8]); subplot(n,m,1) hold on title('Plot 1') x = [-10:0.099:-0.1,0.1:0.099:10]; plot(x,sin(x)./x,'r-') legend('sin(x)/x') subplot(n,m,2) hold on title('Plot 2') load silver_mod.dat plot(silver_mod(:,2),silver_mod(:,3),'r+') legend('silver.dat') subplot(n,m,3) hold on title('Plot 3') load immigration_mod.dat; Y = [immigration_mod(:,4),immigration_mod(:,9), ... immigration_mod(:,10),immigration_mod(:,13)]'; hb = bar(Y,'stacked'); set(gca,'xtick',[1:4], 'xticklabel', ['Austria'; 'France '; ... 'Germany';'Italy '],'Ytick',) set(hb,'linestyle','none') end
- Commercial package for Windows and Mac machines
- Data in connected Excel spreadsheetWhat this means is that if you wish to do a plot like Plot1 (of sin(x)/x), you first need to generate the appropriate data set in your Excel spreadsheet.
- GUI InterfaceClick on item you want to modify (axis, gridlines, chart size, etc.), right-click to bring up context-sensitive menu and choose appropriate option, and adjust parameters.
- High quality output but can be tricky to control
- Cut & Paste – easy interaction with other Windows/Mac OS programsPlots can be pasted into other applications. Pasted into other Microsofot Office applications (such as Word), the Excel document will actually remain an editable plot, and not be an uneditable image.
Because Excel is a GUI-based package, all of the plotting is done via mouse commands and thus there is no explicit ‘script’ or ‘code’ which can be saved to let you exactly replicate the plots below. Plots are generated by first loading a file or generating from a mathematical function a dataset in an Excel spreadsheet. Once you have that, you then select the rows and columns of the spreadsheet which you wish to show and a style of plot (line, scatter, bar, etc.) to use. The generated plot is then modifed by clicking on elements of it and adjusting them (generally done by right-clicking over a selected element of the plot and choosing an action from a drop-down menu such as “Format Axis…” and then appropriately editing the Axis settings and parameters). There are a good variety of plot types and excellent quality of output, but it can be tricky to control, particularly if you are used to more of a command-line style. Each of the three plots below is invididually generated – there is no way to in a single window show multiple totally different plots.
- Free, widely available for Linux, Windows, and Mac
- Command line interface
- Fair output quality
- Fair control of plot appearance
- Interfaces to C, Fortran, Perl, Python
- Extensive documentation and examples easily available on the web (or included with the package)
As mentioned above, this set of examples is actually a built in to Gnuplot’s comprehensive demo script. It is contained in layout.dem and the code for this set of three plots is shown below. For the other three packages shown, we worked to duplicate these same demo plots.
# # Gnuplot version 4.1 demo of multiplot # auto-layout capability # # set multiplot layout 3, 1 title "Multiplot layout 3, 1" set title "Plot 1" plot sin(x)/x # set title "Plot 2" plot 'silver.dat' using 1:2 ti 'silver.dat' # set style histogram columns set style fill solid set key autotitle column set boxwidth 0.8 set format y " " set tics scale 0 set title "Plot 3" plot 'immigration.dat' using 2 with histograms, '' using 7 with histograms , '' using 8 with histograms , '' using 11 with histograms # unset multiplot # # #
- Commercial package available for Linux, Windows, and Mac
- Command line interface
- Very good output quality
- Good control of plot appearance but sometimes fairly involved to achieve
- General purpose scientific computing package, by no means just for plots
As with MATLAB, we have a general tutorial specifically on IDL which can give you a more in depth introduction to the capabilities of this package.
Although it is possible in IDL to show multiple plots in a single window, we have not done so in this example. Click on the plots below to view the full-sized version of each. The code to create these plots is available here, here, and here respectively and the input files referenced are here (for Plot2) and here (for Plot3).
- Freely available for Linux and Windows
- GUI + command line interface
- Good output quality
- Great control over plot appearance
- Can be called from C/Fortran programs
- User’s guide, tutorial, and examples included in package
- Caveats: quirky user interface and non-intuitive workflow; steep learning curve
As with Excel, XMGrace is a GUI-driven package. Also, as with Excel, each of the plots below was generated separately and not as a group as can be and was done with MATLAB and Gnuplot. Click on the plots to view the full-sized version of each. XMGrace does have the ability to save out a text file description of each plot which can then be reloaded into the package. The saved files to generate the plots below are Plot1, Plot2, and Plot3 and a description of how to use the GUI to create the first two plots is here.