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

No comments:

Post a Comment

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.