3

Is there a way in Mathematica to check whether a plot is empty or not?

By empty, I mean it is only showing the axes and not any data points. I have a function that, depending on its inputs, gives some data points (to be plotted later) or none, but i won't know whether it will produce any valid data points unless i plot it. And if it doesn't, when i plot it, it will return an empty plot. I would like to differentiate between the empty plot and a plot with data points.

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
darXider
  • 447
  • 5
  • 16
  • 1
    In conjunction with `ListPlot` you could apply the function to the relevant range of data before plotting and then check what results you had. Is there a reason the plotting has to be bound to the function execution? Do you have a short code example to show exactly what you mean? – image_doctor Jun 18 '12 at 07:06

1 Answers1

2

I'll generate an empty Plot to explore the form.

empty = Plot[{}, {x, 0, 1}];
FullForm[%]

shows that Plot[] returns a Graphics object with two parts - 1.) the content, and 2.) the options. In this case the first is an empty List, so setting a condition like

empty[[1]] == {}

should return True for this particular type of emptiness. It'll work for other Plots

Plot3D[{}, {x, -3, 3}, {y, -2, 2}][[1]] == {}

True

but you might have to pick apart the FullForm of your example to be sure.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
Fred Klingener
  • 146
  • 1
  • 6