Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Supressing some labels in legend or putting sampled markers

Tags:

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

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

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

like image 938
Pedro H. N. Vieira Avatar asked May 08 '19 19:05

Pedro H. N. Vieira


Video Answer


1 Answers

I re-read the docs and found that for the attribute label of Series:

The label for a series, which appears in a legend. If empty, no legend entry is added.

So, making the label an empty string "" solves the problem, while label=false or label=nothing don't.

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

plot

like image 184
Pedro H. N. Vieira Avatar answered Oct 03 '22 23:10

Pedro H. N. Vieira