5
$\begingroup$

Using the following code:

plot = Plot[Sin[x], {x, -Pi, Pi}];
points = Cases[plot // Normal, Line[{x__}] -> x, Infinity];
ListPlot[points]

I get:

enter image description here

Question: Is it possible to thicken such discretization? Also, you can do so at any given internal interval to the graph?

Thank you for your invaluable help.


EDIT:

To be honest I was wrong example, I try again.

plot = Show[Plot[Sin[x], {x, -Pi, Pi}]];
points = Cases[plot // Normal, Line[{x__}] -> x, Infinity];
ListPlot[points]

Here, in the latter case there is any hope to increase the points in a given interval, for example, in [-1,1]? Thanks again and sorry for my incompetence.

$\endgroup$
  • $\begingroup$ "Thicken"? You mean, like increase the number of points? Try adding PlotPoints -> 200 or something to the Plot function: Plot[Sin[x], {x, -Pi, Pi}, PlotPoints -> 200]. As for doing it in particular intervals, you can always make multiple plots with different PlotPoints, and then Show them all together on one plot. $\endgroup$ – march Aug 3 '16 at 20:57
  • 2
    $\begingroup$ You can control exactly the points plotted by creating the table yourself ListPlot[Table[{x,Sin[x]},{x,-Pi,Pi,2*Pi/(n-1)}]] where n is the number of points you want. $\endgroup$ – N.J.Evans Aug 3 '16 at 20:59
  • $\begingroup$ Thanks a lot, always clear. Unfortunately I was wrong example, in the sense that in fact I have to solmanete available a "show", as shown above later. Can you help me anyway? Thank you. $\endgroup$ – TeM Aug 3 '16 at 21:06
  • $\begingroup$ "increase the points in a given interval" - what do you need it for? $\endgroup$ – J. M. will be back soon Aug 3 '16 at 21:06
  • $\begingroup$ Maybe you would like Interpolation[points]? Look up the documentation for Interpolation. $\endgroup$ – march Aug 3 '16 at 21:54
3
$\begingroup$

Here one option is to add new points to increase density in a range.

The pointsInitial list is the values ​​you have in your points list. I've created a new list that creates new points for your given interval.

The Subdivide function controls how many points you want in the minimum and maximum limits. Then two controls were created for number of points.

The Join function joins these two lists

The Sort function organizes the values ​​in ascending order

The DeleteDuplicates function can delete duplicate values.

plot = Plot[Sin[x], {x, -Pi, Pi}];
pointsInitial = Flatten@{{#}, {Sin[#]}} & /@ Subdivide[-Pi, Pi, 100];
pointsAdditional = Flatten@{{#}, {Sin[#]}} & /@ Subdivide[-1, 1, 45];
ListPlot[DeleteDuplicates[Sort@Join[pointsInitial, pointsAdditional]],
  ImageSize -> 500]

IMAGEM

$\endgroup$
3
$\begingroup$

You can also use Mesh as follows:

mesh = Join[Subdivide[-Pi, -1, 20],  Subdivide[-1, 1, 50], Subdivide[1,Pi, 20]]

Plot[Sin[x], {x, -Pi, Pi},  
 Mesh -> {mesh}, MeshStyle -> Red, PlotStyle -> None]

Mathematica graphics

In version 9 or earlier versions, you can define mesh using Range instead of Subdivide as follows:

mesh = Join @@ Range[{-Pi, -1, 1}, {-1, 1, Pi}, {(-1 + Pi)/20, 2/50, (Pi - 1)/20}]

Also (thanks: @MichaelE2 ):

ListPlot@Transpose@{mesh, Sin@mesh}

Mathematica graphics

$\endgroup$
  • $\begingroup$ Also ListPlot@Transpose@{mesh, Sin@mesh}, plus styling as desired. $\endgroup$ – Michael E2 Jun 21 '17 at 21:27
  • $\begingroup$ @Michael, thank you. I updated with your suggestion. $\endgroup$ – kglr Jun 21 '17 at 22:36
2
$\begingroup$

If you have a function density[x] which is proportional to the desired density of the sampling near x, then the following is a way to generate samples with the desired variable density:

Clear[x, t];
interval = {-Pi, Pi};
density[x_] :=     (* NDSolve does not detect the discontinuities in Piecewise here *)
  Simplify`PWToUnitStep@Piecewise[{{3, -1 <= x <= 1}}, 1];   (* convert to UnitStep *)
samplingIFN = NDSolveValue[
   {x'[t] == 1/density[x[t]], x[0] == interval[[1]],
    WhenEvent[x[t] == interval[[2]], "StopIntegration"]},
   x, {t, 0, Infinity}];

domain = First@samplingIFN["Domain"];
xx = samplingIFN@Subdivide[Sequence @@ domain, 100];
ListPlot[Transpose@{xx, Sin@xx}]

Mathematica graphics

$\endgroup$
1
$\begingroup$

Assuming you just want a more even distribution of mesh points, another possibility is to use the "ArcLength" mesh function:

Plot[
    Sin[x], {x,-Pi,Pi},
    PlotStyle->None, MeshStyle->Blue, Mesh->100, MeshFunctions->{"ArcLength"}
]

enter image description here

$\endgroup$
  • $\begingroup$ I think the OP wants "to increase the points in a given interval, for example, in [-1,1]", i.e., increase the density. But since @Manu didn't accept any of those answers, maybe we're wrong. $\endgroup$ – Michael E2 Jun 22 '17 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.