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

White screen #36

Closed Atcold closed 2 years ago

Atcold commented 2 years ago

When I try running the following code I get two white screens.

from matplotlib.pylab import *
π = pi

import pylustrator
pylustrator.start()

t = linspace(0, 1)
x = sin(2 * π * t)
plot(t, x)
show()

image

$ python sine.py 
Traceback (most recent call last):
  File "/usr/local/Caskroom/miniconda/base/envs/book/lib/python3.10/site-packages/pylustrator/QtGuiDrag.py", line 1122, in closeEvent
    if not self.fig.change_tracker.saved:
AttributeError: 'Figure' object has no attribute 'change_tracker'

Commenting out the pylustrator's line, I get the following.

image

I'm using matplotlib 3.5.1.

rgerum commented 2 years ago

Apparently pylustrator does not work when using the pylab * import. I would strongly recommend to import matplotlib and numpy directly with "import matplotlib.pyplot as plt" and "import numpy as np" as it makes the code more readable. For example if you use "max" it is not clear if you are using the builtin function max or the numpy function max, as your approach silently overwrote it.

See also the warning box here: https://matplotlib.org/stable/api/index.html?highlight=pylab#module-pylab

As pylustrator overloads the plt.figure and plt.show commands, it cannot work if you have already imported them directly without still being in the namespace of a module. Therefore, you need to write it like this when using pylustrator.

import matplotlib.pyplot as plt
import numpy as np
π = np.pi

import pylustrator
pylustrator.start()

t = np.linspace(0, 1)
x = np.sin(2 * π * t)
plt.plot(t, x)
plt.show()
rgerum commented 2 years ago

if you want to keep your code like this, you can alternatively just use plt.show() for opening the pylustrator and leave the rest as it is. As just this is the crucial part.

So this also works, although I would recommend against importing with *

from matplotlib.pylab import *
π = pi

import pylustrator
pylustrator.start()

t = linspace(0, 1)
x = sin(2 * π * t)
plot(t, x)
import matplotlib.pyplot as plt
plt.show()
Atcold commented 2 years ago

Oh, thanks! It does work! 🎉

Thanks for the recommendation as well, but I'd rather use a calculator without the need to tell it where to find its functions. If I do write code, then yes, I use namespaces. If I do maths, then no, it just bogs down the thinking.

Finally, not sure this is a bug, but the button strings are quite illegible. image

Also, I believe some y's are missing. image