Use gnuplot in LINUX to visualize your data (2)

Source: Internet
Author: User
Article Title: Using gnuplot in LINUX to visualize your data (2 ). 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.
   Figure 7. Multiplot plot
    
   Data graph
Although this tutorial is intended only for illustration purposes, most of the content is focused on drawing a sine curve, and you may be tempted to plot the test data, sales data, or other similar data. In this section, we will use IBM's stock prices as a data set to illustrate how to draw different graphs (the raw data in this example is included in the references section ):
  
Table 1. IBM stock prices
    
Most data sets are numeric columns, but these data sets are different because the x axis is time data. The following code tells gnuplot how to read and format time data on the x axis (for more information, see help time/data and help set timefmt ):
  
Listing 8. Set time series data
  
Set xdata time # The x axis data is time
Set timefmt "% d-% B-% y" # The dates in the file look like 10-Jun-04
Set format x "% B % d" # On the x-axis, we want tics like Jun 10
  
Once the settings are complete, you can use the following command to open a drawing. We choose to use linear interpolation between different open regions and use the linespoints style, not just points:
  
Plot ["31-May-04": "11-Jun-04"] 'ibm. dat 'using with linespoints
  
   Figure 8. Open a chart
    
As usual, ["31-May-04": "11-Jun-04"] explicitly sets xrange, but not completely necessary. Note that the min and max values must be specified as strings that match timefmt.
  
Depending on the drawing style used, gnuplot can use up to six different numbers when drawing a point. When a record is read, the column number starts from 1. Using tells the plot command to use the first column as the x axis and the second column as the y axis. So to draw the closing price chart, we will use using.
  
Each time the gnuplot sees a new data separator, the column number will increase. By default, the data Delimiter is a space. Therefore, if the time format contains spaces (for example, 10 Jun 04, instead of 10-Jun-04), we will use using instead. For the x axis, we only need to specify the first column where data starts. Gnuplot can intelligently use the timefmt string to read other columns.
  
Using allows you to add more than two colons to specify any other columns that may be required for a specific drawing style. You can also use $ n to represent column n to plot binary expressions. In a completely fictitious example, we assume that different prices represent different test operations, and we need to draw average, maximum, and lowest prices. The yerrorbars style allows you to draw y-high/y-low values (we specify x: y: ylow: yhigh in the using modifier) and calculate the average value of the four prices of the y axis ($2 + $3 + $4 + $5)/4:
  
Plot ["31-May-04": "11-Jun-04"] 'ibm. dat 'using 1 :( $2 + $3 + $4 + $5 \
Title 'daily prices, IBM 'with yerrorbars
  
   Figure 9. Calculate the average value
    
The errorlines style is similar, but it also draws a line segment between the drawn y-axis points. For creating a financial bar chart, Gnuplot also has a separate style, with the highest price, lowest price, and tics on the left, and the opening and closing prices on the right. We use set bars to display the opening and closing prices ):
  
Set bars 5
Plot ["31-May-04": "11-Jun-04"] 'ibm. dat 'using 1: 2: 3: 4: 5 with financebars
  
   Figure 10. Drawing a financial bar chart
    
You can also view help plow.styles to obtain other related styles, such as candlesticks, boxerrorbars, and incorrect drawing styles.
  
   Process real data sets
Of course, things may become more complex for real data sets. The most common is that data points are not separated by spaces in a data set. Gnuplot 4.0 provides new functions. You can use set datafile separator Specify your own separator. Similarly, you can use set datafile commentschar to specify comments in addition. If you need more control, you can specify the format string for the using modifier. Gnuplot uses the scanf function of the C standard library and a given format string (for more information, see help using) to read data. I will describe this method only to make the content more comprehensive. The data set that can be read in this method can also be processed using set data file separator, you can also explicitly specify the column numbers to be used in the using modifier. For example, if you want to ignore the string in the first column, use for plotting. To get more flexibility, you can use the Text Processing utility to pre-process files in Unix systems with popen functions before passing files to the plot command. For example: plot "<awk -- f preprocess. awk data. file ". Unfortunately, this command cannot be used in all systems. The last method is to pre-process the input file in gnuplot.
  
