Skip to content

Commit a151b96

Browse files
committed
Add loc parameter to plotx
1 parent 4a8fec0 commit a151b96

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,11 @@ 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-
legend_size=10, yzoom=1.0, bottom=nothing, disp=false)
167+
legend_size=10, loc="best", 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.
170170
The optional parameter `legend_size` can be used to control the font size of the legend. The default value is 10 points.
171+
The optional parameter `loc` can be used to control the legend location. The default value is "best". Other common values include "upper right", "upper left", "lower right", "lower left", "center", etc.
171172
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.
172173

173174
### **n x m** Plot

src/ControlPlots.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mutable struct PlotX
1919
ylabels
2020
ysize
2121
legend_size
22+
loc
2223
yzoom
2324
xlims
2425
ylims
@@ -58,7 +59,7 @@ function plotxy(X, Y; xlabel="", ylabel="", xlims=nothing, ylims=nothing, ann=no
5859
plt.grid(which="major", color="#DDDDDD")
5960
plt.tight_layout()
6061
end
61-
PlotX(X, Y, nothing, xlabel, ylabel, ysize, nothing, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 3)
62+
PlotX(X, Y, nothing, xlabel, ylabel, ysize, nothing, nothing, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 3)
6263
end
6364

6465
function display(P::PlotX)
@@ -67,7 +68,7 @@ function display(P::PlotX)
6768
scatter=P.scatter, fig=P.fig, title=P.title, ysize=P.ysize, disp=true)
6869
elseif P.type == 2
6970
plotx(P.X, P.Y...; xlabel=P.xlabel, ylabels=P.ylabels, labels=P.labels, xlims=P.xlims, ylims=P.ylims, ann=P.ann,
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)
71+
scatter=P.scatter, fig=P.fig, title=P.title, ysize=P.ysize, legend_size=P.legend_size, loc=P.loc, yzoom=P.yzoom, bottom=P.bottom, disp=true)
7172
elseif P.type == 3
7273
plotxy(P.X, P.Y; xlabel=P.xlabel, ylabel=P.ylabels, xlims=P.xlims, ylims=P.ylims, ann=P.ann,
7374
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, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 4)
89+
PlotX(X, Ys, labels, xlabel, ylabel, ysize, nothing, 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, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 5)
129+
PlotX(X, [Y1, Y2], labels, xlabel, ylabels, ysize, nothing, 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, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 5)
189+
PlotX(X, [Y1, Y2], labels, xlabel, ylabels, ysize, nothing, 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, nothing, xlims, ylims, ann, scatter, title, fig, nothing, 1)
229+
PlotX(X, Y, nothing, xlabel, ylabel, ysize, nothing, 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, legend_size=10, yzoom=1.0, bottom=nothing, disp=false)
3+
scatter=false, title="", fig="", title="", ysize=14, legend_size=10, loc="best", yzoom=1.0, bottom=nothing, disp=false)
44
55
Create an oscilloscope like plot with multiple y axes and one x axis.
66
@@ -18,12 +18,13 @@ Create an oscilloscope like plot with multiple y axes and one x axis.
1818
- `title::String`: title
1919
- `ysize::Int`: y-axis label size
2020
- `legend_size::Int`: legend font size
21+
- `loc::String`: legend location (e.g., "best", "upper right", "lower left", etc.)
2122
- `yzoom::Float64`: y-axis zoom factor
2223
- `bottom::Float64`: bottom margin for the plot
2324
- `disp::Bool`: display plot or just return the PlotX struct
2425
"""
2526
function plotx(X, Y...; xlabel="time [s]", ylabels=nothing, labels=nothing, xlims=nothing, ylims=nothing, ann=nothing,
26-
scatter=false, title="", fig="", ysize=14, legend_size=10, yzoom=1.0, bottom=nothing, disp=false)
27+
scatter=false, title="", fig="", ysize=14, legend_size=10, loc="best", yzoom=1.0, bottom=nothing, disp=false)
2728
if disp
2829
len=length(Y)
2930
fig_ = plt.figure(fig, figsize=(8, len*2*yzoom))
@@ -69,7 +70,7 @@ function plotx(X, Y...; xlabel="time [s]", ylabels=nothing, labels=nothing, xlim
6970
plt.xlim(X[begin], X[end])
7071
plt.ylabel(ylbl; fontsize=ysize);
7172
if ! isnothing(lbl) && lbl != ""
72-
plt.legend(fontsize=legend_size)
73+
plt.legend(fontsize=legend_size, loc=loc)
7374
end
7475
plt.grid(true)
7576
plt.grid(which="major", color="#DDDDDD")
@@ -86,5 +87,5 @@ function plotx(X, Y...; xlabel="time [s]", ylabels=nothing, labels=nothing, xlim
8687
plt.tight_layout()
8788
end
8889
end
89-
PlotX(collect(X), Y, labels, xlabel, ylabels, ysize, legend_size, yzoom, xlims, ylims, ann, scatter, title, fig, bottom, 2)
90+
PlotX(collect(X), Y, labels, xlabel, ylabels, ysize, legend_size, loc, yzoom, xlims, ylims, ann, scatter, title, fig, bottom, 2)
9091
end

0 commit comments

Comments
 (0)