This help page is ahow-to guide. It explains concepts or processes used by the Wikipedia community. It is not one ofWikipedia's policies or guidelines, and may reflect varying levels ofconsensus. |
| Parts of this Wikipedia page (those related to including information about native vega support via the graph extension) need to beupdated. Please help update this Wikipedia page to reflect recent events or newly available information. Relevant discussion may be found on the talk page.(January 2015) |
Charts, graphs, and other pictures can contribute substantially to an article. Here are some hints on how to create a graph. The source code for each of the example images on this page can be accessed by clicking the image to go to the image description page.
These should be followed whenever possible.
See alsoHelp:Pictures on how to include them in articles.
Edward Tufte, in his bookThe Visual Display of Quantitative Information, distinguishes betweenfriendly andunfriendly data graphics as follows:
Many of the graphs on Wikipedia were made with thefree software programgnuplot. It can be used by itself or in conjunction with other software.
For example, to plot the data in file "data":
setxlabel"steps"setylabel"result"unsetkey#use bars in plot: with boxes#choose line color/style in plot: linetype n#plot filled bars (fs): pattern nsetstylefillpattern2plot"data"withboxeslinetype3fs
There is additional discussion of plotting with gnuplot onTemplate talk:Probability distribution § Standard Plots.
Now that MediaWiki supports SVG, it's usually best to generate SVG images directly. SVG images have many advantages, like being fully resizable, easier to modify, and so on, though they are sometimes inferior to raster images. Decide on a case-by-case basis.
A typical plot file could start with:
setterminalsvgenhancedsize10001000fname"Times"fsize36setoutput"filename.svg"

Gnuplot can also generate raster images (PNG):
For the best results, aPostScript file should be generated and converted into PNG in an external program, like theGIMP. PostScript is generated with the lineset terminal postscript enhanced:
setterminalpostscriptenhancedcolorsolidlw2"Times-Roman"20setoutput"filename.ps"
You should use a large number of samples for high-quality plots:
setsamples1001
This is important to preventaliasing or jaggedlinear interpolation (seeFile:Exponentialchirp.png andits history for an example of aliasing). Labels are helpful, but remember to keep language-specific information in the caption if it's not too inconvenient. Including the source code and/or an image without text helps other users create versions in their own language, if text is included in the image.
setxlabel"Time (s)"setylabel"Amplitude"
The legend or key is positioned according to the coordinate system you used for the graph itself:
setkey4,0
Most other options are not Wikipedia-graph-specific, and should be gleaned from documentation or the source code included with other plots. An example of a plot generated with gnuplot is shown on the right, with source code on the image description page.[1]

Maxima is acomputer algebra system licensed under theGPL, similar toMathematica orMaple. It uses gnuplot as its default plotter, though others are available, such asopenmath. Plotting directly to PostScript from Maxima is supported, but gnuplot's PostScript output is more powerful.
The most-used commands areplot2d andplot3d:
plot2d(sin(x),[x,0,2*%pi],[nticks,500]);plot3d(x^2-y^2,[x,-2,2],[y,-2,2],[grid,12,12]);
Since the plot is sent to gnuplot as a series of samples, not as a function, the Maximanticks option is used to set the number of sampling points instead of gnuplot'sset samples. Additionalplot options are included in brackets inside the plot command. To use the same options as in the above gnuplot example, add these lines to the end of the plot command:
PostScript output:
[gnuplot_term, ps][gnuplot_ps_term_command, "settermpostscriptenhancedcolorsolidlw2'Times-Roman'20"]SVG output:
[gnuplot_term, ps][gnuplot_ps_term_command, "setterminalsvgenhancedsize10001000fname'Times'fsize36"]Output filename:
[gnuplot_out_file, "filename.ps"]
Additional gnuplot commands:
[gnuplot_preamble, "setxlabel'Time (s)';setylabel'Amplitude';setkey4,0"]Like so:
plot2d(sin(x),[x,0,2*%pi],[nticks,500],[gnuplot_term,ps],[gnuplot_ps_term_command,"set term postscript enhanced color solid lw 2 'Times-Roman' 20"],[gnuplot_out_file,"filename.ps"],[gnuplot_preamble,"set xlabel 'Time (s)'; set ylabel 'Amplitude'; set key 4,0"]);
Similar for svg output:
plot2d(sin(x),[x,0,2*%pi],[nticks,500],[gnuplot_term,ps],[gnuplot_ps_term_command,"set terminal svg enhanced size 1000 1000 fname 'Times' fsize 36"],[gnuplot_out_file,"filename.svg"]);
Note that the font and labels are in single quotes now, nested inside double quotes. Multiple commands are separated by semicolons.
An example of a plot generated with gnuplot in Maxima is shown on the right, with source code on the image description page.[2]
GNU Octave is anumerical computation program; effectively aMATLAB clone. It usesgnuplot extensively (though also offers interfaces toGrace and other graphing software).
The commands areplot (2D) andsplot (surface plot).

