在 Julia 中使用 PyPlot 图设置轴原点
Set axis origin using PyPlot plot in Julia
我对 Julia 完全陌生。我在 Julia 中使用 PyPlot 包,只是试图将我的 x 和 y 轴原点分别设置为 0 和 0。目前它只是根据我正在绘制的点的值选择原点的位置。
plot(x1,y1,".")
xlabel("X1")
ylabel("Y1")
title("First Line")
grid("on")
我已经尝试了以下方法,但它不起作用。
change matplotlib axis settings
在 Julia 中使用 PyPlot 不同于在 Python 中使用 matplotlib。您正在寻找相当于在轴上设置限制的 Julia。我发现 this useful github repo 可能对你有用。
以下是向 y 轴添加自定义限制的方法:
using PyPlot
x1 = rand(50, 1) .* 30 .+ 50
y1 = rand(50, 1) .* 30 .+ 100
plot(x1, y1)
# get the current axis argument of the plot
ax = gca()
# add new limits from 0 - 100
ax[:set_ylim]([0,100])
在 Julia 中使用 PyPlot 的语法与 Python 有点不同。
这是因为(目前)您不能使用 obj.f()
访问 Julia 中对象 obj
内的方法(函数)f()
,而 PyPlot 大量使用了该对象。
为了解决这个问题,Python 中的 obj.f()
被 Julia 中的 obj[:f]()
取代;注意 :
.
所以 Python 中的 ax.spines
变成
ax[:spines]
朱莉娅
正如其他发帖者所指出的,您必须先执行 ax = gca()
以将当前轴对象存储在变量 ax
.
中
如果您在 Julia REPL 或 Jupyter notebook 中执行此操作,您将看到
julia> ax = gca()
PyObject <matplotlib.axes._subplots.AxesSubplot object at 0x31f854550>
julia> ax[:spines]
Dict{Any,Any} with 4 entries:
"left" => PyObject <matplotlib.spines.Spine object at 0x31f854c10>
"bottom" => PyObject <matplotlib.spines.Spine object at 0x31f854f50>
"right" => PyObject <matplotlib.spines.Spine object at 0x31f854dd0>
"top" => PyObject <matplotlib.spines.Spine object at 0x31f86e110>
显示 ax[:spines]
是一个字典。 (我以前不知道这一点。这是进行交互式会话的优势——你可以直接问 Julia 你需要知道什么。这叫做 "introspection"。)
在原始问题中链接到的 Python 答案之后,您需要将 Python 中的 ax.spines['left'].set_position('zero')
替换为 Julia 中的以下内容:
ax[:spines]["left"][:set_position]("zero")
再次调用对象ax[:spines]["left"]
中的set_position
方法。另请注意,Julia 中的字符串必须具有 "
,而不是 '
.
我对 Julia 完全陌生。我在 Julia 中使用 PyPlot 包,只是试图将我的 x 和 y 轴原点分别设置为 0 和 0。目前它只是根据我正在绘制的点的值选择原点的位置。
plot(x1,y1,".")
xlabel("X1")
ylabel("Y1")
title("First Line")
grid("on")
我已经尝试了以下方法,但它不起作用。 change matplotlib axis settings
在 Julia 中使用 PyPlot 不同于在 Python 中使用 matplotlib。您正在寻找相当于在轴上设置限制的 Julia。我发现 this useful github repo 可能对你有用。
以下是向 y 轴添加自定义限制的方法:
using PyPlot
x1 = rand(50, 1) .* 30 .+ 50
y1 = rand(50, 1) .* 30 .+ 100
plot(x1, y1)
# get the current axis argument of the plot
ax = gca()
# add new limits from 0 - 100
ax[:set_ylim]([0,100])
在 Julia 中使用 PyPlot 的语法与 Python 有点不同。
这是因为(目前)您不能使用 obj.f()
访问 Julia 中对象 obj
内的方法(函数)f()
,而 PyPlot 大量使用了该对象。
为了解决这个问题,Python 中的 obj.f()
被 Julia 中的 obj[:f]()
取代;注意 :
.
所以 Python 中的 ax.spines
变成
ax[:spines]
朱莉娅
正如其他发帖者所指出的,您必须先执行 ax = gca()
以将当前轴对象存储在变量 ax
.
如果您在 Julia REPL 或 Jupyter notebook 中执行此操作,您将看到
julia> ax = gca()
PyObject <matplotlib.axes._subplots.AxesSubplot object at 0x31f854550>
julia> ax[:spines]
Dict{Any,Any} with 4 entries:
"left" => PyObject <matplotlib.spines.Spine object at 0x31f854c10>
"bottom" => PyObject <matplotlib.spines.Spine object at 0x31f854f50>
"right" => PyObject <matplotlib.spines.Spine object at 0x31f854dd0>
"top" => PyObject <matplotlib.spines.Spine object at 0x31f86e110>
显示 ax[:spines]
是一个字典。 (我以前不知道这一点。这是进行交互式会话的优势——你可以直接问 Julia 你需要知道什么。这叫做 "introspection"。)
在原始问题中链接到的 Python 答案之后,您需要将 Python 中的 ax.spines['left'].set_position('zero')
替换为 Julia 中的以下内容:
ax[:spines]["left"][:set_position]("zero")
再次调用对象ax[:spines]["left"]
中的set_position
方法。另请注意,Julia 中的字符串必须具有 "
,而不是 '
.