Julia绘图问题:标签重叠和LaTeXStings扩展

问题描述 投票:2回答:1

我在下面的示例图中遇到了两个相互排斥的问题。

  1. y轴标签和y-tick标签相互重叠。我无法找到移动这两个标签的方法。我希望图的大小能够扩大以适应必要的间距。
  2. 我希望y轴的标签是sigma,并加上一个下标theta。看来Unicode不允许使用下标θ。我希望通过使用 LaTeXStrings 来解决这个限制。然而,我无法让LatexString在图上扩展成适当的形式。请注意,问题#1发生在有无LatexString作为ylabel的情况下,而且LatexString的扩展对其他字符串值也不起作用。

下面提供了样本图、代码和数据来重现。我在Windows上使用带有Plotly后台的Juno编辑器。

Sample plot showing y-axis issues

using Plots, DelimitedFiles, LaTeXStrings
plotly(linewidth=3,titlefont=18,legendfont=16,guidefont=18,tickfont=14,formatter=:plain)
data=readdlm("hoopstress.txt",skipstart=1)
r=data[:,2]
σθ=data[:,end-1:end]
plot(r,σθ,label=["Simplified Vessel" "Full Vessel"],xlabel="r",ylabel=L"\sigma_\theta")
    Length  Simple  Full
1   0.  53280   56859
2   9.4e-2  52158   55405
3   0.1875  51036   53951
4   0.28125 49915   52498
5   0.375   48793   51044
6   0.46875 47671   49590
7   0.5625  46550   48136
8   0.65625 45428   46682
9   0.75    44307   45228
10  0.84375 43185   43774
11  0.9375  42063   42320
12  1.0312  40942   40866
13  1.125   39883   39411
14  1.2187  39256   38780
15  1.3125  38629   38150
16  1.4062  38002   37519
17  1.5 37375   36888
18  1.5938  36748   36257
19  1.6875  36121   35627
20  1.7813  35494   34996
21  1.875   34867   34365
22  1.9688  34239   33735
23  2.0625  33612   33104
24  2.1562  32985   32473
25  2.25    32389   31842
26  2.3437  31998   31441
27  2.4375  31607   31039
28  2.5312  31216   30637
29  2.625   30825   30235
30  2.7187  30434   29833
31  2.8125  30043   29431
32  2.9062  29652   29029
33  3.  29261   28628
34  3.0938  28870   28226
35  3.1875  28479   27824
36  3.2813  28088   27422
37  3.375   27714   27020
38  3.4688  27452   26693
39  3.5625  27190   26367
40  3.6563  26927   26040
41  3.75    26665   25714
42  3.8438  26403   25387
43  3.9375  26141   25061
44  4.0313  25879   24734
45  4.125   25617   24408
46  4.2187  25354   24081
47  4.3125  25092   23755
48  4.4062  24830   23428
49  4.5 24568   23102
latex julia plotly plots.jl juno-ide
1个回答
1
投票

我提交了一个 公关 来解决这个问题,这个问题刚刚被合并到主程序中 :-)

所以,在未来你将能够做的是

plotly(linewidth=3,titlefont=18,legendfont=16,guidefont=18,tickfont=14,formatter=:plain)
r = 1:10
σθ = [100000 ./ (1:10) .+ 10000, 100000 ./ (1:10) .- 1000 .+ 10000]
plot(r , σθ, label=["Simplified Vessel" "Full Vessel"],
    xlabel = "r",
    ylabel = L"\sigma_\theta\\\color{white}.",
    yformatter = :plain,
    include_mathjax = "cdn",
    extra_kwargs = :plot)

如果你已经连接到互联网。

对于本地使用,如果安装了Conda和IJulia,并且按照我之前的回答复制了TeX字体,你可以做如下操作。

import Conda.ROOTENV
function local_mathjax()
    joinpath(ROOTENV, "Lib", "site-packages", "notebook", "static", "components", "MathJax",
            "MathJax.js?config=TeX-AMS-MML_HTMLorMML-full")
