Showing posts with label \psgrid. Show all posts
Showing posts with label \psgrid. Show all posts

Sunday, December 30, 2012

Pstricks custom graphic objects

Pstricks contains a large number of graphics object, but sometimes they are not enough, and you need something very special. This can be realized using command "\pscustom[prameters]{commands}".

Consider this problem. We would like to draw a ribbon like this:
Of course, properly using the line, polygon or curve commands, we can draw it. But I would say "\pscustom" will make the task very simple. Carefully examine the graph, we find that it is something enclosed by two sine curve. So can we draw such a graph object by simply plotting two sine curve? Let us have a try.

\documentclass{article}
\usepackage{pstricks}
\usepackage{pst-plot}
\begin{document}
  \begin{pspicture}*(-1,-1.5)(7,1.5)
  \psgrid
    \psset{xunit=0.015cm}
    \psplot{0}{360}{x sin 0.25 sub}
    \psline(360,0.25)(360,-0.25)
    \psplot{360}{0}{x sin 0.25 add}
    \psline(0,-0.25)(0,0.25)
  \end{pspicture}
\end{document} 

Fig.1 Custom graphic objects --- preparation

We have successfully draw two sine curves and two lines to join them togethre. The problem is how to fill the area enclosed by the two curve. This is where "\pscustom" steps in. Try the following codes (which is very simillar to the upper ones, except that the lines are enclosed in "\pscustom" command and fill parameters is set.)

\documentclass{article}
\usepackage{pstricks}
\usepackage{pst-plot}
\begin{document}
  \begin{pspicture}*(-1,-1.5)(7,1.5)
    \psset{xunit=0.015cm}
    \pscustom[fillstyle=solid,fillcolor=red]
    {
      \psplot{0}{360}{x sin 0.25 sub}
      \psline(360,0.25)(360,-0.25)
      \psplot{360}{0}{x sin 0.25 add}
      \psline(0,-0.25)(0,0.25)
    }
  \end{pspicture}
\end{document}

Fig.2 Custom graphic objects --- ribbon

And you get a graph just like what you asked to draw. Now we have successfully creat the ribbon object. You may have a try by using line or curve command to finish the same task, and compare which method is simpler.

"Practice makes perfect", one more example will be shown.

\documentclass{article}
\usepackage{pstricks}
\usepackage{pst-plot}
\begin{document}
  \begin{pspicture}*(-3.5,-3.5)(3.5,3.5)
    \pscustom[fillstyle=solid,fillcolor=red,
      linestyle=none]
    {
      \psplot{-1.414}{1.414}{2 x 2 exp sub}
      \psplot{-1.414}{1.414}{-2 x 2 exp add}
    }
    \psset{linewidth=2pt,linecolor=blue}
    \psplot{-3}{3}{2 x 2 exp sub}
    \psplot{-3}{3}{-2 x 2 exp add}
    \psset{linecolor=black}
    \psaxes{->}(0,0)(-3.5,-3.5)(3.5,3.5)
  \end{pspicture}
\end{document}

Fig.3 Custom graphic objects --- filled curves
Files Download: tex ps pdf

Tuesday, December 20, 2011

How to plot using pstricks(2) --- plot a data file

Function plotting have been talked last time, and today data file plotting will be introduced.

There are three fundamental data file plotting commands in pstricks, and we will show them one by one.

The first one is "\fileplot[parameters]{datafile}". The datafile should contain a list of coordinates. And the data can be delimited by "{}", "()", "," and one or more white space. If something is following "%", it will be interpreted as comments and will be omitted. It is advised to bracket all the data with "[]", this will significantly speed up the rate at which the data is read. So files like the following ones are all legal.

%mydata
[(0,0),(1,1),(2,2),(3,3),(4,4)] %mydata

[{0,0},{1,1},{2,2},{3,3},{4,4}]

[{0  0}  {1  1}  {2  2}  {3  3}  {4  4}]

[0  0  1  1  2  2  3  3  4  4]

Now a simple practice on this command will be shown.

\documentclass{article}
\usepackage{pst-plot}
\usepackage{pstricks}
\begin{document}
  \begin{pspicture}(-1,-1)(5.5,5.5)
    \psgrid[gridcolor=red,subgridcolor=green,
      gridlabels=0](0,0)(5,5)
    \psaxes{->}(0,0)(5.5,5.5)
    \fileplot[linecolor=blue,plotstyle=dots,
      dotstyle=*,dotsize=5pt]{mydata.dat}
  \end{pspicture}
\end{document}

File mydata.dat is shown below:

[{0.,0.},{0.25,1.},{0.5,1.41421},{0.75,1.73205},
{1.,2.},{1.25,2.23607},{1.5,2.44949},{1.75,2.64575},
{2.,2.82843},{2.25,3.},{2.5,3.16228},{2.75,3.31662},
{3.,3.4641},{3.25,3.60555},{3.5,3.74166},
{3.75,3.87298},{4.,4.},{4.25,4.12311},{4.5,4.24264},
{4.75,4.3589},{5.,4.47214}]

