(Answer) (Category) LON-CAPA User Help : (Category) Authoring : (Category) Authoring Problems :
How do I make a dynamically generated plot?
Dynamically generated plots are used to produce graphs which will be different for each student who views them. The plots are produced by calling 'gnuplot', and in fact the xml tag for the plots is <gnuplot >. Online help for the <gnuplot > tag is available by accessing the 'help' link when editing the problem.

Dynamically generated plots should be used in conjunction with a perl script block which generates the data to be plotted. If you are using static data a dynamically generated plot is not appropriate because of the overhead associated with generating the plot.

There are a great deal of parameters that can be set for a plot. These parameters are accessed by including various sub-tags. By default only the <gnuplot > tag and <curve > tag are present in a plot. The sub-tags allow you to define the axes of the plot, the presence of a key, the placement of gridlines, and the title and legends on the plot. The example given below shows the use of the <axis > sub-tag to set the domain and range shown in the graph.

Below is an example which produces a simple plot. To use it, create a new problem and [edit xml]. Remove all of the text and replace it with the text below:

 <problem>
 <script type="loncapa/perl">
 $amplitude = &random(3,5,1);
 for ($x=-6.0; $x<=6.0; $x+=0.05) {
     push @X,$x;
     push @Y, $amplitude * sin($x);
 }
 </script>
 <gnuplot font="medium" width="400" grid="on" height="300" border="on"  
      fgcolor="x000000" alttag="dynamically generated plot" align="center"  
      bgcolor="xffffff" transparent="off">
     <axis ymin="-6.0" ymax="6.0" xmin="-5.0" xmax="5.0" color="x000000" /> 
     <curve 
          linestyle="lines"
          pointtype="1"
          pointsize="1"
          name=""
          color="x000000">
         <data >@X</data>
         <data >@Y</data>
     </curve>
 </gnuplot>
 <startouttext /><br />
 What is the amplitude of this function?
 <endouttext />
 <numericalresponse answer="$amplitude">
     <responseparam type="tolerance" default="5%" name="tol" 
                    description="Numerical Tolerance" />
     <responseparam name="sig" type="int_range,0-16" default="0,15"  
                    description="Significant Figures" />
     <textline />
 </numericalresponse>
 </problem>
Below is a screenshot (in construction space) of the resulting plot.

(dynamically_generated_plot.gif)

It's likely that the above plot doesn't quite look right. It would be nice to have the gridlines drawn every 1 unit instead of every 2 units. A title, and labels on the axes, would be a nice addition. Although it's overkill for this example, we can also add a key for the plot. We can accomplish these changes by inserting sub-tags into the <gnuplot> tag.

Gridlines can be set using the <xtics> and <ytics> tags. The names of these tags correspond to the names of the commands used in gnuplot. We specify the beginning, end, and increment of the tick marks. Gnuplot only puts gridlines on the tick marks.

Inserting the <title>, <xlabel>, and <ylabel> commands allows us to set the title and axes labels as one would expect. Inserting a <key> tag, but not changing any of the information in it, signals gnuplot to place a key in the graph. If we decide we don't want the key, deleting the <key> tag will remove it from the graph.

These changes in the xml are shown below and a screenshot of the new plot is provided as well.

 <problem>
 <script type="loncapa/perl">
 $amplitude = &random(3,5,1);
 for ($x=-6.0; $x<=6.0; $x+=0.05) {
     push @X,$x;
     push @Y, $amplitude * sin($x);
 }
 </script>
 <gnuplot font="medium" width="400" grid="on" height="300" border="on"  
       fgcolor="x000000" alttag="dynamically generated plot" align="center"  
       bgcolor="xffffff" transparent="off">
     <key title="" pos="top right" box="off" />
     <ylabel >Y</ylabel>
     <xlabel >X</xlabel>
     <title >A sample plot</title>
     <xtics end="5.0" location="border" start="-5.0" increment="1.0" mirror="on" />
     <ytics end="6.0" location="border" start="-6.0" increment="1.0" mirror="on" />
     <axis ymin="-6.0" ymax="6.0" xmin="-5.0" xmax="5.0" color="x000000" />      
     <curve linestyle="lines" pointtype="1" pointsize="1" name="f(x)"   
         color="x000000">
         <data >@X</data>
         <data >@Y</data>
     </curve>
 </gnuplot>
 <startouttext />
 <br />
 What is the amplitude of this function?
 <endouttext />
 <numericalresponse answer="$amplitude">
     <responseparam type="tolerance" default="5%" name="tol" 
                    description="Numerical Tolerance" />
     <responseparam name="sig" type="int_range,0-16" default="0,15"  
                    description="Significant Figures" />
     <textline />
 </numericalresponse> 
 </problem>

(dynamic_plot_2.gif)

[Append to This Answer]
Previous: (Answer) How do I center an entire problem, including the [Submit Answers] button?
Next: (Answer) How can I create a dynamic plot of a piecewise defined function?
This document is: http://help.loncapa.org/cgi-bin/fom?file=150
[Search] [Appearance] [Show This Answer As Text]
This is a Faq-O-Matic 2.719.
This FAQ administered by the LON-CAPA team at MSU. Submit a help request ticket to contact us.