# Plots.jl custom linestyle: longer dashes

**URL:** <https://discourse.julialang.org/t/plots-jl-custom-linestyle-longer-dashes/117772>\
**Category:** Visualization\
**Tags:** plots, pgfplotsx\
**Created:** [August 2, 2024, 6:22pm UTC](https://discourse.julialang.org/t/plots-jl-custom-linestyle-longer-dashes/117772 "2024-08-02T18:22:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [August 2, 2024, 6:22pm UTC](https://discourse.julialang.org/t/plots-jl-custom-linestyle-longer-dashes/117772/1 "2024-08-02T18:22:32Z")

</div>

I would like to insist whether there is a way to make a customized dash for the linestyle of Plots.jl, by choosing its length and the gap length as well among the dashes. There is a similar topic under [Longer dashes in dashed lines](https://discourse.julialang.org/t/longer-dashes-in-dashed-lines/90104),  
but with no simple solution.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [August 2, 2024, 11:03pm UTC](https://discourse.julialang.org/t/plots-jl-custom-linestyle-longer-dashes/117772/2 "2024-08-02T23:03:28Z")

</div>

As documented in [this PR](https://github.com/JuliaPlots/Plots.jl/pull/4765) you can do this with the `PGFPlotsX` back end:

```julia
using Plots
pgfplotsx()
plot(1:5, ls = :dash, dash_pattern="on 4cm off 2cm")

```

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

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [August 4, 2024, 8:31pm UTC](https://discourse.julialang.org/t/plots-jl-custom-linestyle-longer-dashes/117772/3 "2024-08-04T20:31:24Z")

</div>

@PeterSimon Could you elaborate a bit more on your solution: I have looked up TikZ manual, and there are several more options, such as `\draw[solid]`, `\draw[loosely dashed]`, etc; could we use them from within the plotspgfx backend in julia?

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [August 4, 2024, 10:14pm UTC](https://discourse.julialang.org/t/plots-jl-custom-linestyle-longer-dashes/117772/4 "2024-08-04T22:14:58Z")

</div>

I’m not sure whether or not you can pass those types of options (I suspect not), but I also don’t think they are necessary. You can control the line style with the usual `linestyle` option of `Plots` and you can customize dash styles as desired using the `dash_pattern` keyword argument:

```julia
using Plots
pgfplotsx()
x = 0:10
y = ones(length(x))
default(lw=2)
plot(x, 1*y, ls=:solid)
plot!(x, 2*y, ls=:dot)
plot!(x, 3*y, ls=:dash)
plot!(x, 4*y, ls=:dashdotdot)
plot!(x, 5*y, ls=:dash, dash_pattern="on 1cm off 0.5cm")    
plot!(x, 6*y, ls=:dash, dash_pattern="on 1cm off 0.5cm on 0.25 cm off 0.5cm")    

```

 ![plotnew](https://global.discourse-cdn.com/julialang/original/3X/2/c/2c2796c909e69e3386e44182ef6656326d6d0274.png)

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [August 5, 2024, 5:10pm UTC](https://discourse.julialang.org/t/plots-jl-custom-linestyle-longer-dashes/117772/5 "2024-08-05T17:10:24Z")

</div>

@PeterSimon Excellent!

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [August 7, 2024, 10:42am UTC](https://discourse.julialang.org/t/plots-jl-custom-linestyle-longer-dashes/117772/6 "2024-08-07T10:42:18Z")

</div>

@PeterSimon Would we be able to fix the legend so that the representative strokes before the corresponding labels have a wider space, so as to distinguish between the first, the fifth and the sixth strokes (not only in color, but in the pattern itself) in the legend itself?

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [August 7, 2024, 4:17pm UTC](https://discourse.julialang.org/t/plots-jl-custom-linestyle-longer-dashes/117772/7 "2024-08-07T16:17:04Z")

</div>

The legend doesn’t seem to scale the dash lengths appropriately, so you have to choose lengths that are appropriate for the short width of the legend:

```julia
using Plots
pgfplotsx()

x = 0:10
y = ones(length(x))
default(lw=2)
plot(x, 1*y, ls=:solid)
plot!(x, 2*y, ls=:dot)
plot!(x, 3*y, ls=:dash)
plot!(x, 4*y, ls=:dashdotdot)
plot!(x, 5*y, ls=:dash, dash_pattern="on 2mm off 1mm")    
plot!(x, 6*y, ls=:dash, dash_pattern="on 2mm off 1mm on 0.5 mm off 1mm")    

```

 ![plotnew2](https://global.discourse-cdn.com/julialang/original/3X/a/a/aafb755d6d1a46e07185ac8e9260123ab99d740c.png)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 7, 2024, 4:58pm UTC](https://discourse.julialang.org/t/plots-jl-custom-linestyle-longer-dashes/117772/8 "2024-08-07T16:58:04Z")

</div>

You could use layouts and `annotate!()` to get something like:

 ![Plots_pgfplotsx_dash_spacing_with_wide_legend](https://global.discourse-cdn.com/julialang/original/3X/5/d/5db9c69115b2087f7068c5ffcc7498d0392f40de.png)

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [August 7, 2024, 10:47pm UTC](https://discourse.julialang.org/t/plots-jl-custom-linestyle-longer-dashes/117772/9 "2024-08-07T22:47:16Z")

</div>

@rafel.guerra Could you elaborate a little bit more, particularly as concerns layouts? Thanks in advance

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 8, 2024, 6:47am UTC](https://discourse.julialang.org/t/plots-jl-custom-linestyle-longer-dashes/117772/10 "2024-08-08T06:47:16Z")

</div>

The Plots.jl layout macro (ex: `l = @layout [a{0.66w} b]`) allows plotting the data on the left subplot without a legend, and then plotting the long annotated lines on the right subplot (that second plot has no frame nor legend).
