Use gnuplot in LINUX to visualize your data (1)

Source: Internet
Author: User
Tags in degrees corel draw
Article Title: Using gnuplot in LINUX to visualize your data (1 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
   Basic
Run gnuplot in the shell command prompt to start gnuplot. First, you can see the hint>. This prompt is the input point for entering gnuplot. Linux users will get used to this method. For example, you can use the arrow keys to search for previous command history records, and then edit and re-execute these commands. The Home and End keys are no different from the usual ones. You can recompile gnuplot to use the GNU readline library to move back and forth at the input prompt, but similar to the default function.
  
Gnuplot provides a wide range of online help. If you want to do anything useful, you will certainly use these online help. The syntax is unified: Enter help You can get help from any command. Start gnuplot and try the help set yrange and help set commands (after each command, exit the help with q ). Note that yrange is one of the available sub-options in help set. In general, the gnuplot help provides further help for all possible customization of commands. The examples in the browser help section usually show you how to use commands.
  
Gnuplot also has a lot of demos to demonstrate its capabilities, usually in the demo subdirectory installed. To obtain these capabilities, go to this directory at the gnuplot prompt (for example, cd'/opt/gnuplot/demo'-note that gnuplot requires that all file names and directory names be enclosed in single or double quotation marks), and then input 'all. dem '. The. dem files in this directory demonstrate various functions. all. dem loads them all at once. However, you may want to delay this operation until the end of this article, so that we can start to use gnuplot, no other operations...
  
For those who want to continue, you can enter each line of code in the code list at the prompt. Alternatively, save the entire list to a temporary file and run the code by entering the load 'filename' (do not forget the quotation marks) in the gnuplot prompt.
  
It is foreseeable that the 2D drawing command is plot. Enter plot sin (x) in the prompt: You should see the familiar sine curve in the pop-up window.
  
   Figure 1. sin (x)
    
We just created the simplest graph. Let's see how to customize the graph in different ways. Suppose we only want to see a sine curve period. We can do this by limiting the default x range of the graph. Use the notation [min: max] to specify the range. To specify only the minimum value, use [min:]; to specify only the maximum value, use [: max]. This is called the "closed" interval notation in mathematics.
  
Here we use [-pi: pi] to obtain a sine curve period:
  
Listing 1. sin (x) from-pi to + pi)
  
Set xrange [-pi: pi]
Replot
Reset
  
   Figure 2. redraw the sine curve
    
We just used the replot command to execute the previous plot command. This command is useful when you draw a graph and constantly modify it to add desired features. In addition, replot allows you to add more graphs. Try to input replot cos (x ). According to the syntax, this command is equivalent to plot sin (x), cos (x ). Replot is to get the previous drawing string, add necessary commas, and then attach the rest of the input to it.
  
The last reset command used is not necessary for drawing the sine curve. It removes the effect of all previous set commands and restores the default value. Therefore, in this example, it uses the default x range.
  
Now let's try something more strange: plot sin (x)/x. You should see the following figure:
  
   Figure 3. sin (x)/x
    
From the three simple examples, you can see that gnuplot can understand pi and has a rich vocabulary of built-in mathematical functions. It even knows useful statistical functions for managing normal distributions, as well as esoteric special functions such as longer, besell, beta, and gamma functions (and more !), These functions are usually available only in mathematica and other fully functional mathematical tools. Based on experience, the syntax in C is similar to that in C, and that in C is similar to that in general mathematics. (An important difference is the exponential representation: For convenience, the y power of x is written as x ** y ).
  
Next, name the Graph ("My First Graph") and name the axis (the x axis is "angle, in degrees", and the y axis is "sin (angle )").
  
Listing 2. Naming charts and axes
  
Set title "My first graph"
Set xlabel "Angle, \ n in degrees"
Set ylabel "sin (angle )"
Plot sin (x)
  
Note that \ n in xlabel will generate a new row. In general, gnuplot performs a backslash processing similar to C in the string in double quotation marks. Note for Windows users: If you want to use a string enclosed by double quotation marks for a file name, you must use two backslashes, for example, "c: \ developerworks" (or you can use a forward slash: "c:/developerworks ").
  
Now we notice that the x axis is not actually marked as a degree and does not look very good. To modify this issue, adjust the tic flag on the x axis so that the specified (master) flag only increases by 90 degrees and the secondary flag increases by 45 degrees. The "level" of the main tics is 0, which is the default value; the level of the secondary tics is 1. Each vertex is specified by a triple: "label" (placed in quotation marks ), And .
  