And the output picutre looks like this:

Fig.1 Plot data file with command fileplot.

Files Download: tex ps pdf

Other file plot commands will be talked next time.

Tuesday, December 13, 2011

How to plot using pstricks(1)----plot a function

After the previous posts, now we have the ability to deal with the plotting problems using pstricks. And this time we will talk about how to plot a function using pstricks.

The command used to plot a function in pstricks is "\psplot{xmin}{xmax}{function}". "xmin" and "xmax" determine the plot range and "function" is the function to be plotted. "function" should be written in postscript code. Since this blog is not about postscript, how to write a function in postscript code will not be talked in detail, only when it is used some simple explanation will be given.

There are also some optional parameters to control the appearance of the plot. "plotstyle=line,polygon,dots,curve..." controls the plotstyle, "plotpoints=int" controls how many points be plotted and other parameters such as "showpoints", "linestyle", "linecolor", "dotstyle"... have the same meaning as we have talked before.

Now we come to some examples.

\documentclass{article}
\usepackage{pst-plot}
\usepackage{pstricks}
\begin{document}
  \begin{pspicture}*(-1,-1)(5.5,5.5)
    \psgrid[gridlabels=0,gridcolor=gray,
      subgridcolor=lightgray](0,0)(5,5)
    \psaxes{->}(0,0)(0,0)(5.5,5.5)
    \psplot[linecolor=blue,plotstyle=dots,
      plotpoints=15,dotstyle=x,dotsize=4pt]
      {0}{5}{x}
    \rput{45}(2.5,3){\color{blue}$y=x$}
    \psplot[linecolor=red,plotstyle=dots,
      plotpoints=15,dotstyle=+,dotsize=4pt]
      {0}{5}{x 2 exp 5 div} %(x^2)/5
    \psplot[linecolor=red,plotstyle=line]
      {0}{5}{x 2 exp 5 div} %(x^2)/5
    \rput{55}(4,2.25)
      {\color{red}$y=\frac{x^{2}}{5}$}
    \rput(0.25,5.25){$y$}
    \rput(5.25,0.25){$x$}
  \end{pspicture}
\end{document} 

2
Fig.1 An example of Psplot—Plot function y = x and y = x /5.

\documentclass{article}
\usepackage{pst-plot}
\usepackage{pstricks}
\begin{document}
  \begin{pspicture}*(-1.25,-1.25)(4.5,1.25)
    \psgrid[gridlabels=0,gridcolor=red,
      subgridcolor=green](0,-1)(4,1)
    \psaxes[labels=y,tickstyle=bottom]
      {->}(0,0)(0,-1.25)(4.5,1.25)
    \rput(0.25,1){$y$}
    \rput(4.25,0.25){$x$}
    \psset{xunit=0.0111cm} %xuni=4cm/360
    \psplot[plotstyle=curve,
      linecolor=blue]{0}{360}{x sin}
       %"x sin" is postscript code
       %with meaing sin(x)
    \psplot[plotstyle=curve,linecolor=brown,
      linestyle=dashed]{0}{360}{x cos}
       %"x cos" is postscript code
       %with meaing cos(x)
    \uput[-90](90,0){$\frac{\pi}{2}$}
    \uput[-90](180,0){\scriptsize $\pi$}
    \uput[-90](270,0){$\frac{3\pi}{2}$}
    \uput[-90](360,0){\scriptsize $2\pi$}
  \end{pspicture}
\end{document}

Fig.2 Another example of Psplot—Plot function y = sin(x) and y =
cos(x).

Files Dowload: tex ps pdf

Sunday, December 11, 2011

Axes of coordinates

This posts will talk about making axes. Axes is an essential part in a plotting. And after this posts, I think we have gained enough knowledge to take over the subject of plotting. So in the next posts plotting will be talked about.

The command for drawing axes in pstricks is "\psaxes(x0,y0)(x1,y1)(x2,y2)" in package "pst-plot" (so do remember to include this package). (x0,y0) is the position of the origin. (x1,y1) is the lower left corner of the coordinate and (x2,y2) is the upper right corner. If (x0,y0) is not specified, (x1,y1) will be used. And if both (x0,y0) and (x1,y1) is not specified, then the origin of the current coordinate will be used. It is to say that "\psaxes(x1,y1)(x2,y2)" is equivalent to "\psaxes(x1,y1)(x1,y1)(x2,y2)" and "\psaxes(x2,y2)" is equivalent to "\psaxes(0,0)(0,0)(x2,y2)". Isn't it very similar to the command "\psgrid"?