t=[0:.01:1];y=sin(2*pi*t);plot(t,y,"linewidth",2)xlabel("Time (s)");ylabel("Amplitude");print("filename.svg","-color","-solid","-tight","-FTimes-Roman:20")
Matplotlib is a plotting package for the free programming languagePython. Itspyplot interface is procedural and modeled afterMATLAB, while the full Matplotlib interface is object-oriented.
Python and Matplotlib are cross-platform, and are therefore available for Windows, OS X, and the Unix-like operating systems like Linux and FreeBSD.
Matplotlib can create plots in a variety of output formats, such as PNG and SVG. Matplotlib mainly does 2-D plots (such as line, contour, bar, scatter, etc.), but 3-D functionality is also available.

Here is a minimal line plot (output image is shown on the right):
importmatplotlib.pyplotaspltimportnumpyasnpa=np.linspace(0,8,501)b=np.exp(-a)plt.plot(a,b)plt.savefig("Matplotlib3 lineplot.svg")plt.show()# show plot in GUI (optional)
Save this script as e.g.lineplot.py and then run it withpython lineplot.py. After a few seconds, a window with the interactive graphical output should pop up, and the SVG will also be in the folder.
Numerous examples with Python source code are available, for examplethe Matplotlib gallery andcommons:Category:Valid SVG created with Matplotlib code.

Wikimedia SVG Chart is a graph generator using the templates functionality of Wikimedia Commons. This template generates line and point charts in a structured and readable svg format. The original values are provided unmodified for the SVG file. Therefore the data of the chart may be checked and added at any time directly in the native file with any text editor.
Instructions for a simple line plot:
{{SVG Chart| Title= Demo| XAxisText= foo| YAxisText= bar| LegendType= none| XMax= 60| YMax= 40| XAxisMarkStep= 10| YAxisMarkStep= 10| Graph1Values= 0 30 20 10 40 40 60 30| Graph2Values= 0 0 20 30 40 10 60 30}}
Xfig is an open source vector graphics editor that runs underX on most Unix platforms. In xfig, figures may be drawn using objects such as circles, boxes, lines, spline curves, text, etc. It is possible to import images in many formats, such as GIF, JPEG, SVG, and EPSF. An advantage of Xfig consists in its ability to display nice mathematical formula in the labels and legends using theTeX language.

The free statistical package R (seeR programming language) can make a wide variety of nice-looking graphics. It is especially effective to display statistical data. OnWikimedia Commons, the categoryCreated with R contains many examples, often including the corresponding R source code. Other examples can be found in theR Graph Gallery.
In order to output postscript, use “postscript” command:
postscript(file="myplot.ps")plot(...)graphics.off()
The last command will close the postscript file; it won't be ready until it's closed.
With an additional (free) package, it's also possible to generate SVG-graphs with R directly. See an example with code onImage:Circle area Monte Carlo integration2.svg.
Other packages (lattice, ggplot2) provide alternative graphics facilities or syntax.
Here is another example with data.
TheGri graphical language can be used to generate plots and figures using a script-like commands. Unlike other tools Gri is not point and click, and requires learning the Gri script syntax.
Maple is a popular proprietarycomputer algebra system. Maple can export graphs inEncapsulated PostScript format, which can then be converted to SVG for example in Inkscape. To do this using the standard GUI interface, follow these steps:

GeoGebra is adynamic geometry program that can be used to create geometric objects free-hand usingcompass-and-ruler tools. It can also be used to plot implicit curves, parametric curves, and loci of points. It supports SVG, PNG, EPS, PDF, EMF,PGF/TikZ and PSTricks as export formats and has support forLaTeX formulas within text objects.
GeoGebra is not a drawing tool,[3][4] and therefore suffers from some caveats that people accustomed to programs such as Inkscape or Illustrator might not be expecting.[5] However, if your requirements with regard to pixel-perfect results are not too stringent, then you can quickly and easily create graphs and diagrams in GeoGebra.
If you want to set the dimensions (in pixels) of the graphical output as close to exact as possible, you should start by referring to the instructions inthis discussion, and note the observations inthis discussion. I.e. the resulting image may still be off by a few pixels. Alternatively, you can export to SVG andfix the file using a text editor. Having done this, you can then use Inkscape to convert the SVG file to PNG or JPEG.

