1
$\begingroup$

I have table with values like:

t = {1.234 <= a <= 2.345, 3.42 <= a <= 5.67, ...}

and I want to plot this table, the x axis should be the number of element in table (first value, second value etc.) and the y axis should be values of a which satisfy the inequality

$\endgroup$
  • $\begingroup$ @Artes Hmm...so what is the conclusion? Maybe that only one answer was as good as I expected, and only 4 were a little help or steer on the right course? $\endgroup$ – Ziva Jan 4 '14 at 16:51
  • $\begingroup$ @Artes I'm not expecting any help from you. If you want, you can answer my question. If you don't, it is also ok. First of all, I will accept any answer if I will read all answers and decide which was the most helpful and explained. Yo gave your answer two days ago, maybe I just think it wasn't an answer worth accepting. I am glad, that you posted your answer, but it just wasn't good for me, because it was not well explained and edited so many times, that I just stopped to read it. So I will not accept you answer, sorry. $\endgroup$ – Ziva Jan 4 '14 at 17:13
  • $\begingroup$ You deleted your comment that my answer was what you were looking for. In general I don't answer such questions, nonetheless I wanted to provide some help rather extraordinarily because of the new year. It appears I shouldn't. $\endgroup$ – Artes Jan 4 '14 at 22:11
  • $\begingroup$ My answer was edited only two times, and only because you requested some more detailed explanation. I did it partially in a comment and providing another approach in my answer. $\endgroup$ – Artes Jan 4 '14 at 22:32
3
$\begingroup$

Using pattern-replacement:

(* Generate some input *)
SeedRandom[1];
u := RandomReal[];
ranges = Table[u <= a <= u + 3, {i, 1, 10}]
(* test the patern replacement *)
ranges /. L_ <= a <= U_ -> {L, U}
(* plot *)
With[
 {pairs = ranges /. L_ <= a <= U_ -> {L, U}},
 ListPlot[{First /@ pairs, Last /@ pairs},
   Joined -> True, Filling -> {1 -> {2}}]]

output:

{0.242992 <= a <= 3.05046, 0.32668 <= a <= 3.86396, 
 0.626079 <= a <= 3.56989, 0.0298104 <= a <= 3.24608, 
 0.198906 <= a <= 3.65168, 0.534017 <= a <= 3.33043, 
 0.599726 <= a <= 3.43771, 0.780776 <= a <= 3.04535, 
 0.907559 <= a <= 3.18661, 0.394221 <= a <= 3.60119}

{{0.242992, 3.05046}, {0.32668, 3.86396}, {0.626079, 
  3.56989}, {0.0298104, 3.24608}, {0.198906, 3.65168}, {0.534017, 
  3.33043}, {0.599726, 3.43771}, {0.780776, 3.04535}, {0.907559, 
  3.18661}, {0.394221, 3.60119}}

enter image description here

Edit. Actually the pattern replacement is not needed, this will do just as well:

ListPlot[{First /@ ranges, Last /@ ranges}, Joined -> True, Filling -> {1 -> {2}}]
$\endgroup$
  • $\begingroup$ Your edit is nice! +1 $\endgroup$ – ybeltukov Jan 3 '14 at 13:12
7
$\begingroup$

Let's exploit an obvious approach using ParametricPlot:

tab = { 1.234 <= a <= 2.345, 3.42 <= a <= 5.67, 2.13 <= a <= 5.39, 
        3.14 <= a <= 4.66, 1.33 <= a <= 3.77, 2.31 <= a <= 5.11   };

Now one can map a function over tab

i = 0;
tab1 = { ++i, First @ # + ( Last @ # - First @ #) t}& /@ tab; 

or apply at the first level of tab another function:

i = 0;
tab1 = { ++i, #1 + (#3 - #1) t} & @@@ tab;

Most of Mathematica programmers prefer the latter approach, it is more concise and we expect it is more efficient (but in general it depends on various details of implementation, so it should be considered on a case by case basis).

ParametricPlot[ tab1, {t, 0, 1}, AxesOrigin -> {0, 0}, PlotStyle -> Thick] 

We needn't define another list of data since the above can be evaluated inside ParametricPlot, using different input e.g.:

tab3 = ((3 + 2Min @ # <= a <= 3 + 2 Max @ #& @ {Sin[#/3], -Sin[#/3]})& /@ Range[38]);

we could do this:

Block[{i = 0},
  ParametricPlot[{ ++i, #1 + (#3 - #1) #2} & @@@ tab3, {a, 0, 1}, 
                 AxesOrigin -> {0, 0}, PlotStyle -> Thickness[0.008], Evaluated -> True]]

insted of unnecesary playing with Block we could simply exploit Length of tab3 and Transpose:

ParametricPlot[ Transpose[{Range[Length[tab3]], #1 + (#3 - #1) #2 & @@@ tab3}], 
                {a, 0, 1}, AxesOrigin -> {0, 0}, PlotStyle -> Thickness[0.008], 
                Evaluated -> True]

enter image description here

$\endgroup$
  • $\begingroup$ In the first case we added ++i which evaluates to 1, 2, ..., n during subsequent steps. To understand the first approach use FullForm on tab or on any of its elements. Then you'll understand why we needed First and Last. In the second approach we used standard Mathematica notation for pure functions (#1, #3 denotes respectively the first and the third argument of oa function, if it applies to 1.234 <= a <= 2.345 #1 will evaluate to 1.234 while #3 to 2.345). See documentation what is a pure function. $\endgroup$ – Artes Jan 2 '14 at 23:57
4
$\begingroup$

A graphics approach:

tab = {1.234 <= a <= 2.345, 3.42 <= a <= 5.67, 2.13 <= a <= 5.39, 
   3.14 <= a <= 4.66, 1.33 <= a <= 3.77, 2.31 <= a <= 5.11};

Graphics[Line@MapIndexed[{{#2[[1]], #1[[1]]}, {#2[[1]], #1[[3]]}} &, tab], 
 Axes -> True, PlotRange -> {0, All}]

enter image description here

$\endgroup$
0
$\begingroup$

StackedListPlot

SeedRandom[1]
t = (# <= y <= #2 & @@@ (Sort /@ RandomReal[100, {40, 2}]));

t // Short[#, 5]&

{11.142 <= y <= 81.7389, 18.7803 <= y <= 78.9526, 6.57388 <= y <= 24.1361,<<34>>,
33.4215 <= y <= 83.3364, 69.4751 <= y <= 90.9209, 20.1479 <= y <= 85.104}

Get the lower series from and the difference between the upper and lower series from t:

t2 = Transpose[{#, #3 -#} & @@@ t];

StackedListPlot[t2, Filling -> {1 -> {2}}]

enter image description here

$\endgroup$

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.