{"id":391,"date":"2019-04-23T14:50:18","date_gmt":"2019-04-23T18:50:18","guid":{"rendered":"https:\/\/pressbooks.bccampus.ca\/introtolatex\/?post_type=chapter&#038;p=391"},"modified":"2019-05-08T18:58:53","modified_gmt":"2019-05-08T22:58:53","slug":"plot-practice-exercises","status":"publish","type":"chapter","link":"https:\/\/pressbooks.bccampus.ca\/introtolatex\/chapter\/plot-practice-exercises\/","title":{"raw":"Plot Practice Exercises","rendered":"Plot Practice Exercises"},"content":{"raw":"Before starting these exercises, make sure you have added the tikz and pgfplots packages by following the instructions at the beginning of the section. Solutions are included after the exercises.\r\n<ol>\r\n \t<li>First, we'll make some modifications to the plot from the previous section to get more familiar with the code.\r\n<ol>\r\n \t<li>Change the domain to between -2 and +2.<\/li>\r\n \t<li>Change the label on the y-axis to 'y'.<\/li>\r\n \t<li>Change the color of the line to green.<\/li>\r\n \t<li>Add a second quadratic function. You may need to adjust the domain to see both functions.<\/li>\r\n<\/ol>\r\nHere is the code for the original plot:\r\n<code>[\u200clatex]\r\n\\begin{tikzpicture}\r\n\\begin{axis}[axis lines = left, xlabel = $x$, ylabel = $f(x)$]\r\n\\addplot [domain=-4:4, samples=100, color=blue]\r\n{x^2 + 2*x + 1};\r\n\\end{axis}\r\n\\end{tikzpicture}\r\n[\/latex]<\/code><\/li>\r\n \t<li>We'll now make the plot more informative by adding a title and legend. Add a title to your plot by adding <code>title=Your Title<\/code> to the axes parameters. Add a legend by adding <code>\\legend{function 1, function 2}<\/code> after the addplot commands.<br \/>\r\n<strong>Hint:<\/strong> If your legend covers up an important part of your plot, add <code>legend pos=north\/south east\/west<\/code> to the axis parameters to move it. For example, to move the legend to the upper left use <code>legend pos=north west<\/code>.<\/li>\r\n \t<li>Change the color of the plot to a color mapping using by adding the parameters <code>colorbar, point meta rel=axes wide, point meta=y<\/code> to the axis parameters, changing the parameters for each addplot to <code>mesh, marks=none<\/code>, and changing the <code>addplot<\/code> command to <code>addplot+<\/code>. What is the difference in color if you set <code>point meta res=per plot<\/code>?<br \/><strong>Note:<\/strong> You need to change addplot to addplot+ because there are parameters for the plot in the axis parameters. Without the +, the style changes would override the axis parameters.<\/li>\r\n \t<li>Change the function used in the 3D surface plot from the last section. Try the function <code>1\/x + 1\/y<\/code> or come up with your own. Note that in pgf plots you use <code>a\/b<\/code> for division and <code>a*b<\/code> for multiplication rather than the regular Latex functions.Depending on the function you choose, you might want to change the domain to see an interesting range for the function. For example, [latex]\\frac{1}{x} + \\frac{1}{y}[\/latex] is most interesting near 0, so try a domain like -2:2.\r\n\r\nHere is the code for the original plot:<code>\r\n[\u200clatex]\r\n\\begin{tikzpicture}\r\n\\begin{axis}\r\n\\addplot3[surf, domain=0:360, samples=40] {cos(y) + sin(x)};\r\n\\end{axis}\r\n\\end{tikzpicture}\r\n[\/latex]<\/code><\/li>\r\n \t<li>PGFPlots can also create bar graphs. <a href=\"https:\/\/www.iro.umontreal.ca\/~simardr\/pgfplots.pdf\">Look at page 45 of the PGFplots documentation for some examples.<\/a> Copy the first example from the top of page 45 and use a second instance of addplot coordinates to add red bars beside the blue, like in the plot below.[latex]\r\n\\begin{tikzpicture}\r\n\\begin{axis}[ybar, enlarge y limits={0.15,upper},\r\nenlarge x limits=0.15]\r\n\\addplot plot coordinates\r\n{(0,3) (1,2) (2,4) (3,1) (4,2)};\r\n\\addplot plot coordinates\r\n{(0,2) (1,3) (2,6) (3,0) (4,4)};\r\n\\end{axis}\r\n\\end{tikzpicture}\r\n[\/latex]\r\n\r\n<strong>Hint:<\/strong> If you are having trouble having the bars start exactly on the x-axis, set the limits individually using <code>enlarge y limits={0.15,upper},\r\nenlarge x limits=0.15<\/code> instead of <code>enlargelimits=0.15<\/code>.<\/li>\r\n<\/ol>","rendered":"<p>Before starting these exercises, make sure you have added the tikz and pgfplots packages by following the instructions at the beginning of the section. Solutions are included after the exercises.<\/p>\n<ol>\n<li>First, we&#8217;ll make some modifications to the plot from the previous section to get more familiar with the code.\n<ol>\n<li>Change the domain to between -2 and +2.<\/li>\n<li>Change the label on the y-axis to &#8216;y&#8217;.<\/li>\n<li>Change the color of the line to green.<\/li>\n<li>Add a second quadratic function. You may need to adjust the domain to see both functions.<\/li>\n<\/ol>\n<p>Here is the code for the original plot:<br \/>\n<code>[\u200clatex]<br \/>\n\\begin{tikzpicture}<br \/>\n\\begin{axis}[axis lines = left, xlabel = $x$, ylabel = $f(x)$]<br \/>\n\\addplot [domain=-4:4, samples=100, color=blue]<br \/>\n{x^2 + 2*x + 1};<br \/>\n\\end{axis}<br \/>\n\\end{tikzpicture}<br \/>\n[\/latex]<\/code><\/li>\n<li>We&#8217;ll now make the plot more informative by adding a title and legend. Add a title to your plot by adding <code>title=Your Title<\/code> to the axes parameters. Add a legend by adding <code>\\legend{function 1, function 2}<\/code> after the addplot commands.<br \/>\n<strong>Hint:<\/strong> If your legend covers up an important part of your plot, add <code>legend pos=north\/south east\/west<\/code> to the axis parameters to move it. For example, to move the legend to the upper left use <code>legend pos=north west<\/code>.<\/li>\n<li>Change the color of the plot to a color mapping using by adding the parameters <code>colorbar, point meta rel=axes wide, point meta=y<\/code> to the axis parameters, changing the parameters for each addplot to <code>mesh, marks=none<\/code>, and changing the <code>addplot<\/code> command to <code>addplot+<\/code>. What is the difference in color if you set <code>point meta res=per plot<\/code>?<br \/><strong>Note:<\/strong> You need to change addplot to addplot+ because there are parameters for the plot in the axis parameters. Without the +, the style changes would override the axis parameters.<\/li>\n<li>Change the function used in the 3D surface plot from the last section. Try the function <code>1\/x + 1\/y<\/code> or come up with your own. Note that in pgf plots you use <code>a\/b<\/code> for division and <code>a*b<\/code> for multiplication rather than the regular Latex functions.Depending on the function you choose, you might want to change the domain to see an interesting range for the function. For example, <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/pressbooks.bccampus.ca\/introtolatex\/wp-content\/ql-cache\/quicklatex.com-d74435529f17a706b077c2a81ec02c32_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#102;&#114;&#97;&#99;&#123;&#49;&#125;&#123;&#120;&#125;&#32;&#43;&#32;&#92;&#102;&#114;&#97;&#99;&#123;&#49;&#125;&#123;&#121;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"21\" width=\"33\" style=\"vertical-align: -7px;\" \/> is most interesting near 0, so try a domain like -2:2.\n<p>Here is the code for the original plot:<code><br \/>\n[\u200clatex]<br \/>\n\\begin{tikzpicture}<br \/>\n\\begin{axis}<br \/>\n\\addplot3[surf, domain=0:360, samples=40] {cos(y) + sin(x)};<br \/>\n\\end{axis}<br \/>\n\\end{tikzpicture}<br \/>\n[\/latex]<\/code><\/li>\n<li>PGFPlots can also create bar graphs. <a href=\"https:\/\/www.iro.umontreal.ca\/~simardr\/pgfplots.pdf\">Look at page 45 of the PGFplots documentation for some examples.<\/a> Copy the first example from the top of page 45 and use a second instance of addplot coordinates to add red bars beside the blue, like in the plot below.\n<p class=\"ql-center-picture\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/pressbooks.bccampus.ca\/introtolatex\/wp-content\/ql-cache\/quicklatex.com-68b6618b69ce2052fb9870a3ec5330b3_l3.png\" height=\"242\" width=\"272\" class=\"ql-img-picture quicklatex-auto-format\" alt=\"Rendered by QuickLaTeX.com\" title=\"Rendered by QuickLaTeX.com\" \/><\/p>\n<p><strong>Hint:<\/strong> If you are having trouble having the bars start exactly on the x-axis, set the limits individually using <code>enlarge y limits={0.15,upper},<br \/>\nenlarge x limits=0.15<\/code> instead of <code>enlargelimits=0.15<\/code>.<\/li>\n<\/ol>\n","protected":false},"author":671,"menu_order":3,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[],"license":[],"class_list":["post-391","chapter","type-chapter","status-publish","hentry"],"part":204,"_links":{"self":[{"href":"https:\/\/pressbooks.bccampus.ca\/introtolatex\/wp-json\/pressbooks\/v2\/chapters\/391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pressbooks.bccampus.ca\/introtolatex\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/pressbooks.bccampus.ca\/introtolatex\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/introtolatex\/wp-json\/wp\/v2\/users\/671"}],"version-history":[{"count":25,"href":"https:\/\/pressbooks.bccampus.ca\/introtolatex\/wp-json\/pressbooks\/v2\/chapters\/391\/revisions"}],"predecessor-version":[{"id":810,"href":"https:\/\/pressbooks.bccampus.ca\/introtolatex\/wp-json\/pressbooks\/v2\/chapters\/391\/revisions\/810"}],"part":[{"href":"https:\/\/pressbooks.bccampus.ca\/introtolatex\/wp-json\/pressbooks\/v2\/parts\/204"}],"metadata":[{"href":"https:\/\/pressbooks.bccampus.ca\/introtolatex\/wp-json\/pressbooks\/v2\/chapters\/391\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/pressbooks.bccampus.ca\/introtolatex\/wp-json\/wp\/v2\/media?parent=391"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/introtolatex\/wp-json\/pressbooks\/v2\/chapter-type?post=391"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/introtolatex\/wp-json\/wp\/v2\/contributor?post=391"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/introtolatex\/wp-json\/wp\/v2\/license?post=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}