C.a.R. (standing for "compass and ruler") is very similar toGeoGebra in that both programs are free, point-and-click, dynamic geometry applications running under Java and supporting PNG, SVG and other output formats. It is not nearly as feature-rich as GeoGebra, but at the same time overcomes some of GeoGebra's limitations with respect to vector and raster image export.[6]
POV-Ray is a free general-purposeconstructive solid geometry ray-tracing package with a scene description language very similar to many programming languages. It can also render parametric surfaces and algebraic surfaces of degree up to seven, as well as triangle mesh approximations using the "mesh" and "mesh2" object types and"param.inc". An updated version of the file"screen.inc" can be used to output the exact two-dimensional screen coordinates of any three-dimensional object so as to facilitate the addition of labels or other 2D elements in post-processing, for instance in Inkscape.
Other usable tools include:
These tools are only capable of producing raster output.
For graph-theory diagrams and other "circles-and-arrows" pictures,Graphviz is quick and easy, and also able to make SVGs.
Next to being useful for post-processing (see the next section),Inkscape is a point-and-click tool that can be used to create high-quality figures. It is a particularly easy tool for creatingvector graphics, thoughGeoGebra andC.a.R. may be better suited for mathematical graphics. Also, Inkscape's design concepts differ in some fundamental ways from SVG. For instance, in SVG element widths are appliedbefore stroke widths, whereas in Inkscape they are applied after.
LibreOffice andApache OpenOffice are two free office suites (both forked from the now-discontinuedOpenOffice.org suite) that contain among other things means of creating line, bar and pie charts based on data contained in spreadsheets and databases, as well as a program for drawing vector graphics calledDraw. There is also a plugin forimporting SVG images into OpenOffice.org and SVG import and export is included in current LibreOffice Draw by default. As of 2010, support for the full range of options offered by Inkscape and many other vector formats was still preliminary at best.[7][8]

Gnumeric is a fairly lightweight spreadsheet and charting application, part of theGNOME Free Software Desktop Project. It is available for Linux and other Unix-like systems, as well NT-based versions of Windows. Charts are generated by the usual method of selecting a data range and clicking a toolbar icon. The approach is to start minimally, but double clicking the chart opens a tabbed dialogue, giving a high level of control over all elements of the chart, which are arranged in a hierarchical, nested structure. New elements can be added by clicking on the appropriate level and theAdd button. The most important feature from the point of view of Wiki charts, is that Gnumeric charts can be exported as graphics simply by right clicking anywhere on the finished chart and selectingSave as Image. A range of formats is supported, including SVG and PNG. The accompanying population chart is a typical result.
SVG images can be post-processed inInkscape. Line styles and colors can be changed with theFill and Stroke tool. Objects can be moved in front of other objects with theObject→Raise andLower menu commands.
Saving from Inkscape also adds information that isn't present in Gnuplot's default output – neither Firefox nor Mozilla will render the file natively without it. These browsers can be persuaded to render Gnuplot's SVG output if the<svg> tag has the following attribute:xmlns="http://www.w3.org/2000/svg", as described at theMozilla FAQ.
pstoedit -f plot-svg Picture.ps Picture.svgDirect SVG output is probably better if the program supports it. SeeWikipedia:WikiProject Electronics/How to draw SVG circuits using Xcircuit for an example.
Setting colors and linestyles in gnuplot is not easy. They can more easily be changed after the PostScript file is generated by editing the PostScript file itself in a regulartext editor.
This avoids needing to open in proprietary software, and really isn't that difficult (especially if you are unfamiliar with other PS editing software).
Find the section of the .ps file with several lines starting with /LT. Identify the lines easily by their color ("the arrow is currently magenta and I want it to be black. Ah, there is the entry with 1 0 1, red + blue = magenta") or by using the gnuplot linestyle−1 (for instance, gnuplot's linestyle 3 corresponds to the ps file's /LT2). Then you can edit the colors and dashes by hand.
/LT0{PL[]100DL}def
/LT0 corresponds to gnuplot's linestyle 1. The[] represents a solid line.1 0 0 is the color of the line; anRGB triplet with values from 0 to 1. This line is red.
/LT2{PL[2dl3dl]001DL}def
/LT2 corresponds to gnuplot's linestyle 3. The[2 dl 3 dl] represents a dashed line. There are 2 units of line followed by 3 units of empty space, and so on.0 0 1 represents the color blue.
/LT5{PL[5dl2dl1dl2dl]0.50.50.5DL}def
/LT5 corresponds to gnuplot's linestyle 6. The[5 dl 2 dl 1 dl 2 dl] represents a dash-dot line. There are 5 units of line (the dash) followed by 2 units of empty space, 1 unit of line (the dot), 2 more units of empty space, and then it starts over again.0.5 0.5 0.5 represents the color gray.
/LTb is the graph's border, and /LTa is for the zero axes.[9]
To post-processPostScript files for raster output (though vector is preferred):[10]
Another route to convert a PS or EPS file (postscript) in PNG is to useImageMagick, available on manyoperating systems. A single command is needed:
convert -density 300 file.ps file.png
The density parameter is the output resolution, expressed indots per inch. With the standard 5x3.5in size of a gnuplot graph, this results in a 1500x1050 pixels PNG image. ImageMagick automatically applies antialiasing, so no post-processing is needed, making this technique especially suited to batch processing. The followingMakefile automatically compiles all gnuplot files in a directory to EPS figures, converts them to PNG and then clears the intermediate EPS files. It assumes that all gnuplot files have a ".plt" extension and that they produce an EPS file with the same name, and the ".eps" extension:
GNUPLOT_FILES=$(wildcard*.plt)# create the target file list by substituting the extensions of the plt filesFICHIERS_PNG=$(patsubst%.plt,%.png,$(GNUPLOT_FILES))all:$(FICHIERS_PNG)%.eps:%.plt@echo"compillation of "$<@gnuplot$<%.png:%.eps@echo"conversion in png format"@convert-density300$<$*.png@echo"end"