How to force ListPlot to print the new curve while keeping the old one

3

2

I would like to create a graph using ListPlot which prints new dataset every time the button is evaluated (based on the value given in InputField), by adding the new curve to the one that has been previously plotted, showing multiple curves.

For example: if the button is clicked 5 times, the graph should show 5 curves.

The ListPlot I have here does replace the old dataset with the new one (always showing only one curve) every time the value of InputField is changed and button is re-evaluated, which is not what I want. How to force ListPlot to print the new curve while keeping the old one?

 DynamicModule[{inputValue = 0, dataSet = {}}, 
 Column[{
   InputField[Dynamic[inputValue]], 
   Button["Plot", dataSet = Table[inputValue + i^j, {i, 5}, {j, 2}]], 
   Spacer[5],
   Dynamic@ListPlot[dataSet, Joined -> True, ImageSize -> {300}]
   }]] 

enter image description here

Kamila

Posted 2019-03-08T17:58:49.577

Reputation: 77

Answers

5

Append the new data to your dataSet each time the button is pressed, rather than simply assigning it a new value:

DynamicModule[
 {inputValue = 0, dataSet = {}},
 Column[{
   InputField[Dynamic[inputValue]],
   Button["Plot", AppendTo[dataSet, Table[inputValue + i^j, {i, 5}, {j, 2}]]],
   Spacer[5],
   Dynamic@ListPlot[dataSet, Joined -> True, ImageSize -> {300}]
 }]
]

enter image description here

MarcoB

Posted 2019-03-08T17:58:49.577

Reputation: 53 573

Thanks a lot!, this is exactly what I need :) – Kamila – 2019-03-08T18:23:12.620