Julia 如何绘制混合整数问题的结果?
我解决了一个线性双目标混合整数问题,我想画出结果。结果包括线和点。比如说Julia 如何绘制混合整数问题的结果?,julia,julia-jump,julia-studio,Julia,Julia Jump,Julia Studio,我解决了一个线性双目标混合整数问题,我想画出结果。结果包括线和点。比如说 list=[([0.0; 583000.0], 0), ([190670.0; 149600.0], 0), ([69686.0, 385000.0], 1), ([33296.0, 484000.0], 1), ([136554.0, 2.38075e5], 1), ([24556.0, 503800.0], 0), ([47462.0, 437800.0], 1), ([129686.0, 253000.0], 1),
list=[([0.0; 583000.0], 0), ([190670.0; 149600.0], 0), ([69686.0, 385000.0], 1), ([33296.0, 484000.0], 1), ([136554.0, 2.38075e5], 1), ([24556.0, 503800.0], 0), ([47462.0, 437800.0], 1), ([129686.0, 253000.0], 1), ([164278.0, 178200.0], 1)]
在此列表中,第三个点([69686.0,385000.0],1)
第二个元素1
确定该点通过一条线连接到前一个点([190670.0;149600.0],0)
通过一条线连接到第二个点
我将其编码如下:
using JuMP,Plots
list=[([0.0, 583000.0], 0), ([24556.0, 503800.0], 0), ([33296.0, 484000.0],1), ([47462.0, 437800.0], 1), ([69686.0, 385000.0], 1), ([129686.0, 253000.0], 1), ([136554.0, 23805.0], 1), ([164278.0, 178200.0], 1), ([190670.0, 149600.0], 0)]
x=zeros(1,1)
for i=1:size(list,1)
x=[x;list[i][1][1]]
end
row=1
x = x[setdiff(1:end, row), :]
y=zeros(1,1)
for i=1:size(list,1)
y=[y;list[i][1][2]]
end
row=1
y = y[setdiff(1:end, row), :]
for i=2:size(list,1)
if list[i][2]==0
plot(Int(x[i]),Int(y[i]),seriestype=:scatter)
plot(Int(x[i+1]),Int(y[i+1]),seriestype=:scatter)
end
if list[i][2]==1
plot(Int(x[i]),Int(y[i]))
plot(Int(x[i+1]),Int(y[i+1]))
end
end
但它没有起作用。你能帮帮我吗。
谢谢您可以简单地将每个线段的x和y值推送到两个单独的数组中,
x
和y
,在下面的代码中。在每个线段的值(即x1和x2或y1和y2)之后,将NaN
放入数组中。如果没有连接,这将阻止将线段连接到下一个线段。(例如,您看到的情况是1,然后是0)。最后,绘制(x,y)
下面的代码片段可以实现这一点。请注意,allx
和ally
用于保持所有点,而不管连接状态如何。您可能希望从中排除连接点x
和y
保存连接的线段
using Plots
x, y, allx, ally = Float64[], Float64[], Float64[], Float64[]
# iterate through list
for i = 1:length(list)-1
if list[i+1][2] == 1
# push x1 from the first point, x2 from the second and a `NaN`
push!(x, list[i][1][1], list[i+1][1][1], NaN)
# push y1, y2, `NaN`
push!(y, list[i][1][2], list[i+1][1][2], NaN)
end
push!(allx, list[i][1][1])
push!(ally, list[i][1][2])
end
push!(allx, list[end][1][1])
push!(ally, list[end][1][2])
# scatter all points
scatter(allx, ally)
# plot connections with markers
plot!(x, y, linewidth=2, color=:red, marker=:circle)
这应该会给你你想要的情节
如果您碰巧使用了
Gadfly.jl
而不是Plots.jl
,您可以使用
using Gadfly
connectedpoints = layer(x=x, y=y, Geom.path, Geom.point, Theme(default_color="red"))
allpoints = layer(x=allx, y=ally, Geom.point)
plot(connectedpoints, allpoints)
作为旁注,如果您计划在已创建的绘图对象上绘制另一个系列,则应使用
plot代码>而不是绘图