【问题标题】:How to add patches to a figure using Julia's PyPlot.jl如何使用 Julia 的 PyPlot.jl 为图形添加补丁
【发布时间】:2017-12-26 01:36:27
【问题描述】:

例子:

using PyPlot
fig = gcf(); ax0=subplot(2,2,2)
ax1 = subplot(2,2,4)
ax0tr = ax0[:transAxes]; ax1tr = ax1[:transAxes] 
figtr = fig[:transFigure]
# 2. Transform arroww start point from axis 0 to figure coordinates
ptB = figtr[:transform](ax0tr[:transform]((20., -0.5)))
# 3. Transform arroww end point from axis 1 to figure coordinates
ptE = figtr[:transform](ax1tr[:transform]((20., 1.)))
# 4. Create the patch
arroww = matplotlib[:patches][:FancyArrowPatch](
ptB, ptE, transform=figtr,  # Place arroww in figure coord system
fc = "C0", alpha = 0.25, connectionstyle="arc3,rad=0.2",
arrowstyle="simple",
mutation_scale = 40.0)
# 5. Add patch to list of objects to draw onto the figure
push!(fig[:patches], arroww)
fig[:show]()

如何将补丁对象添加到图形并在图形上实际看到它?这行不通。它不会抛出任何错误,但我看不到任何箭头。

(我也不能使用函数arrow,因为我想创建一个从子图到子图的箭头)。

【问题讨论】:

  • 这真的是 Julia 代码吗?我从 Plots 和 PyPlot 中看到的样本看起来完全不同
  • 我不太确定你所说的“Julia”代码是什么意思。介意解释吗?此代码在 Julia 控制台中 100% 运行,所有命令均不返回错误,所有命令都为您提供您所期望的:对象或方法等(来自 PyCall 的 Python 类型)。据我所知,这是有效的。唯一的问题是这些变化没有反映在图中。无论如何,我的问题不在于我的代码是否正确;我不在乎那个。我只关心是否有办法在 Julia 中使用 PyPlot.jl 向 matplotlib 图添加补丁。
  • 当我将您的代码粘贴到 Julia 0.6 中时,每一行都会报告一个错误。我想念“使用绘图”;使用“PyPlot”等等
  • 你显然已经在使用PyPlot...我的问题是关于使用 PyPlot 实现这一点,所以在不加载它的情况下运行我的代码是没有意义的...@987654324 @ 已添加到我的示例中。

标签: matplotlib julia patch


【解决方案1】:

因为这仍然是一个热门搜索结果,所以这里有一个解决方案(使用 Julia 1.5.3)

import PyPlot; const plt = PyPlot
fig = plt.figure(figsize=(6,8), dpi=150)
ax0 = fig.add_subplot(2,2,2)
ax1 = fig.add_subplot(2,2,4)
arrow = patches.ConnectionPatch(
    [0.2,1],
    [0.6,0.5],
    coordsA=ax0.transData,
    coordsB=ax1.transData,
    color="black",
    arrowstyle="-|>",
    mutation_scale=30,
    linewidth=3,
)
fig.patches = [arrow]

它产生以下情节。

【讨论】:

    猜你喜欢
    • 2021-08-28
    • 2020-10-27
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多