# Ignoring missing data when plotting

**URL:** https://discourse.julialang.org/t/ignoring-missing-data-when-plotting/14124
**Category:** Visualization
**Created:** [August 27, 2018, 12:04pm UTC](https://discourse.julialang.org/t/ignoring-missing-data-when-plotting/14124 "2018-08-27T12:04:16Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Kryohi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kryohi/32/3539_2.png) [@Kryohi](https://discourse.julialang.org/u/Kryohi)
#### Post date: [August 27, 2018, 12:04pm UTC](https://discourse.julialang.org/t/ignoring-missing-data-when-plotting/14124/1 "2018-08-27T12:04:17Z")

</div>

Kind of a newbie question, so if this isn’t the appropriate section I’ll move it.

I was trying to plot a column of a dataframe with some elements that cannot be calculated.

At first I thought of initialize them to 0, and then picking only values different from 0, but it’s kind of contrived, so I was wondering if Plots.jl wouldn’t just ignore y-values if they are of missing type.

As it turns out it can’t, since plotting an array of the type `Array{Union{Float64,Missing}}` gives `TypeError: non-boolean (Missing) used in boolean context`.

Am I _missing_ something? I guessed this was the kind of use case for the new missing type… Of course it’s not a big problem, i can use findall or similar functions to trim unwanted values, but if it’s possible wouldn’t it make sense to be able to plot an array with missing elements?

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [August 27, 2018, 12:17pm UTC](https://discourse.julialang.org/t/ignoring-missing-data-when-plotting/14124/2 "2018-08-27T12:17:22Z")

</div>

Plots isn’t really Missings-compliant yet… The solution is to use StatPlots and plot directly from the Dataframe - that works. E.g.

```julia
using StatPlots, DataFrames
mydata = DataFrame(a = [1,missing,3], b = [2,3,4])
@df mydata scatter(:a, :b)

```

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [August 27, 2018, 2:30pm UTC](https://discourse.julialang.org/t/ignoring-missing-data-when-plotting/14124/3 "2018-08-27T14:30:47Z")

</div>

`replace(my_array, missing=>NaN)` maybe works?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 27, 2018, 3:16pm UTC](https://discourse.julialang.org/t/ignoring-missing-data-when-plotting/14124/4 "2018-08-27T15:16:33Z")

</div>

PGFPlotsX handles `missing` (by emitting a `nan` for pgfplots):

```julia
using PGFPlotsX
@pgf Plot({only_marks}, Table(x = [0, 1, missing, 3], y = [0, missing, 2, 3]))

```

![plot](https://global.discourse-cdn.com/julialang/original/3X/d/f/df5da5b45b6ec6f0f807606ab809c9b5564e01b2.png)

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [August 27, 2018, 3:42pm UTC](https://discourse.julialang.org/t/ignoring-missing-data-when-plotting/14124/5 "2018-08-27T15:42:40Z")

</div>

Yes, replacing missing values with NaN works. The only reason that’s not just done internally is that Plots accepts many other input types apart from Float64

---

<div class="post-metadata">

### Author: ![Kryohi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kryohi/32/3539_2.png) [@Kryohi](https://discourse.julialang.org/u/Kryohi)
#### Post date: [August 27, 2018, 5:48pm UTC](https://discourse.julialang.org/t/ignoring-missing-data-when-plotting/14124/6 "2018-08-27T17:48:12Z")

</div>

Using `NaN`s did the trick, thanks everyone!

I will also try the other packages mentioned.

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [August 28, 2018, 2:24am UTC](https://discourse.julialang.org/t/ignoring-missing-data-when-plotting/14124/7 "2018-08-28T02:24:34Z")

</div>

A bit of a hack that I use sometimes with Plots is to map `!ismissing` over your array, and then index with the result. Eg:

```julia
keep = map(!ismissing, my_ys)
plot(my_xs[keep], my_ys[keep])

```

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [August 28, 2018, 6:14am UTC](https://discourse.julialang.org/t/ignoring-missing-data-when-plotting/14124/8 "2018-08-28T06:14:45Z")

</div>

Then make sure you don’t have `missing`s in `my_xs`! 🙂

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [August 28, 2018, 7:38am UTC](https://discourse.julialang.org/t/ignoring-missing-data-when-plotting/14124/9 "2018-08-28T07:38:24Z")

</div>

OTOH we could do the same as StatPlots `@df` does: replace by appropriate thing if we know how to replace (strong and float) error on a missing otherwise

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [August 28, 2018, 7:43am UTC](https://discourse.julialang.org/t/ignoring-missing-data-when-plotting/14124/10 "2018-08-28T07:43:13Z")

</div>

Yes, easily enough, on the input data - because we already `copy` them here [Plots.jl/series.jl at 602dbdf1d260ef07daa2a2bdae558d82299e3d96 · JuliaPlots/Plots.jl · GitHub](https://github.com/JuliaPlots/Plots.jl/blob/602dbdf1d260ef07daa2a2bdae558d82299e3d96/src/series.jl#L75-L88)

But that wouldn’t work for any of the input passed as keyword arguments (the “attributes”). And frankly I think a cleaner approach is to provide first-class `missings` support in actually wrapping internal calls in Plots that may return `missing` in `skipmissing` where relevant. It’s just a bit more work.

---

<div class="post-metadata">

### Author: ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)
#### Post date: [August 28, 2018, 10:49am UTC](https://discourse.julialang.org/t/ignoring-missing-data-when-plotting/14124/11 "2018-08-28T10:49:23Z")

</div>

Plotly and Matlab, for example, discontinue the line of line/scatter plots when NaNs are present. This makes the fact of missing data visible without disturbing vizualisation of the good data too much. It can be useful, particularly when working with real (not simulated or modeled) data which can have some error condition flagged. Therefore, yes, it would be good if `missing`/`nothing` data could be handled within Plots taking this capability of the backend into account.

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [August 28, 2018, 11:48am UTC](https://discourse.julialang.org/t/ignoring-missing-data-when-plotting/14124/12 "2018-08-28T11:48:14Z")

</div>

Plots already handles `NaN` that way.

It’s just that it’s not straightforward to convert missing to NaN, since only Float64 vectors support NaN values. It could be done, as @piever said above, by replacing with NaN on Float64 input, “” on String input etc. But I’d say that given that `missing` is defined in Base, the cleanest approach is to propagate the `missing` values on to the backend plotting package and let that decide.

Maybe worth discussing in an issue.

[https://github.com/JuliaPlots/Plots.jl/issues/1706](https://github.com/JuliaPlots/Plots.jl/issues/1706)

---

<div class="post-metadata">

### Author: ![AlexisRenchon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexisrenchon/32/13328_2.png) [@AlexisRenchon](https://discourse.julialang.org/u/AlexisRenchon)
#### Post date: [November 27, 2019, 10:11pm UTC](https://discourse.julialang.org/t/ignoring-missing-data-when-plotting/14124/13 "2019-11-27T22:11:38Z")

</div>

Plots ignore NaNs to plot lines, but when I used scatter and the attribute z = marker\_z to color the dots, if my z has NaN elements, it is still going to plot a black dot.  
One way to go around this is for x or y to be = NaN, but that doesn’t seem very elegant.

Is there a way to make NaN in marker\_z to not plot the dot?