end

plotly(linewidth=3,titlefont=18,legendfont=16,guidefont=18,tickfont=14,formatter=:plain)
r = 1:10
σθ = [100000 ./ (1:10) .+ 10000, 100000 ./ (1:10) .- 1000 .+ 10000]
plot(r , σθ, label=["Simplified Vessel" "Full Vessel"],
    xlabel = "r",
    ylabel = L"\sigma_\theta\\\color{white}.",
    yformatter = :plain,
    include_mathjax = local_mathjax(),
    extra_kwargs = :plot)

Y轴数字的格式化现在是通过以下方式实现的 yformatter = :plain 而y-title中的空格是通过添加一行带白色格式的点来实现的。

当然,你也可以将mathjax拷贝到你的系统中的任何地方,并将链接放在了 includ_mathjax 参数。

更多的使用细节可以在PR.Happy coding中找到。


1
投票

Latex的输出似乎是 目前在Plotly中已经中断.我读到,对一些人来说,这取决于浏览器。我无法让它在Edge和Chrome上工作。

关于重叠,很不幸,这是 plotly 本身的标准设置。然而,plotly 和 Julia 包 Plotly 提供 yaxis.automargin = true 如果你想要额外的空间,你可以通过在你的标题上添加一个非空行来实现,这可以通过添加html代码来实现。<br>&nbsp;下面我提供了一个示例代码来实现这一点,在 Plotly. (目前,该委员会的目标是: plotly() 后端 Plots不支持传递后台特定参数。但是有 工作中 来实现这一目标。)

using Plotly

trace1 = Plotly.scatter(Dict(
  "x" => [1, 2, 3, 4],
  "y" => [1, 4, 9, 16] .* 1000000,
  "name" => L"\alpha_{1c} = 352 \pm 11 \text{ km s}^{-1}",
  "type" => "scatter")
)

trace2 = Plotly.scatter(Dict(
  "x" => [1, 2, 3, 4],
  "y" => [0.5, 2, 4.5, 8] .* 1000000,
  "name" => L"\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}",
  "type" => "scatter")
)

data = [trace1, trace2]

# standard layout (shortens numbers by SI prefix)
layout1 = Layout(xaxis = attr(title = L"\sqrt{(n_\text{c}(t|{T_\text{early}}))}"),
                 yaxis = attr(title = L"$d, r \text{ (solar radius)}")
)

# sets the number format to "0", i.e. without prefixing
layout2 = Layout(xaxis = attr(title = L"\sqrt{(n_\text{c}(t|{T_\text{early}}))}"),
                 yaxis = attr(title = L"$d, r \text{ (solar radius)}",
                             tickformat = "0")
)

# forces automargin
layout3 = Layout(xaxis = attr(title = L"\sqrt{(n_\text{c}(t|{T_\text{early}}))}"),
                 yaxis = attr(title = L"$d, r \text{ (solar radius)}",
                             tickformat = "0",
                             automargin = true)
)

# adds an additional space by adding a line to the title
# note that Plotly seems to accept html code ...
layout4 = Layout(xaxis = attr(title = L"\sqrt{(n_\text{c}(t|{T_\text{early}}))}"),
                 yaxis = attr(title = L"$d, r \text{ (solar radius)}<br>&nbsp;",
                              tickformat = "0",
                              automargin = true)
)

p1 = Plotly.plot(data, layout1)
p2 = Plotly.plot(data, layout2)
p3 = Plotly.plot(data, layout3)
p4 = Plotly.plot(data, layout4)

Layout 1Layout 2Layout 3Layout 4


0
投票

PyPlot Output

切换到 PyPlot 后台直接解决了这两个问题。

切换到 GR 后台修复了LaTeX的问题,但使标签重叠的情况更加严重。

我在使用 Plotly 后端通过添加 using Plots.PlotMeasures 到脚本的顶部,并添加 left_margin=80px 对任一 plotplotly 语句。然而,结果并不真正令人满意。增加的页边距并没有影响到标签的间距。GR 后台。

