Skip to content

Commit 4a8fec0

Browse files
committed
Add param legend_size to plotx
1 parent 0a4f3d4 commit 4a8fec0

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,10 @@ Full function signature:
164164
```julia
165165
plotx(X, Y...; xlabel="time [s]", ylabels=nothing, labels=nothing, xlims=nothing,
166166
ylims=nothing, ann=nothing, scatter=false, fig="", title="", ysize=14,
167-
yzoom=1.0, bottom=nothing, disp=false)
167+
legend_size=10, yzoom=1.0, bottom=nothing, disp=false)
168168
```
169169
The optional parameter `ysize` can be used to change the size of the y-axis labels. The default value is 14 points.
170+
The optional parameter `legend_size` can be used to control the font size of the legend. The default value is 10 points.
170171
The optional parameter `bottom` can be used to control the bottom margin of the plot when using `plt.tight_layout(rect=[0, bottom, 1, 1])`. When `nothing`, the default tight layout is used.
171172

172173
### **n x m** Plot

src/ControlPlots.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mutable struct PlotX
1818
xlabel
1919
ylabels
2020
ysize
21+
legend_size
2122
yzoom
2223
xlims
2324
ylims
@@ -57,7 +58,7 @@ function plotxy(X, Y; xlabel="", ylabel="", xlims=nothing, ylims=nothing, ann=no
5758
plt.grid(which="major", color="#DDDDDD")
5859
plt.tight_layout()
5960
end
60-
PlotX(X, Y, nothing, xlabel, ylabel, ysize, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 3)
61+
PlotX(X, Y, nothing, xlabel, ylabel, ysize, nothing, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 3)
6162
end
6263

6364
function display(P::PlotX)
@@ -66,7 +67,7 @@ function display(P::PlotX)
6667
scatter=P.scatter, fig=P.fig, title=P.title, ysize=P.ysize, disp=true)
6768
elseif P.type == 2
6869
plotx(P.X, P.Y...; xlabel=P.xlabel, ylabels=P.ylabels, labels=P.labels, xlims=P.xlims, ylims=P.ylims, ann=P.ann,
69-
scatter=P.scatter, fig=P.fig, title=P.title, ysize=P.ysize, yzoom=P.yzoom, bottom=P.bottom, disp=true)
70+
scatter=P.scatter, fig=P.fig, title=P.title, ysize=P.ysize, legend_size=P.legend_size, yzoom=P.yzoom, bottom=P.bottom, disp=true)
7071
elseif P.type == 3
7172
plotxy(P.X, P.Y; xlabel=P.xlabel, ylabel=P.ylabels, xlims=P.xlims, ylims=P.ylims, ann=P.ann,
7273
scatter=P.scatter, fig=P.fig, title=P.title, ysize=P.ysize, disp=true)