Sometimes, real data may not contain all vertices-for columns to be drawn, there may be x values, but the corresponding y values are missing. Gnuplot allows you to specify strings to indicate missing data points. For example:
  
Set datafile missing 'nan '# The IEEE floating point not-a-number
  
Note that before version 4.0, the command is set missing .
  
Unfortunately, the method for processing missing data points depends on the using description. For details about how to use plot 'file', plot 'file' using, and plot 'file' using ($1) :( $2), see help using. For the sake of security, the explicit using format is usually used for specifying columns. If you want to completely clear unsuitable data points, do not evaluate the expression (the third option evaluates the expression because of parentheses) -- instead, pre-process the data file and add the exact column with the result of the calculated expression.
  
Normally, you may end with invalid data points. If gnuplot is told to ignore invalid data points, you can still plot such a data set. Gnuplot ignores undefined points, so that when you encounter an inappropriate point, the trick is to use undefined y values, such as 1/0. A ternary operator similar to C can easily express this information (if you are not familiar with this function, see help ternary; it is also useful when defining a piecewise function ). The following is an example in help using, which allows you to plot with the second column as the y value, unless the third column is greater than 10:
  
Plot 'file' using 1 :( $3> 10? $2: 1/0)
  
   Advanced example
Although this article shows you most of the frequently used commands, you may need to study them further in some places. In this section, we briefly discuss some of these concepts and then provide a gnuplot demo file (electron. dem), which is used to draw a common amplitude frequency response diagram in electronics to describe their usage.
  
   User-Defined Functions and variables
You can define your own complex functions as follows: A (jw ). These functions can be expressed by parameters (p1, p2). You can use them in the plot command later to define parameter variables.
Plural
Gnuplot can recognize the plural. {A, B} indicates a + bi. You can use the abs () and arg () functions to obtain the absolute value and the spoke angle.
Virtual variable
When sin (x) is used in the plot command, gnuplot uses x as a virtual variable and assigns different values to it for example. When writing complex user-defined functions, you can use the traditional variable names used in the function definition, or explicitly set the name of the virtual variable to be used (for example, set dummy jw ).
X2 and y2 Axes
In addition to the common x and y axes along the bottom and left of the graph, you can also use the top (x2) and right (y2) of the graph as separate axes. These axes are independent of each other and can be set separately for range, tics, label, and ratio (for example, logarithm scale ). In addition, by using the axes clause, the plot command can use different axis sets, such as x2y2. By default, the plot command draws on the x and y axes. This advanced feature is helpful when you need to draw lines of different magnitude or units at the same time. In the following example, the y axis draws the amplitude, the maximum value is 1, and the y2 axis draws the phase, the maximum value is 100. If you plot the same set of axes, the amplitude curve is almost invisible.
Logarithm Scale
You can use the set logscale command to draw any axis on a logarithm scale. This command draws the base-10 logarithm of the value on the axis. You can explicitly set the base number (for example, set logscale 2, or set logscale xy 2 if it is set only for x and y ).
The following example is taken from the demo file electron. dem provided with gnuplot. This script uses all the advanced concepts discussed above:
  
List 9. Response frequency
  
A (jw) = ({0, 1} * jw/({0, 1} * jw + p1) * (1/(1 + {0, 1} * jw/p2 ))
P1 = 10
P2 = 10000
Set dummy jw
Set grid x y2
Set key default
Set logscale xy
Set log x2
Unset log y2
Set title "amplency and Phase Frequency Response"
Set xlabel "jw (radians )"
Set xrange [1.1: 90000.0]
Set x2range [1.1: 90000.0]
Set ylabel "magncategory of A (jw )"
Set y2label "Phase of A (jw) (degrees )"
Set ytics nomirror
Set y2tics
Set tics out
Set autoscale y
Set autoscale y2
Plot abs (A (jw), 180/pi * arg (A (jw) axes x2y2
  
   Figure 15. Amplitude and Phase Frequency Response
  
   Conclusion
In this article, we discuss the complexity of using the newly released gnuplot 4.0 to draw 2D images. Although we have come into contact with most important aspects of using gunplot, we still have a few topics that are not discussed due to limited space. It is worth noting that some omitted scopes include drawing from very simple parameter functions (see help parametric), polar coordinate (help polar), and curve fitting (which enables users to define curves and

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.