子图的坐标轴标签

huangapple go评论97阅读模式
英文:

Axis labeling for subplots

问题

以下是翻译好的部分:

"The following code generates this image 子图的坐标轴标签"

以下代码生成了这张图片 子图的坐标轴标签

"I want the "y"-axis label to be "Space" and the "x"-axis label to be "time" for the left subplot. However, I am failing to achieve this. Why does my plotting code not do as I desire?"

我想要左子图的“y”轴标签为“Space”,“x”轴标签为“time”。然而,我未能实现这一目标。为什么我的绘图代码没有按照我的要求执行?

  1. p2 = contourf(sol.t,x,z, xlabel="Time", ylabel="Space")
  2. plt = plot(p1,p2,layout=(1,2), size=(1200,800))
  1. p2 = contourf(sol.t, x, z, xlabel="Time", ylabel="Space")
  2. plt = plot(p1, p2, layout=(1, 2), size=(1200, 800))
  1. plotlyjs()
  2. N₁=31 # Number of waveguides / size of solution vector
  3. γ=1 # Nonlinear term strength parameter
  4. h=1 # Grid spacing
  5. centerGrid = (N₁-1)/2;
  6. x = -centerGrid:centerGrid;
  7. # Coefficient matrix of second-order centered-difference operator (δ²u)ₙ
  8. M = spdiagm(-1 => fill(1, N₁-1), 0 => fill(-2, N₁), 1 => fill(1, N₁-1))
  9. M[N₁, 1] = 1; # Periodic boundary conditions
  10. M[1, N₁] = 1;
  11. # RHS of DNLS. The solution vector u is a N₁x1 complex vector
  12. g₁(u, p, t) = 1 * im * (p[1] * M * u + @.(γ * ((abs(u))^2) .* u) )
  13. # Julia is explicitly typed (e.g., cannot have Int and Complex in the same array) and so we must convert the object containing the initial data to be complex
  14. u0 = Complex.(sech.(x))
  15. tspan = (0.0, 200)
  16. prob = ODEProblem(g₁, u0, tspan, [h])
  17. sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)
  18. z= [abs(sol.u[i][j])^2 for j=1:N₁, i=1:size(sol)[2]] # |u|²;
  19. p1 = surface(sol.t, x, z, xlabel="Time", ylabel="Space", zlabel="|u|²", colorbar=false)
  20. p2 = contourf(sol.t, x, z, xlabel="Time", ylabel="Space")
  21. plt = plot(p1, p2, layout=(1, 2), size=(1200, 800))
英文:

The following code generates this image 子图的坐标轴标签

I want the "y"-axis label to be "Space" and the "x"-axis label to be "time" for the left subplot. However, I am failing to achieve this. Why does my plotting code not do as I desire?

  1. p1 = surface(sol.t, x, z, xlabel="Time", ylabel="Space", zlabel="|u|²", colorbar = false)
  2. p2 = contourf(sol.t,x,z, xlabel="Time", ylabel="Space")
  3. plt = plot(p1,p2,layout=(1,2), size=(1200,800))

  1. using DifferentialEquations, LinearAlgebra, Plots, SparseArrays
  2. plotlyjs()
  3. N₁=31 # Number of waveguides / size of solution vector
  4. γ=1 # Nonlinear term strength parameter
  5. h=1 # Grid spacing
  6. centerGrid = (N₁-1)/2;
  7. x = -centerGrid:centerGrid;
  8. # Coefficient matrix of second-order centered-difference operator (δ²u)ₙ
  9. M = spdiagm(-1 => fill(1,N₁-1), 0 => fill(-2,N₁), 1 => fill(1,N₁-1))
  10. M[N₁,1] = 1; # Periodic boundary conditions
  11. M[1,N₁] = 1;
  12. # RHS of DNLS. The solution vector u is a N₁x1 complex vector
  13. g₁(u,p,t) = 1*im*(p[1]*M*u + @.(γ*((abs(u))^2).*u) )
  14. # Julia is explicitly typed (e.g, cannot have Int and Complex in same array) and so we must convert the object containing the initial data to be complex
  15. u0 = Complex.(sech.(x))
  16. tspan = (0.0,200)
  17. prob = ODEProblem(g₁,u0,tspan, [h])
  18. sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)
  19. z= [abs(sol.u[i][j])^2 for j=1:N₁, i=1:size(sol)[2]] # |u|²
  20. p1 = surface(sol.t, x, z, xlabel="Time", ylabel="Space", zlabel="|u|²", colorbar = false)
  21. p2 = contourf(sol.t,x,z, xlabel="Time", ylabel="Space")
  22. plt = plot(p1,p2,layout=(1,2), size=(1200,800))

答案1

得分: 0

自定义3D图的轴标签在Plots和plotlyjs()后端上不起作用,只有在GR后端上才会显示您的标签。

您可以尝试使用PLotlyJS.jl而不是Plots.jl的这个版本:

  1. fig = make_subplots(rows=1, cols=2, specs=[Spec(kind="scene"), Spec(kind="xy")],
  2. horizontal_spacing=-0.1, column_widths=[0.65, 0.35])
  3. add_trace!(fig, PlotlyJS.surface(x=sol.t, y=collect(x), z=z', showscale=false), row=1, col=1)
  4. add_trace!(fig, PlotlyJS.contour(x=sol.t, y=collect(x), z=z), row=1, col=2)
  5. relayout!(fig, template=templates["plotly_white"], font_size=11,
  6. width=1000, height=600, scene=attr(xaxis_title="时间", yaxis_title="空间",
  7. zaxis_title="|u|²", camera_eye=attr(x=1.8, y=1.8, z=1)),
  8. xaxis2_title="时间", yaxis2_title="空间", margin_l=15)
  9. display(fig)

子图的坐标轴标签

  1. <details>
  2. <summary>英文:</summary>
  3. Setting custom axis labels for 3d plots doesn&#39;t work with Plots and plotlyjs() backend. Only with GR backend your labels are displayed.
  4. You can try this version using PLotlyJS.jl instead Plots.jl:

fig=make_subplots(rows=1, cols=2, specs =[Spec(kind="scene") Spec(kind="xy")],
horizontal_spacing=-0.1, column_widths=[0.65, 0.35])
add_trace!(fig, PlotlyJS.surface(x=sol.t, y=collect(x), z=z', showscale=false), row=1, col=1)
add_trace!(fig, PlotlyJS.contour(x=sol.t, y=collect(x), z=z), row=1, col=2)
relayout!(fig, template=templates["plotly_white"], font_size=11,
width=1000, height=600, scene=attr(xaxis_title="Time", yaxis_title="Space",
zaxis_title="|u|²", camera_eye=attr(x=1.8, y=1.8, z=1)),
xaxis2_title="Time", yaxis2_title="Space", margin_l=15)
display(fig)

  1. [![enter image description here][1]][1]
  2. [1]: https://i.stack.imgur.com/UTjNF.png
  3. </details>

huangapple
  • 本文由 发表于 2023年6月1日 23:13:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定