src/plot.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function plot(X, Ys::AbstractVector{<:Union{AbstractVector, Tuple}}; xlabel="",
8686
else
8787
println("OK")
8888
end
89-
PlotX(X, Ys, labels, xlabel, ylabel, ysize, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 4)
89+
PlotX(X, Ys, labels, xlabel, ylabel, ysize, nothing, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 4)
9090
end
9191

9292
function plot(X, Y1::AbstractVector{<:AbstractVector}, Y2::AbstractVector{<:Number};
@@ -126,7 +126,7 @@ function plot(X, Y1::AbstractVector{<:AbstractVector}, Y2::AbstractVector{<:Numb
126126
else
127127
println("OK")
128128
end
129-
PlotX(X, [Y1, Y2], labels, xlabel, ylabels, ysize, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 5)
129+
PlotX(X, [Y1, Y2], labels, xlabel, ylabels, ysize, nothing, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 5)
130130
end
131131

132132
end
@@ -186,7 +186,7 @@ function plot(X, Y1::AbstractVector{<:Number}, Y2::AbstractVector{<:Number};
186186
else
187187
println("OK")
188188
end
189-
PlotX(X, [Y1, Y2], labels, xlabel, ylabels, ysize, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 5)
189+
PlotX(X, [Y1, Y2], labels, xlabel, ylabels, ysize, nothing, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 5)
190190
end
191191

192192
function plot(Y::AbstractVector{<:Number}; xlabel="", ylabel="", title="", fig="", ysize=14, disp=false)
@@ -226,6 +226,6 @@ function plot(X, Y::AbstractVector{<:Number}; xlabel="", ylabel="", xlims=nothin
226226
plt.grid(which="major", color="#DDDDDD")
227227
plt.tight_layout()
228228
end
229-
PlotX(X, Y, nothing, xlabel, ylabel, ysize, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 1)
229+
PlotX(X, Y, nothing, xlabel, ylabel, ysize, nothing, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 1)
230230
end
231231

src/plotx.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
plotx(X, Y...; xlabel="time [s]", ylabels=nothing, labels=nothing, xlims=nothing, ylims=nothing, ann=nothing,
3-
scatter=false, title="", fig="", title="", ysize=14, yzoom=1.0, bottom=nothing, disp=false)
3+
scatter=false, title="", fig="", title="", ysize=14, legend_size=10, yzoom=1.0, bottom=nothing, disp=false)
44
55
Create an oscilloscope like plot with multiple y axes and one x axis.
66
@@ -17,12 +17,13 @@ Create an oscilloscope like plot with multiple y axes and one x axis.
1717
- `fig::String`: figure name
1818
- `title::String`: title
1919
- `ysize::Int`: y-axis label size
20+
- `legend_size::Int`: legend font size
2021
- `yzoom::Float64`: y-axis zoom factor
2122
- `bottom::Float64`: bottom margin for the plot
2223
- `disp::Bool`: display plot or just return the PlotX struct
2324
"""
2425
function plotx(X, Y...; xlabel="time [s]", ylabels=nothing, labels=nothing, xlims=nothing, ylims=nothing, ann=nothing,
25-
scatter=false, title="", fig="", ysize=14, yzoom=1.0, bottom=nothing, disp=false)
26+
scatter=false, title="", fig="", ysize=14, legend_size=10, yzoom=1.0, bottom=nothing, disp=false)
2627
if disp
2728
len=length(Y)
2829
fig_ = plt.figure(fig, figsize=(8, len*2*yzoom))
@@ -68,7 +69,7 @@ function plotx(X, Y...; xlabel="time [s]", ylabels=nothing, labels=nothing, xlim
6869
plt.xlim(X[begin], X[end])
6970
plt.ylabel(ylbl; fontsize=ysize);
7071
if ! isnothing(lbl) && lbl != ""
71-
plt.legend()
72+
plt.legend(fontsize=legend_size)
7273
end
7374
plt.grid(true)
7475
plt.grid(which="major", color="#DDDDDD")
@@ -85,5 +86,5 @@ function plotx(X, Y...; xlabel="time [s]", ylabels=nothing, labels=nothing, xlim
8586
plt.tight_layout()
8687
end
8788
end
88-
PlotX(collect(X), Y, labels, xlabel, ylabels, ysize, yzoom, xlims, ylims, ann, scatter, title, fig, bottom, 2)
89+
PlotX(collect(X), Y, labels, xlabel, ylabels, ysize, legend_size, yzoom, xlims, ylims, ann, scatter, title, fig, bottom, 2)
8990
end

test_legend_size.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using ControlPlots
2+
3+
# Test the legend_size parameter
4+
T = 0:0.1:2π
5+
Y1 = sin.(T)
6+
Y2 = cos.(T)
7+
8+
# Test plotx with default legend_size
9+
p1 = plotx(T, [Y1, Y2]; ylabels=["Channel 1"], labels=[["sin", "cos"]], fig="test1", title="Default legend size")
10+
display(p1)
11+
12+
# Test plotx with custom legend_size
13+
p2 = plotx(T, [Y1, Y2]; ylabels=["Channel 1"], labels=[["sin", "cos"]], fig="test2", title="Large legend size", legend_size=16)
14+
display(p2)
15+
16+
println("legend_size parameter value in p1: ", p1.legend_size)
17+
println("legend_size parameter value in p2: ", p2.legend_size)

0 commit comments

Comments
 (0)