Listing 3. Modifying tic on the axis and setting the grid
  
Set xrange [-pi: pi] # we want only one cycle
Set xtics ("0" 0, "90" pi/2, "-90"-pi/2, "" pi/4 1, ""-pi/4 1, "" 3 * pi/4 1, ""-3 * pi/4 1)
Set grid
Set xlabel "Angle, \ n in degrees"
Set ylabel "sin (angle )"
Plot sin (x)
  
   Figure 4. sin (x) with an axis of tics, title, grid, and tag)
    
Note that an empty tag ("") is used for the + 45 degrees and-45 degrees secondary tags (""). Note that we do not need to list vertices in any order. Like all other items in gnuplot, xtics has many features and convenient options for creating tic tags in fixed increments (normal multiples in logarithm scales ), and the special format used for time-related data. View the examples in help set xtics for more information.
  
As an edge note, note that we use a backslash to extend the xtics command to multiple input lines. This makes it easier to understand long or complex commands.
  
We also use set grid to set the grid. This command allows you to view the figure from the number. So far, we have used a lot of set commands. Gnuplot has a unified method to unset settings: In gnuplot 4.0, the command is unset. For example, if you do not want a grid, use unset grid. In versions earlier than 4.0, the command is set nogrid.
  
By the way, all the examples we provide can run in interactive mode or batch mode. To run in interactive mode, enter each command in the prompt. To run in batch mode, enter the command or add it to a text file, and then use the load 'filename' command at the gnuplot prompt to read the file, or, at the shell prompt, provide it as a parameter to gnuplot: $ gnuplot filename. Using the second method, gnuplot exits after executing all the commands in the input file, so when drawing directly to the file (see the next section, the batch processing mode is particularly effective. If the second method is used for drawing on the screen, the output window disappears as soon as it appears (when gnuplot exits, therefore, you need to use the explicit "pause-1" command (see help pause) to keep the window.
  
The Gnuplot command file used in batch mode operations often remains for a while, helping to improve readability by using annotations. # The content after the end of the row is a comment. Therefore, in Example 3, gnuplot does not process we want only one cycle.
  
   Terminal devices -- digress
When starting, you may notice that the terminal type is set to X11. Gnuplot adopts a standard design and can be drawn on multiple terminal devices. This includes printing directly to multiple printers, including Epson, HP, and Imagen printers. It can even be drawn in pseudo devices, such as postscript and png. This mainly involves generating output files, rather than viewing files or printing output. This is a trick to include your chart in other reports. Although I have not found any differences between Windows and Linux, we recommend that you set the terminal type before setting the output file name:
  
Listing 4. Set the output and terminal types
  
Set terminal png # gnuplot recommends setting terminal before output
Set output "output.png" # The output filename; to be set after setting
# Terminal
Replot
  
Now, the file output.png contains the image you just created and can be included in a larger report. Many commonly used typographical and plotting programs have specialized terminals, including Adobe Illustrator (set term aifm), Corel Draw (set term corel), and AutoCad (set term dxf) terminals related to multiple LaTex (eepic, latex, pstricks, texdraw, and tpic ). You can also obtain PostScript, enhanced PostScript (eps), or Adobe PDF files. Your gnuplot version may not compile all the terminal drivers-for example, Windows users do not need Xll terminals, and Linux users do not need Windows terminals. Similarly, for legal reasons, gnuplot 4.0 removed support for gif terminals.
  
All terminals have different capabilities. Some of them (such as LaTeX) may not support text rotation, so if you set ylabel as we did before, it may be displayed differently on different terminals. In other words, you can use the LaTeX command on a specific LaTex terminal. For example, set ylabel "$ sin (\ theta) $ "(note that we use two backslash to generate a backslash required by LaTex -- before gnuplot transfers the string enclosed in double quotation marks to the terminal driver, first, perform backslash processing ). Now, you can use \ input {output. tex} to include the output file in the LaTeX file. To obtain the same results in the PostScript terminal, use the corresponding PostScript command: {/Symbol q }. In enhanced PostScript and LaTeX, you can use the notation x ^ {superscript} and x _ {subscript} to obtain the superscript. Also note the abbreviated Terminal
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.