rgerum / pylustrator

Visualisations of data are at the core of every publication of scientific research results. They have to be as clear as possible to facilitate the communication of research. As data can have different formats and shapes, the visualisations often have to be adapted to reflect the data as well as possible. We developed Pylustrator, an interface to directly edit python generated matplotlib graphs to finalize them for publication. Therefore, subplots can be resized and dragged around by the mouse, text and annotations can be added. The changes can be saved to the initial plot file as python code.
GNU General Public License v3.0
706 stars 38 forks source link

add custom_stack_position etc for Julia (PyCall) support #26

Closed sp94 closed 3 years ago

sp94 commented 3 years ago

I love the package but I normally use matplotlib from Julia. However, there are two problems:

  1. traceback.stack_position gives the wrong file/line number if calling from Julia
  2. The generated pylustrator code is in Python

I added a custom_stack_position so anything calling pylustrator from an unusual environment can manually declare the filename and lineno.

I also added custom_prepend, and custom_append variables so when we write to the Julia file we can wrap the generated code in a py"""...""" string, which tells Julia to run it as Python code.

Here is my Julia test file:

include("setup.jl")
pylustrator.start()
plt.figure()
plot([0,1],[0,1])
py"""#% start: automatic generated code from pylustrator
plt.figure(1).ax_dict = {ax.get_label(): ax for ax in plt.figure(1).axes}
import matplotlib as mpl
plt.figure(1).axes[0].set_position([0.376563, 0.439167, 0.523437, 0.438750])
#% end: automatic generated code from pylustrator"""
show()

The setup.jl is

using PyCall
using PyPlot
plt = pyimport("matplotlib.pyplot")
py"""
from matplotlib import pyplot as plt
"""

# ensure my local (modified) copy of pylustrator is loaded
pushfirst!(PyVector(pyimport("sys")."path"), "") 
pylustrator = pyimport("pylustrator")

function show()
    stack_pos = stacktrace()[2]
    filename = abspath(string(stack_pos.file))
    @assert filename == @__FILE__
    ct = pylustrator.change_tracker
    ct.custom_stack_position = ct.CustomStackPosition(filename,stack_pos.line)
    ct.custom_prepend = "py\"\"\""
    ct.custom_append = "\"\"\""
    plt.show()
end

If this was merged, I could make a small package for Julia that handles the install and wrapping of pylustrator, and maybe others could do the same for other languages

rgerum commented 3 years ago

Well, the code seems not to introduce too much clutter for python users, so it seems good to add it. Sounds great to have it also available for julia users.