As axes is in fact a line, the command "\psaxes" can take almost all the parameters a line can take. And there are also some parameters specially for it.

"Ox(y)=num" is the label at origin with default value 0. "Dx(y)=num" is the label increment with default value 1. "dx(y)=num" is the distance between labels. If it takes value 0, Dx*\psunit will be used, and the default value is 0. "labels/ticks=all/x/y/none" controls which labels/ticks will appear. "showorigin=true/false" controls whether the origin label will be drawn. "tickstyle" can take values "full","top" and "bottom". If "top" is chosen, the ticks are only on the side of the axes away from the label. "Bottom" is just the opposite. And "full" gives ticks on both sides of the axes. The size of the ticks can be set using parameter "ticksize". Another mostly used parameter is "axesstyle", its legal values include "axes","frame" and "none".

In the following example the using of "\psaxes" is illustrated.

\documentclass{article}
\usepackage{pst-plot}
\usepackage{pstricks}
\begin{document}
  \begin{pspicture}(5,5)
    \psaxes[axesstyle=frame,Ox=0,Oy=100](4,4)
  \end{pspicture}
  \hspace{1.5cm}
  \begin{pspicture}(5,5)
    \psframe[linecolor=lightgray](0,0)(5,5)
    \psaxes[Dx=1,Dy=0.5,linewidth=1pt,
      ticksize=4pt]{->}(2,2)(0.5,0.5)(4.5,4.5)
  \end{pspicture}

  \vspace{1cm}

  \begin{pspicture}(5,5)
    \psframe[linecolor=lightgray](0,0)(5,5)
    \psgrid[gridcolor=red,subgridcolor=green,
      gridlabels=0](1,1)(1,1)(4,4)
    \psaxes[showorigin=false,
      tickstyle=bottom]{->}(1,1)(4.5,4.5)
  \end{pspicture}
  \hspace{1.5cm}
  \begin{pspicture}(5,5)
    \psframe[linecolor=lightgray](0,0)(5,5)
    \psaxes[axesstyle=frame,
      showorigin=false](5,5)(5,5)(1,1)
  \end{pspicture}
\end{document} 

Fig.1 The usage of Psaxes
Files Download: tex ps pdf

Thursday, December 8, 2011

A Powerful Pstricks Tool ---- Psgrid

Making grid with pstricks is very easy. You can just include "\psgrid" in your pspicture environment (note that psgrid can also used outside pspicture environment), then a beautiful grid will be drawn, just like the following:

\documentclass{article}
\usepackage{pstricks}
\begin{document}
  \begin{pspicture}(5,4)
    \psgrid
  \end{pspicture}
\end{document}

Fig.1 The simplest usage of psgrid

Command "\psgrid" can be followed with three arguments, i.e., "\psgrid(x0,y0)(x1,y1)(x2,y2)". (x1,y1) and (x2,y2) are the opposing corner of the grid area. The dedault value of (x1,y1) is (0,0). If (x1,y1) and (x2,y2) are both not specified, in a pspicture environment the coordinate of the current pspicture will be used, and outside a pspicture environment (10,10) will be used. By default, the intervals are labeled. The horizontal labels will be placed at positions (*,y0) and the vertical labels will be placed at positions (x0,*). If (x0,y0) is omitted, (x1,y1) will be used.

The appearance of the grid can be changed if some parameters are applied."(sub)gridwidth","(sub)gridcolor" can be used to change the width and color of the main grid and sub grid. When (sub)griddots=num and num is a positive value , the (sub)grid lines are dotted with num dots per division. gridlabels and gridlabelcolor can used to control the size and color of the grid lables, and the default value of these two parameters is 10pt and black. If a positive int number(num) is set to parameter subgriddiv, then the main grid will be divided into num parts, and the default value is 5.

In the following example we will examine "\psgrid" in detail.

\documentclass{article}
\usepackage{pstricks}
\begin{document}
  \begin{pspicture}(9,9)
    \psgrid[gridcolor=red,subgridcolor=green]
      (1,5)(1,5)(4,8)
    \psgrid[gridcolor=blue,subgridcolor=yellow,
      gridlabels=5pt](6,6)(5,5)(8,8)
    \newrgbcolor{mycolor1}{0.5 0.3 0.3}
    \newrgbcolor{mycolor2}{0.3 0.5 0.5}
    \psgrid[gridcolor=mycolor1,
      subgridcolor=mycolor2,subgriddiv=2,
      subgridwidth=0.8pt,subgriddots=15,
      gridlabels=0](1,1)(4,4)
    \psgrid[griddots=10,subgriddots=3,
      gridlabelcolor=red](5,1)(8,4)
  \end{pspicture}
\end{document}

Fig.2 Psgrid with some options
Files Download: tex ps pdf
Creative Commons License
Except as otherwise noted, the content of this page is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.