Message Boards Message Boards

0
|
11449 Views
|
7 Replies
|
0 Total Likes
View groups...
Share
Share this post:

ListPlot and empty lists?

Posted 10 years ago

I am writing a notebook in which ListPlot is used to plot a list of 3 data lists. At least one of the data lists is non-empty. Any non-empty data list consists of a list of pairs. So this is a common application of plotting multiple data sets on a single graph.

The issue is that the data sets are derived from earlier processing in such a manner that either or both of the second and third list can be empty. Then I would just like Mathematica to plot the non-empty sets. I had hoped this would be straightforward. When I plot an empty list with ListPlot, I just get an empty plot. So far so good. But when I plot a list of lists in which contains an empty list, I get an error. Unless I find a way to solve this, the notebook must take on added complexity to account for the 4 possibilities. I can't just strip out the empty lists, since their identity is important and corresponds to a legend.

Does anyone see a nice way to do this? Some test code is below: (remove semicolons to get plots )

In[1]:= (* some data *)
d = Table[{n, Log[n]}, {n, 1, 100, 10}] // N;

In[2]:= (* it plots fine *)
ListPlot[d];

In[3]:= (* here ListPlot has an empty list, so it plots nothing *)
ListPlot[{}];

In[4]:= (* but if ListPlot is asked to plot both a non-empty list and \
an empty list, it generates an error *)
ListPlot[{d, {}}]

During evaluation of In[4]:= ListPlot::lpn: {<<1>>,{}} is not a list of numbers or pairs of numbers. >>

Out[4]= ListPlot[{{{1., 0.}, {11., 2.3979}, {21., 3.04452}, {31., 
    3.43399}, {41., 3.71357}, {51., 3.93183}, {61., 4.11087}, {71., 
    4.26268}, {81., 4.39445}, {91., 4.51086}}, {}}]

In[5]:= (* but if ListPlot is asked to plot both a non-empty list and \
an empty list, it generates an error *)
ListPlot[{d, {{}}}]

During evaluation of In[5]:= ListPlot::lpn: {<<1>>,{{}}} is not a list of numbers or pairs of numbers. >>

Out[5]= ListPlot[{{{1., 0.}, {11., 2.3979}, {21., 3.04452}, {31., 
    3.43399}, {41., 3.71357}, {51., 3.93183}, {61., 4.11087}, {71., 
    4.26268}, {81., 4.39445}, {91., 4.51086}}, {{}}}]
POSTED BY: David Keith
7 Replies
Posted 5 years ago

I am having this exact issue as well, in 11.3, but with ListPointPlot3D. I found the replacement rules with Missing[] or \[EmptySet] got rid of the errors, but didn't actually make the plot. I ended up instead replacing the empty lists with a single point at the origin, and then adding an If statement to PlotStyle to assign None if the length was zero., which preserved the structure of the Legend

POSTED BY: Evan Johnson
Posted 10 years ago

Thank to both of you. Works great.

Marco -- Interesting concept: the structure of the empty set. Maybe related to 1==={ {} }, 2==={ {},{} } . . . . ;-)

POSTED BY: David Keith

When I have had to deal with this issue I have generally replaced the empty list by a list with a single element with the value Missing[]. In the case of DateListPlot, I use an arbitrary date with its corresponding value as Missing[]. E.g.

ListPlot[{d, {Missing[]}}]
POSTED BY: David Reiss

Hi David,

I guess, that the same trick works:

DateListPlot[{FinancialData["GE", "Jan. 1, 2000"], FinancialData["AA", "Jan. 1, 2000"], {{{},}}},  PlotLegends -> Automatic]

The full version would be something like:

DateListPlot[{FinancialData["GE", "Jan. 1, 2000"], FinancialData["AA", "Jan. 1, 2000"], {}} /. {} -> {{{},}}, PlotLegends -> Automatic]

which gives

enter image description here

The main idea is that DateListPlot requires data in a different format from ListPlot. Roughly speaking a valid "empty set" for ListPlot, if the other lists are of the form {x,y}, is

{\[EmptySet]}

whereas DateListPlot requires a different "empty set". I suppose that there is a much nicer argument/way of solving this though.

Cheers,

Marco

POSTED BY: Marco Thiel
Posted 10 years ago

I thought I would try the same solution with DateListPlot, but it doesn't like the empty set.

POSTED BY: David Keith
Posted 10 years ago

Thank you, Marco. That is extremely useful By not stripping lists, I meant I would not what to just delete a list from the list of lists based on it having Length 0. But I can certainly run this rule and that will be much cleaner than handling the 4 cases separately. You rock!

POSTED BY: David Keith

Hi,

I have no really nice way of doing what you want to achieve, but I noticed that:

ListPlot[{d, {\[EmptySet]}}]

plots fine. And so does

ListPlot[{d, {_, _}}]

I guess that this command

ListPlot[{d, {{}}} /. {{}} -> {\[EmptySet]}]

could be useful. If you do that Mathematica does draw legends:

ListPlot[{d, {\[EmptySet]}}, PlotLegends -> Automatic]

enter image description here

Unfortunately, I do not really understand what you mean by "I can't just strip out the empty lists, since their identity is important and corresponds to a legend.", and therefore am not sure whether this is useful at all.

Cheers,

Marco

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract