George Datseris
George Datseris

Reputation: 411

How to add patches to a figure using Julia's PyPlot.jl

Example:

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]()

How can I add a patch object to a figure and actually see it on the figure? This doesn't work. It doesn't throw any errors but I cannot see any arrows.

(I also cannot use the function arrow because I want to create an arrow that goes from subplot to subplot).

Upvotes: 2

Views: 326

Answers (1)

Andrew Saydjari
Andrew Saydjari

Reputation: 81

Because this is still a top search result, here is a solution (using 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]

It produces the following plot.

Arrow from top subplot to bottom subplot

Upvotes: 3

Related Questions