Plotting Multiple Functions with Different Colors in LaTeX

Plotting Multiple Functions with Different Colors in LaTeX

Plotting multiple functions with different colors in LaTeX is both feasible and straightforward, thanks to the powerful graphic packages such as PSTricks and TikZ. These packages enable you to create sophisticated and customizable visualizations, making your document enhanced and more engaging.

Introduction to PSTricks and TikZ

Two of the most used packages for drawing in LaTeX are PSTricks and TikZ. Both offer extensive capabilities for creating various types of diagrams, plots, and shapes. PSTricks is particularly strong in drawing complex figures and has built-in support for a wide range of features, while TikZ provides a more flexible and powerful framework, which makes it suitable for a broader range of drawing tasks.

Using PSTricks for Advanced Plotting

Here is an example of how you might use PSTricks to plot several functions with different colors. This example was used in a class teaching about integrals and includes custom regions that are colored.

Note that PSTricks can be used when compiling your document with XeLaTeX, but not without extra help at least with PDLaTeX. For this reason, many people prefer TikZ. I have nothing against TikZ, but I am more familiar with PSTricks.

Here is the code I used for the above image:

begin{pspicture}(-2,-1)(5,5.5)
    pscustom[linestylenone,fillstylesolid,fillcolorlightgray]{
        psline(0,0)(1,1)
        psplot[algebraic]{0}{2}{0.5*x-1}
        psplot[algebraic]{2}{2.8284}{4-0.5*x^2}
        psline(2.8284,0)(0,0)
    }
    psline{-}(-1,-1)(5,-1)
    psline{-}(-2,0)(-2,4.90)
    rput{30}(-0.5,0.5){small $frac{1}{2}x-1$}
    rput{45}(-1,3.0){small $4-frac{1}{2}x^{2}$}
    psplot[algebraic]{-1}{4}{0.5*x-1}
    psplot[algebraic]{-2}{4}{4-0.5*x^2}
    psline[linestyledotted](2,0)(2,2)
    rput[B](2,-0.4){small a}
    rput[B](2.8284,-0.4){small b}
end{pspicture}

Adding Color to Your Functions

Let's say you want the parabola to be in blue and the line to be in red. All you have to do is specify the desired color in the psplot command like this:

psplot[algebraic, linecolorred]{-1}{4}{0.5*x-1}
psplot[algebraic, linecolorblue]{-2}{4}{4-0.5*x^2}

In the above code, the optional linecolor parameter is used to set the color of the plotted functions. This allows for easy customization and differentiation between functions in your plot.

Conclusion

Both PSTricks and TikZ offer powerful tools for plotting multiple functions with different colors in LaTeX. PSTricks is great for simple and advanced drawings and provides an easy-to-use interface, while TikZ is highly flexible and versatile, making it suitable for a broad range of complex drawings. Whether you prefer the simplicity of PSTricks or the power of TikZ, you can create stunning visualizations in your documents with these tools.