Mathematica: How to obtain data points plotted by plot command?

后端 未结 7 1804
[愿得一人]
[愿得一人] 2021-01-30 15:34

When plotting a function using Plot, I would like to obtain the set of data points plotted by the Plot command.

For instance, how can I obtain the list of points {t,f} P

7条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-30 15:50

    Based on the answer of Sjoerd C. de Vries, I've now written the following code which automates a plot preview (tested on Mathematica 8):

    pairs[x_, y_List]:={x, #}& /@ y
    pairs[x_, y_]:={x, y}
    condtranspose[x:{{_List ..}..}]:=Transpose @ x
    condtranspose[x_]:=x
    Protect[SaveData]
    MonitorPlot[f_, range_, options: OptionsPattern[]]:=
      Module[{data={}, plot},
        Module[{tmp=#},
          If[FilterRules[{options},SaveData]!={},
            ReleaseHold[Hold[SaveData=condtranspose[data]]/.FilterRules[{options},SaveData]];tmp]]&@
        Monitor[Plot[(data=Union[data, {pairs[range[[1]], #]}]; #)& @ f, range,
                     Evaluate[FilterRules[{options}, Options[Plot]]]],
          plot=ListLinePlot[condtranspose[data], Mesh->All,
          FilterRules[{options}, Options[ListLinePlot]]];
          Show[plot, Module[{yrange=Options[plot, PlotRange][[1,2,2]]},
            Graphics[Line[{{range[[1]], yrange[[1]]}, {range[[1]], yrange[[2]]}}]]]]]]
    SetAttributes[MonitorPlot, HoldAll]
    

    In addition to showing the progress of the plot, it also marks the x position where it currently calculates.

    The main problem is that for multiple plots, Mathematica applies the same plot style for all curves in the final plot (interestingly, it doesn't on the temporary plots).

    To get the data produced into the variable dest, use the option SaveData:>dest

提交回复
热议问题