0
$\begingroup$

I'm still pretty new to Mathematica and i want to plot the following data points with the x axis advancing in 0.5 steps:

OG[6] = 1.0685458987703726`;
OG[5.5] = 1.0649776023558106`;
OG[5] = 1.0585;
OG[4.5] = 1.046957482313205`;
OG[4] = 1.0453;
OG[3.5] = 0.9951879076222613`;
OG[3] = 0.9597;
OG[2.5] = 0.8761019519547198`;
OG[2] = 0.8023;
OG[1.5] = 0.809720272706028`;
OG[1] = 1.0542;
OG[0.5] = 1.4881005040525765`;
OG[0] = 1.9194;

The datapoints correspond to a upper price limit (number in brackets) in a financial market model. I wrote the datapoints together from different simulation runs so there is no function that gives me all of these.

When i try to plot them using the following code:

OG[P_]:= P
ListLineplot[Table[OG[P],{P,0,6,0.5}]]

The function of OG[P_] is random because i thought i need a function for Table to work.

I get this result which has nothing to do with the data I wanted to plot and the x axis only shows whole numbers.

My question is how do i plot this correctly so that the x axis advances in 0.5 steps and it plots my datapoints? Thanks in advance for you help.

$\endgroup$
  • $\begingroup$ Look up DataRange. Also, you don't need Table[] for plotting values, since you can do ListLinePlot[{1.9194, 1.4881005040525765, (* everything else *)}] directly. $\endgroup$ – J. M. is away Nov 1 '17 at 14:50
  • 1
    $\begingroup$ ListLinePlot[Table[{P, OG[P]}, {P, 0, 6, 0.5}]] will do the plot and use a horizontal increment of 0.5. If you don't include the {P, } or if you just use the raw list of numbers, it will use a horizontal increment of 1.0, not 0.5 $\endgroup$ – Bill Nov 1 '17 at 15:28
  • 1
    $\begingroup$ @Bill ... except if you use DataRange. $\endgroup$ – anderstood Nov 1 '17 at 15:45
  • $\begingroup$ @J.M. ok thx but is there a way to plot the values so that OG[0]has the coordinates (0, 1.9194) and ÒG[1]` the coordinates (0.5, 1.4881005040525765)? So you can see in the graph which value corresponds to which upper limit. $\endgroup$ – user52902 Nov 1 '17 at 15:55
  • 1
    $\begingroup$ @anderstood There are almost always a dozen different ways of accomplishing anything in Mathematica, almost always a few of which are completely incomprehensible. Pick one that you can remember and can likely use without making too many mistakes and stick with it $\endgroup$ – Bill Nov 1 '17 at 17:09
3
$\begingroup$

I don't think you need to define OG in this way. I would first try to get your data into a form like this (there can be many ways to do this, depending on how you want to enter your data):

data = {{0., 1.9194}, {0.5, 1.4881}, {1., 1.0542}, {1.5, 0.80972}, {2., 
  0.8023}, {2.5, 0.876102}, {3., 0.9597}, {3.5, 0.995188}, {4., 
  1.0453}, {4.5, 1.04696}, {5., 1.0585}, {5.5, 1.06498}, {6., 
  1.06855}};

ListLinePlot will take this and plot this in the way I think you want:

ListLinePlot[data]

ListLinePlot, plain

If you want to modify the $x$-axis to be in increments of 0.5, you can use the Ticks option:

ListLinePlot[
 data,
 Ticks -> {Range[0, 6, 0.5], Automatic}
]

ListLinePlot, modified ticks

(Of course, there's a lot more you can play with, formatting-wise; this is just a "basic" version you can start from.)


To give some insight into what (I think) is happening with your code above, is to note that the values of P that Table[OG[P], {P, 0, 6, 0.5}] goes through are non-integers (see Types of Numbers), whereas when you enter your data as, for example, OG[0] = 1.9194;, the 0 "argument" of OG is an integer.

So, when Table[OG[P], {P, 0, 6, 0.5}] looks for OG[0.] (for example), it doesn't find OG[0.] from the list of "OG[ ] =" lines, so it instead goes the OG[P_] := P line to figure out OG[0.] (0, according to OP[P_]:=P). You can see how this happens for all the "integer" values of P in your table:

Table of OGs

Another thing to note is that ListLinePlot[{y1,y2,…}] plots points {1,y1},{2,y2},..., so even if you were to plot ListLinePlot[{1.9194, 1.4881005040525765, ...}], the $x$-values would still be "incorrect".

Even in this case, you can, of course, still mess with the ticks to get the appearance you want, but I think a solution similar to the one I wrote above will be more straightforward.

Hopefully all this makes sense and is (at least a little bit) helpful!

$\endgroup$

Your Answer

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