Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyzx.draw() does not work in some matplotlib environments #222

Open
dlyongemallo opened this issue May 5, 2024 · 2 comments · May be fixed by #223
Open

pyzx.draw() does not work in some matplotlib environments #222

dlyongemallo opened this issue May 5, 2024 · 2 comments · May be fixed by #223

Comments

@dlyongemallo
Copy link
Contributor

dlyongemallo commented May 5, 2024

I am embedding a ZX diagram into a web app (using shiny), and calling pyzx.draw(g) results in an error 'NoneType' object has no attribute 'rowspan'.

The cause is due to the way that PyZX is creating the figure in these lines, which does not set matplotlib.gridspec.SubplotSpec properly.

    fig1 = plt.figure(figsize=figsize)
    ax = fig1.add_axes([0, 0, 1, 1], frameon=False)

Some matplotlib environments ignore this silently, but in others this causes an error. For example, this fails also in the latest version of pandas (see this issue and this Stack Overflow answer).

This can be fixed by calling subplots instead:

    fig1, ax = plt.subplots(figsize=figsize)
    ax.set_frame_on(False)

But I think this might result in a slightly different appearance in some environments, as the axes are computed rather than explicitly set. Were the original values [0, 0, 1, 1] explicitly chosen to force a particular appearance? Otherwise, it seems it's likely more robust to let them be computed based on the environment.

@dlyongemallo
Copy link
Contributor Author

Repro case:
Screenshot from 2024-05-05 12-59-35

With the fix:
Screenshot from 2024-05-05 12-57-59

The bottom label is slightly cut off, but given that the alternative is no diagram displayed at all, it's an improvement.

@dlyongemallo
Copy link
Contributor Author

If I explicitly call ax.set(xlim=(0, 1), ylim=(0, 1)), that fixes the issue with the label being cut off. This must've been the original reason for forcing those axes. I'll send a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant