Plots.jl custom linestyle: longer dashes

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,
but with no simple solution.
Thanks in advance.

As documented in this PR you can do this with the PGFPlotsX back end:

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

1 Like

@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?

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:

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")    

2 Likes

@PeterSimon Excellent!

@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?

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:

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")    

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

1 Like

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

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).