gpt4 book ai didi

plot - 如何使用 Plots.jl 将特定的 x 刻度、标签和垂直线添加到绘图中?

转载 作者:行者123 更新时间:2023-12-03 14:56:54 25 4
gpt4 key购买 nike

我正在尝试在绘图中添加两条垂直线,在 x 轴上带有相应的刻度和标签,以突出显示重要点。

我找到了两种方法(后面的 A 和 B),但在第一种方法中,新的刻度/标签替换而不是附加旧的,而在第二种方法中,标签打印在某种不一致的位置(取决于范围)和无法将它们带到我想要的地方:

using Plots, StatPlots, DataFrames
pyplot()
df = DataFrame(a = 1:10, b = 10*rand(10), c = 10 * rand(10))
f = Plots.font("DejaVu Sans", 10)
@df df plot(:a, [:b :c], label=["serie a" "serie b"], xtickfont=f, ytickfont=f, legendfont=f, guidefont=f, titlefont=f)

答:
plot!([5,7], seriestype="vline", xticks = ([5,7],["\$\\bar \\delta \$","\$\\bar \\gamma \$"]), label="")

这显示如下:
screenshot of A

乙:
plot!([5], seriestype="vline", label="")
annotate!(5, 0, text("\$ \\bar \\delta \$",f, :bottom, :left))
plot!([7], seriestype="vline", label="")
annotate!(7, 0, text("\$\\bar \\gamma \$",f, :top)

这显示如下:
screenshot of B

最佳答案

一种解决方案是获取 xticks从您的原始图并将它们与 xticks 合并你要的那个。下面是一个接近你的例子的 MWE

using Plots, DataFrames
df = DataFrame(a = 1:10, b = 10rand(10), c = 10rand(10))
plt = plot(df.a, [df.b df.c], label=["serie a" "serie b"])
old_xticks = xticks(plt[1]) # grab xticks of the 1st subplot
new_xticks = ([5, 8], ["\$\\bar \\delta\$", "\$\\bar \\gamma\$"])
vline!(new_xticks[1])
keep_indices = findall(x -> all(x .≠ new_xticks[1]), old_xticks[1])
merged_xticks = (old_xticks[1][keep_indices] ∪ new_xticks[1], old_xticks[2][keep_indices] ∪ new_xticks[2])
xticks!(merged_xticks)
并产生
enter image description here
请注意,我故意移动了您的索引 78所以我必须小心地删除那里的原始 xtick,这样这个解决方案就更通用了。
另请注意,滴答获取函数类似于 xticksPlots.jl v1.13.0 中添加.

关于plot - 如何使用 Plots.jl 将特定的 x 刻度、标签和垂直线添加到绘图中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51651364/

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