Skip to content

Add margins for conv graph plot #993

Merged
ryanhill1 merged 12 commits intoqBraid:mainfrom
AishSweety:patch-1
Jun 11, 2025
Merged

Add margins for conv graph plot #993
ryanhill1 merged 12 commits intoqBraid:mainfrom
AishSweety:patch-1

Conversation

@AishSweety
Copy link
Copy Markdown
Contributor

@AishSweety AishSweety commented Jun 8, 2025

Summary of changes

The autoscaling that matplotlib uses will sometimes cause the plot to be clipped. This is more likely to occur when using the "scatter" plot function with large symbols. This change does two things:

  1. Explicitly creates a matplotlib figure object and associated Axes instance and passes the Axes instance to mpl_draw
  2. After mpl_draw returns, a margin of 15% is added to the plot.

The margin around the plot allows the contents to be rendered without clipping.

Note that currently, the margin is hard coded at 15%. To make this more flexible it could be added as an extra keyword argument for the plot_conversion_graph function.

Potentially closes #851

@AishSweety AishSweety requested a review from ryanhill1 as a code owner June 8, 2025 01:17
Copy link
Copy Markdown
Member

@ryanhill1 ryanhill1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Your changes do seem to resolve the issue of nodes being cut off at the edges. However, reducing the margins has the side effect of compressing the nodes toward the center, which makes the graph slightly harder to read.

One way to address this, like you mentioned, could be by introducing a keyword argument to control the margin size—but that introduces a bit of a trade-off. Ideally, instead of shrinking the plot area, we’d find a way to prevent nodes from being plotted beyond a certain perimeter, preserving their current spacing while avoiding cutoff.

Would you be open to exploring that approach? If it's not feasible, we can go ahead with the margin parameter as a fallback.

@ryanhill1
Copy link
Copy Markdown
Member

Here are some examples that show how reducing the margins causes the nodes to appear more crowded in the center. While this does fix the original issue of nodes being cut off, it introduces a new issue with overly tight spacing, which could be seen as equally problematic.

Example 1 before:

d925a284-1356-4fd2-9a67-d4ef5fb7eea1

Example 1 after:

caa729dc-97c7-44b6-b1d2-a52aefad20c4

Example 2 before:

64e3d741-db7d-4d5b-aae5-fd93994caf30

Example 2 after:

2c38f05b-2d8e-4c7f-8a4d-9a30edc5dfd0

@AishSweety
Copy link
Copy Markdown
Contributor Author

We've made "plot_margin" a keyword argument with a default value of 0.1 (10%). This seems to be a better value than the original 0.15 in terms of the use of space within the plot canvas.

The rx.spring_layout doesn't appear to provide many controls by which the distribution of nodes can be modified. However, judicious use of the "repulsive_exponent" argument may improve the spacing and readability of the graph plot (repulsive_exponent is an integer with a default value of 2).

Here are three examples, each using the new default value of the plot_margin argument (note that we have turned on the visible axes for the purposes of gauging the extent of the plot canvas):

graph.plot(seed=362, legend=True)
b59383db439b3583d3b048aabf9f79c0e1a0381ab2c2f83668e69213b45ba7dc

graph.plot(seed=362, legend=True, repulsive_exponent=0)
f58a984331320941ac67aef9d3077b201a9371547d07f38738a1b5df88eaba19

graph.plot(seed=362, legend=True, repulsive_exponent=-1)
dc9c9d1313603528a0452cddbcfd4a0ed58c5fbb27fa075b011ef2c8ab0e12bf
It's probably not a good idea to use negative values of the repulsive_exponent.

Copy link
Copy Markdown
Member

@ryanhill1 ryanhill1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks for looking into that. Instead of using kwargs.pop can you explicitly add margin to the kwargs and docstring of the plot_conversion_graph function, and update CHANGELOG.md? After that, this should be good to go.

Copy link
Copy Markdown
Member

@ryanhill1 ryanhill1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update CHANGELOG.md, see #993 (comment)

@AishSweety AishSweety mentioned this pull request Jun 11, 2025
@AishSweety
Copy link
Copy Markdown
Contributor Author

Thank you! I hope this closes the issue :)

@ryanhill1
Copy link
Copy Markdown
Member

Please include the changelog update in this PR, not a new one. And link back to this PR, not the issue.

@AishSweety
Copy link
Copy Markdown
Contributor Author

I have made the changes.

ryanhill1
ryanhill1 previously approved these changes Jun 11, 2025
@codecov
Copy link
Copy Markdown

codecov bot commented Jun 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@ryanhill1 ryanhill1 merged commit dce0346 into qBraid:main Jun 11, 2025
8 checks passed
ryanhill1 pushed a commit that referenced this pull request Jun 17, 2025
* Update plot_conversions.py

* Update plot_conversions.py

* Update plot_conversions.py

* Update plot_conversions.py

* Update plot_conversions.py

* Update CHANGELOG.md

* Update CHANGELOG.md

* Update CHANGELOG.md

* Update CHANGELOG.md

* Update CHANGELOG.md

* format, re-word docstring, update arg name

* update changelog for arg name consistency
ryanhill1 added a commit that referenced this pull request Jun 17, 2025
…,<0.41 (#991)

* Update qiskit-ibm-runtime requirement

Updates the requirements on [qiskit-ibm-runtime](https://github.com/Qiskit/qiskit-ibm-runtime) to permit the latest version.
- [Release notes](https://github.com/Qiskit/qiskit-ibm-runtime/releases)
- [Commits](Qiskit/qiskit-ibm-runtime@0.25.0...0.40.0)

---
updated-dependencies:
- dependency-name: qiskit-ibm-runtime
  dependency-version: 0.40.0
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <[email protected]>

* Put upper bound on `pydantic` version

* Add margins for conv graph plot  (#993)

* Update plot_conversions.py

* Update plot_conversions.py

* Update plot_conversions.py

* Update plot_conversions.py

* Update plot_conversions.py

* Update CHANGELOG.md

* Update CHANGELOG.md

* Update CHANGELOG.md

* Update CHANGELOG.md

* Update CHANGELOG.md

* format, re-word docstring, update arg name

* update changelog for arg name consistency

* Reset CHANGELOG.md for new version [no ci] (#996)

* Bump patch version to 0.9.7 (#997)

Co-authored-by: qbraidTeam <[email protected]>

* Update CHANGELOG.md

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ryan Hill <[email protected]>
Co-authored-by: Ash <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: qbraidTeam <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Fix nodes being cut-off around outside of conv graph plot

2 participants