Supressing some labels in legend or putting sampled markers

Ped*_*ira 5 julia plots.jl

I am using Julia 1.1.0 and want to do some plotting in it. I am using Plots and pyplot backend for that.

I have two data that I want to plot with the same line style and color, differing them by markers.

Here is an example:

using Plots
pyplot()
nf = 150;
freq = exp10.(range(2, stop=6.4, length=nf)); #logspace
foo1 = (freq*2 .- 3);
foo2 = (freq .- 10);
plot(freq, foo1, label="foo 1", line=(1, :black, :solid), xaxis=:log, yaxis=:log)
plot!(freq, foo2, label="foo 2", line=(1, :black, :solid), marker=(:circle, :black, 3, 0,5))
Run Code Online (Sandbox Code Playgroud)

情节 1

The problem is that the line with the markers gets convoluted. I did not find an argument to put a marker every n-th point (is there one?)

Then I tried to generate a plot with line and no markers using the full data, then a plot with only markers (no lines) with a sampled data, as follows

plot(freq, foo1, label="foo 1", line=(1, :black, :solid), xaxis=:log, yaxis=:log)
plot!(freq, foo2, line=(1, :black, :solid))
r = 1:10:nf;
plot!(freq[r], foo2[r], line=(false), marker=(:circle, :black, 3, 0,5))
plot!(freq[1:1:2], foo2[1:1:2], label="foo2", line=(1, :black, :solid), marker=(:circle, :black, 3, 0,5))
Run Code Online (Sandbox Code Playgroud)

情节 2

It has the looks that I want, but now the legends are messed up. How could I suppress labels y2 and y3 from the legend?

Ped*_*ira 7

我重新阅读文档,发现该属性label系列

系列的标签,出现在图例中。如果为空,则不添加图例条目。

因此,无论是否使用,将标签设为空字符串都""可以解决问题。label=falselabel=nothing

plot(freq, foo1, label="foo 1", line=(1, :black, :solid), xaxis=:log, yaxis=:log)
plot!(freq, foo2, line=(1, :black, :solid), label="")
r = 1:10:nf;
plot!(freq[r], foo2[r], line=(false), marker=(:circle, :black, 3, 0,5), label="")
plot!(freq[[1,11]], foo2[[1,11]], label="foo 2", line=(1, :black, :solid), marker=(:circle, :black, 3, 0,5))
Run Code Online (Sandbox Code Playgroud)

阴谋

  • 这似乎是一个糟糕的 API 选择。空字符串仍然是字符串,因此我希望该行显示在图例中,而没有任何文本。你的任何本能选择,甚至“label=[]”,都会更好。那好吧... (3认同)