Showing posts with label \psplot. Show all posts
Showing posts with label \psplot. 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, March 27, 2012

Plot in polar coordinate --- polarplot

With the option "polarplot=true", it is possible to use command "\psplot" to create a polar plot. The syntax is the same as usual --- "\psplot[polarplot=true,...]{x0}{x1}{function}" where "x0" and "x1" are the start and end angle. "function" is interpreted as a function r=f(theta) in the polar plotting. So if a circle with radius 1 is to be plotted, command "\psplot[polarplot=true]{0}{360}{1}" can be used.
Now we come to draw a cardioid. The function of a cardioid in polar coordinate reads "r=a(1+sin(x))". And we take a=1.5.

\documentclass{article}
\usepackage{pstricks}
\usepackage{pstricks-add}
\usepackage{pst-plot}
\begin{document}
  \psset{plotpoints=500}
  \begin{psgraph}{->}(0,0)(-0.5,-2)(3.5,2.5){5cm}{5cm}
    \psplot[polarplot=true]{0}{360}
      {x cos 1 add 1.5 mul}
  \end{psgraph}
\end{document}

Fig.1 Plot in polar coordinate --- cardioid.

The axesstyle can also be set to polar. The only thing need to do is setting "axesstyle=polar". This time we use a three-leaved rose curve (r=a*sin(3x),a=3.5) to shown it.

\documentclass{article}
\usepackage{pstricks}
\usepackage{pstricks-add}
\usepackage{pst-plot}
\begin{document}
\psset{plotpoints=500}
\begin{pspicture}(-4,-4)(4,4)
  \psaxes[axesstyle=polar,xAxisLabel=some,
    subticks=2,tickcolor=red,tickwidth=1pt,
    subtickcolor=green]{->}(0,0)(-4,4)(4,4)
  \psplot[polarplot=true,linewidth=2pt,
    linecolor=blue]{0}{360}
    {3 x mul sin 3.5 mul}
\end{pspicture}
\end{document}

Fig.2 Plot in polar coordinate — three leaved rose curve.
 
Files Download: tex ps pdf 

Saturday, February 4, 2012

More options on pstricks plotting (2)

When a Logarithmic scale graph is plot, "x(y)logBase=num|empty", and "logLines=none|x|y|all" are useful. The first one controls the logBse of corresponding axes. The later one controls the log grid line.

Let us plot a logarithmic scale graph to shown how these options are used.

\documentclass{article}
\usepackage{pstricks}
\usepackage{pstricks-add}
\usepackage{pst-plot}
\begin{document}
  \begin{pspicture}(0,0)(6,5)
  \psaxes[axesstyle=frame,ylogBase=10,
    subticks=5,logLines=y](0,0)(6,5)
  \psplot[linecolor=red]{0}{5}{x}
  \psplot[linecolor=blue,linestyle=dotted,
    linewidth=1.5pt,dotsep=1pt]
    {0}{6}{0.5 x mul}
  \psplot[linecolor=green,linestyle=dashed]
    {1}{6}{x log 4 mul}
  \rput(3.5,4.25){\color{red}$y=10^{x}$}
  \rput(3,2.25){\color{green}$y=x^{4}$}
  \rput(3.5,1.25){\color{blue}$y=10^{x/2}$}
  \rput(3,-0.75){$x$}
  \rput(-1,2.5){$y$}
  \end{pspicture}
\end{document}

Fig.1 Plotting Logarithmic scale graph using Pstricks.

Options "nStep, nStart, nEnd, xStep, XStart, xEnd, yStart, yEnd" controls the plot range.

By default the plot macros expect x|y data records, but when data files contains more than one y value, like:
x y1 y2 ... yMax
x y1 y2 ... yMax
...
one can select the column to be plotted using "plotNo" and "PlotNoMax". "plotNo=num" tells pst-plot to plot the num-th y-column. "plotNoMax" tells pst-plot how many y-column are present.

Files: tex ps pdf

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
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.