# Analog of markevery in Plots.jl?

**URL:** <https://discourse.julialang.org/t/analog-of-markevery-in-plots-jl/10269>\
**Category:** Visualization\
**Created:** [April 10, 2018, 8:15pm UTC](https://discourse.julialang.org/t/analog-of-markevery-in-plots-jl/10269 "2018-04-10T20:15:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [April 10, 2018, 8:15pm UTC](https://discourse.julialang.org/t/analog-of-markevery-in-plots-jl/10269/1 "2018-04-10T20:15:47Z")

</div>

Matplotlib had a nice feature, `markevery=n`, that, for a bivariate data set, allowed you to only plot every n-th point. This was quite useful if you had an abundance of data and wanted to reduce the graphical intensity of a figure. Is there any natural analog in `Plots.jl`? Could there be?

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [April 10, 2018, 8:24pm UTC](https://discourse.julialang.org/t/analog-of-markevery-in-plots-jl/10269/2 "2018-04-10T20:24:30Z")

</div>

you should be able to use `view(x, 1:n:length(x))`!

---

<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:** [April 11, 2018, 6:36am UTC](https://discourse.julialang.org/t/analog-of-markevery-in-plots-jl/10269/3 "2018-04-11T06:36:57Z")

</div>

I’d say that is preferable to adding a keyword to `plot`

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 11, 2018, 8:10am UTC](https://discourse.julialang.org/t/analog-of-markevery-in-plots-jl/10269/4 "2018-04-11T08:10:44Z")

</div>

That works if you are just plotting a vector.

But if you are using a recipe that generates the plots for you, and you are plotting some complicated struct, it might be difficult create such a view perhaps?

---

<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:** [April 11, 2018, 9:02am UTC](https://discourse.julialang.org/t/analog-of-markevery-in-plots-jl/10269/5 "2018-04-11T09:02:57Z")

</div>

I guess that is true. I can’t really think of a concrete example but that’s not to say it won’t occur in the wild.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 11, 2018, 9:23am UTC](https://discourse.julialang.org/t/analog-of-markevery-in-plots-jl/10269/6 "2018-04-11T09:23:24Z")

</div>

Let’s say you are plotting some struct that represents a bunch of solutions to some ODE with a recipe. Maybe you have a ton of timesteps but for the plot you don’t want the plots to be so dense.

Here, you just want to tell Plots.jl, please spread the points out a bit, without having to mess with the struct itself.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [April 11, 2018, 9:31am UTC](https://discourse.julialang.org/t/analog-of-markevery-in-plots-jl/10269/7 "2018-04-11T09:31:00Z")

</div>

So that would be part of the recipe then, right? My point is, I think the most basic plotting construct should just accept views and not do this via kw\_args - however, how you downsample in recipes will be much more domain specific and possibly will need kw\_args 😉

---

<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:** [April 11, 2018, 9:48am UTC](https://discourse.julialang.org/t/analog-of-markevery-in-plots-jl/10269/8 "2018-04-11T09:48:55Z")

</div>

I think so too. It would be easy to implement a `thin` kwarg in a recipe. You’d lose the applicability across Plots but have complete flexibility in terms of how to interpret it in the context of your recipe.

---

<div class="post-metadata">

**Author:** ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)\
**Post date:** [April 11, 2018, 11:46am UTC](https://discourse.julialang.org/t/analog-of-markevery-in-plots-jl/10269/9 "2018-04-11T11:46:32Z")

</div>

A disadvantage of the `view` approach is that it’d apply to the line and the symbol. In matplotlib, markevery shows the line in full resolution. Using `view`, you have to `plot!`each series twice, once for the line and once for the symbols, and that makes it harder to make nice legends.

---

<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:** [April 11, 2018, 11:50am UTC](https://discourse.julialang.org/t/analog-of-markevery-in-plots-jl/10269/10 "2018-04-11T11:50:45Z")

</div>

Thanks for explaining the marker-line dichotomy, that makes the mechanism make more sense. Oh and `primary = false` is your friend wrt the legend.

---

<div class="post-metadata">

**Author:** ![nicolas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicolas/32/26439_2.png) [@nicolas](https://discourse.julialang.org/u/nicolas)\
**Post date:** [March 9, 2021, 11:05pm UTC](https://discourse.julialang.org/t/analog-of-markevery-in-plots-jl/10269/11 "2021-03-09T23:05:26Z")

</div>

Hi all, sorry for restarting this old thread. I had the same issue and worked out a solution in case people are reading this. Actually, for my sake, markevery wouldn’t have been exactly what I was after since it doesn’t account for changes in y.

```julia
using Plots, StatsFuns
pyplot()

function fixmarkerspacing(x, y, maxspace, rangex, rangey)
   newx = [x[1]]
   newy = [y[1]]
   sumnorm = 0
   for i ∈ 2:length(x)
      sumnorm += sqrt(((x[i] - x[i-1])/rangex)^2 + ((y[i] - y[i-1])/rangey)^2)
      if sumnorm > maxspace
         push!(newx,x[i])
         push!(newy,y[i])
         sumnorm = sumnorm - maxspace
         if sumnorm > maxspace # In case a jump in y was very large
            sumnorm = 0
         end
      end
   end
   return newx, newy
end

x = -10:0.05:10
y = logistic.(x)
newx, newy = fixmarkerspacing(x, y, 0.2, 20, 1)
plot([], [], markershape = :square, label = "foo", linecolor = :blue, markercolor = :red)
plot!(x, y, linecolor = :blue, label = "")
plot!(newx, newy, line = false, markershape = :square, label = "", markercolor = :red)

```

With the result:  
 ![evenmarkers](https://global.discourse-cdn.com/julialang/original/3X/0/8/08b46e472e3a251df28dc4ebfc0675d790c75537.png)