我还是想找一个直接修改情节标签定位的方案,因为plotly的交互性更好。


0
投票

目前plotly没有提供mathjax环境,但Jupyter提供了。然而,Jupyter默认不安装TeX字体,而TeX字体现在似乎是Mathjax的默认字体。

为了解决这个问题,你必须将原mathjax发行版复制到jupyter保存静态文件的目录下。

  • 下载 MathJax-2.7.7.zip
  • 抄袭 jax-文件夹,从MathJax-2.7.7.7.zip到 C:\Users\<username>\.julia\conda\3\Lib\site-packages\notebook\static\components\MathJax 或您系统中存储文件的任何地方。请注意,可能会有更多的 MathJax 文件夹,例如:MathJax,但只有一个才是服务器的真正来源。C:\Users\<username>\.julia\conda\3\pkgs\notebook-6.0.3-py36_0\Lib\site-packages\notebook\static\components\MathJax但只有一个才是服务器的真正来源。

现在你可以使用Plotly

using Plotly

trace1 = Plotly.scatter(Dict(
  "x" => [1, 2, 3, 4],
  "y" => [1, 4, 9, 16] .* 1000000,
  "name" => L"\alpha_{1c} = 352 \pm 11 \text{ km s}^{-1}",
  "type" => "scatter")
)

trace2 = Plotly.scatter(Dict(
  "x" => [1, 2, 3, 4],
  "y" => [0.5, 2, 4.5, 8] .* 1000000,
  "name" => L"\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}",
  "type" => "scatter")
)

data = [trace1, trace2]


layout = Layout(xaxis = attr(title = L"\sqrt{(n_\text{c}(t|{T_\text{early}}))}"),
                 yaxis = attr(title = L"\sigma_\theta\\?",
                              tickformat = "0",
                              automargin = true)
)

p = Plotly.plot(data, layout)

LaTeX in Plotly

(我找不到LaTeX语法中断行的快速解决方案。只有当我添加一个字符时,换行才会被保留。也许,其他人可以在这里做出贡献;-) )

或剧情。

using Plots, LaTeXStrings

plotly()
Plots.plot(1:4, [[1,4,9,16], [0.5, 2, 4.5, 8]], 
    labels = [L"\alpha_{1c} = 352 \pm 11 \text{ km s}^{-1}" L"\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}"],
    xlabel = L"\sqrt{(n_\text{c}(t|{T_\text{early}}))}",
    ylabel = L"d, r \text{ (solar radius)}"
    )

LaTeX with plotly backend of Plots

为了使它在Juno中工作,请确保以下几点: Conda 软件包安装完毕,并按上述方法复制mathjax字体。然后进行以下定义。

import Plots.plotly_html_head
import Conda.ROOTENV

function mathjax()
    mathjaxdir = joinpath(ROOTENV, "Lib\\site-packages\\notebook\\static\\components\\MathJax")
    mathjaxfile =  joinpath(mathjaxdir, "MathJax.js?config=TeX-AMS-MML_HTMLorMML-full")
    return """<script type="text/javascript" src="file://$mathjaxfile"></script>"""
end

function plotly_html_head(plt::Plots.Plot)
    local_file = ("file://" * Plots.plotly_local_file_path)
    plotly = Plots.use_local_dependencies[] ? Plots.local_file : Plots.plotly_remote_file_path
    if Plots.isijulia() && !Plots.ijulia_initialized[]
        # using requirejs seems to be key to load a js depency in IJulia!
        # https://requirejs.org/docs/start.html
        # https://github.com/JuliaLang/IJulia.jl/issues/345
        display("text/html", """
        <script type="text/javascript">
        requirejs([$(repr(plotly))], function(p) {
        window.Plotly = p
        });
        </script>
        """)
        ijulia_initialized[] = true
    end
    return """$(mathjax())
    <script src=$(repr(plotly))></script>"""
end
© www.soinside.com 2019 - 2024. All rights reserved.