【问题标题】:How to fix aspect ratio with linked axes?如何使用链接轴固定纵横比?
【发布时间】:2022-08-16 11:09:35
【问题描述】:

我正在尝试制作具有边际密度的散点图,并让散点图具有固定的 x 和 y 限制,纵横比等于 1。但是,我似乎找不到轴链接、轴限制的组合,和适合这个的 colsize。

这很接近,但请注意散点图没有相等的轴:

using CairoMakie

n = 3000
r = randn(2,3000)
x = @view r[1,:]
y = @view r[2,:]

fig = Figure(resolution = (1000, 1000), font = \"sans\", fontsize = 20)
ax1 = (Axis(fig[1, 1]))
density!(ax1, y; bins = 20, color = :orange, strokewidth = 1,
    strokecolor = :black, label = \"20 bins\", aspect=1)

ax3 = Axis(fig[2, 1]; xlabel = \"value\", ylabel = \"counts\")
ax4 = Axis(fig[2, 2]; xlabel = \"value\", ylabel = \"counts\")
scatter!(ax3, x, y; markersize = 4, color = :black,label=\"samples\")
axislegend(ax3; position = :rt)

density!(ax4, x; label = \"default\",direction=:y)
rowsize!(fig.layout, 2, Auto(3))
colsize!(fig.layout, 1, Auto(3))
xlims!(ax3,-4,4)
ylims!(ax3,-4,4)
linkxaxes!(ax1, ax3)
linkyaxes!(ax3, ax4)


fig

产生:

这是这篇文章的后续问题:Change the size of a sub-figure?

  • 只需将 aspect_ratio=1 添加到 scatter! 图中?
  • @MikaelÖhman,是的,看起来它有效(我有aspect=1 而不是aspect_ratio=1)你想创建一个答案来接受吗?此外,也许对这两个 kwargs 之间差异的快速解释也有助于理解。

标签: plot julia makie.jl


【解决方案1】:

您需要将aspect_ratio=1 添加到散点图中(归功于@mikael-Öhman):

using CairoMakie

n = 3000
r = randn(2,3000)
x = @view r[1,:]
y = @view r[2,:]

fig = Figure(resolution = (1000, 1000), font = "sans", fontsize = 20)
ax1 = (Axis(fig[1, 1]))
density!(ax1, y; bins = 20, color = :orange, strokewidth = 1,
    strokecolor = :black, label = "20 bins", aspect=1,aspect_ratio=1)

ax3 = Axis(fig[2, 1]; xlabel = "value", ylabel = "counts")
ax4 = Axis(fig[2, 2]; xlabel = "value", ylabel = "counts")
scatter!(ax3, x, y; markersize = 4, color = :black,label="samples")
axislegend(ax3; position = :rt)

density!(ax4, x; label = "default",direction=:y)
rowsize!(fig.layout, 2, Auto(3))
colsize!(fig.layout, 1, Auto(3))
xlims!(ax3,-4,4)
ylims!(ax3,-4,4)
linkxaxes!(ax1, ax3)
linkyaxes!(ax3, ax4)


fig

【讨论】:

    猜你喜欢
    • 2018-11-10
    • 2011-10-26
    • 2021-10-29
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 2015-12-02
    相关资源
    最近更新 更多