Re: ListPlot: Choose segents to draw.
- To: mathgroup at smc.vnet.net
- Subject: [mg82366] Re: ListPlot: Choose segents to draw.
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Thu, 18 Oct 2007 05:00:46 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <ff4g7u$fmv$1@smc.vnet.net>
Nacho wrote:
> Using Mathematica V6, I have several hundred lists to plot.
>
> They draw OK just with
>
> ListPlot[mytable, Joined->True,PlotRange->All]
>
> But several hundred plots are a mess. I'm only interested in the
> segments of each list that are increasing, that is, for every x(i),
> x(i)>x(i+1).
>
> Is there any set of options to ListPlot to make the descending
> segments invisible? Some kind of MeshShading to choose betweet
> Automatic and None depending of two consecutive values.
>
> If I have to create a new mytable, please bear in mind that the total
> length of the lists must remain equal.
The solution I suggest is the following:
ListPlot[mytable, PlotRange -> All,
Epilog -> {Line /@
DeleteCases[Split[mytable, #1[[-1]] < #2[[-1]] &], {{_, _}}]}]
How does it work? We assume that mytable is a list of pairs, each pair
being the (x, y) coordinates of an individual point. (Note that, in the
example below, I have chosen integer coordinates for convenience only.)
To draw the ascending lines, we use the *Epilog* option and use a user
defined function to draw the lines of interest.
In[1]:= mytable = Table[{n, RandomInteger[{0, 5}]}, {n, 10}]
Out[1]= {{1, 3}, {2, 4}, {3, 2}, {4, 5}, {5, 5}, {6, 4}, {7, 2}, {8,
5}, {9, 4}, {10, 2}}
We split the original list in running sequences of points taken
according to their increasing values of their second element (the
y-coordinate).
In[2]:= Split[mytable, #1[[-1]] < #2[[-1]] &]
Out[2]= {{{1, 3}, {2, 4}}, {{3, 2}, {4, 5}}, {{5, 5}}, {{6, 4}}, {{7,
2}, {8, 5}}, {{9, 4}}, {{10, 2}}}
Any sequence of only one point must be discarded.
In[3]:= DeleteCases[%, {{_, _}}]
Out[3]= {{{1, 3}, {2, 4}}, {{3, 2}, {4, 5}}, {{7, 2}, {8, 5}}}
Finally, we apply the function *Line* to the each resulting sequence.
In[4]:= Line /@ %
Out[4]= {Line[{{1, 3}, {2, 4}}], Line[{{3, 2}, {4, 5}}],
Line[{{7, 2}, {8, 5}}]}
Here is the resulting plot,
In[5]:= ListPlot[mytable, PlotRange -> All,
Epilog -> {Line /@
DeleteCases[Split[mytable, #1[[-1]] < #2[[-1]] &], {{_, _}}]}]
Out[5]= [ ... plot deleted ... ]
that we can compare with the original.
In[6]:= ListPlot[mytable, Joined -> True, PlotRange -> All]
Out[6]= [ ... plot deleted ... ]
I hope that this answers your question,
--
Jean-Marc