gpt4 book ai didi

plot - Julia:网格上分类数据的可视化

转载 作者:行者123 更新时间:2023-12-04 11:12:26 24 4
gpt4 key购买 nike

有时需要在规则网格上绘制分类值以显示它们如何覆盖某个区域。原则上,plot() 函数非常适合这种情况,但是有一个问题需要每次调整图标的大小以创建实体封面的错觉。更改图像的覆盖范围时,旧尺寸变得无关紧要,需要再次调整。有没有一种技术可以自动调整这个大小?

using Plots
using CategoricalArrays
a = [1, 2, 3, 1, 2, 3, 1, 2, 3]
b = [1, 1, 1, 2, 2, 2, 3, 3, 3]
c = CategoricalArray(["X", "X", "Y", "Z", "Y", "Y", "Z", "Y", "Z"])
plot(a, b, group = c, seriestype = :scatter, aspect_ratio = 1, markersize=90,
markershape=:square, markerstrokewidth=0.0, xlim = (0.5, 3.5), ylim = (0.5, 3.5))
结果在所有方面都很好,除了每次需要调整单元格的大小以使没有重叠区域或间隙时:
enter image description here
作为替代方案,我考虑了 heatmap(),但它对分类数据的工作非常奇怪,通过连续的值渐变为它们设置某种规模。我还没有遇到任何使用 heatmap() 会得到带有像 plot() 这样美丽图例的 map 的例子,所以我不确定在这里使用 heatmap() 是否正确。
a = b = [1, 2, 3]
c = CategoricalArray(["X" "X" "Y"; "Z" "Y" "Y"; "Z" "Y" "Z"])
heatmap(a, b, c)
enter image description here
也许还有一些方法可以自动设置plot()的单元格大小?

最佳答案

在 Plots.jl 中有多种方法可以创建这样的图。也许对你想要的最明显的解释是 shapes .对于这种方法,您还需要了解 how to group unconnected data within the same groups .基于形状的解决方案可能如下所示:

a = [1, 2, 3, 1, 2, 3, 1, 2, 3]
b = [1, 1, 1, 2, 2, 2, 3, 3, 3]
c = CategoricalArray(["X", "X", "Y", "Z", "Y", "Y", "Z", "Y", "Z"])

groups = Dict(cat => NTuple{2,Int}[] for cat in levels(c))
for (ca, cb, cat) in zip(a,b,c)
push!(groups[cat], (ca,cb))
end

w = 1
shapes = map(collect(groups)) do (cat, vals)
cat => mapreduce(vcat, vals) do (ca, cb)
[ca cb] .+ [-.5 -.5; .5 -.5; .5 .5; -.5 .5; -.5 -.5; NaN NaN]*w
end
end

p = plot(aspect_ratio=1)
for (cat, s) in sort(shapes;by=x->x[1])
plot!(s[:,1], s[:,2], label=cat, seriestype=:shape, linewidth=0)
end
enter image description here
大多数代码只是简单地移动数据,因此我们从分类值到指定所有顶点的矩阵中得到一个对向量,例如“X”:
"X" =>
12×2 Matrix{Float64}:
0.5 0.5
1.5 0.5
1.5 1.5
0.5 1.5
0.5 0.5
NaN NaN
1.5 0.5
2.5 0.5
2.5 1.5
1.5 1.5
1.5 0.5
NaN NaN
一个可能稍微简单的解决方案是“欺骗” Plots 以使用热图显示我们想要的内容,如下所示:
a = b = [1, 2, 3]
c = CategoricalArray(["X" "X" "Y"; "Z" "Y" "Y"; "Z" "Y" "Z"])
pal = palette(:default)
p = plot(aspect_ratio=1, size=(400,400))
heatmap!(a,b,c, c=pal, colorbar=false, clims=(1,length(pal)))
for cat in sort(collect(Set(c)))
plot!(
[], [], seriestype=:shape,
label=cat, color=pal[levelcode(cat)]
)
end
enter image description here

关于plot - Julia:网格上分类数据的可视化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67492506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com