-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Example: Cursor widget with text #20019
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
Example: Cursor widget with text #20019
Conversation
…l and trouble with the dataaxis parameter
… widgets. Had to modify default formatstring therefore.
…multiline function calls.
This leaves widgets.py unchanged in comparison to master branch
ff merge was rejected. This merges changes from upstream/master, which is necessary due to this feature branch being unused for a long time (sorry)
The hint on the missing cursor in example's preview is now a note in the docstring. FIxed format of multiline examples of Cursor instantiation
Change suggested by smartlixx Co-authored-by: Xianxiang Li <smartlixx@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also https://matplotlib.org/stable/gallery/misc/cursor_demo.html#sphx-glr-gallery-misc-cursor-demo-py, which is related. It implements a cursor without relying on the cursor widget, but otherwise has some similar features like your example. Not sure how to organize all this reasonably. Suggestions are welcome.
examples/widgets/text_cursor.py
Outdated
.. note:: | ||
The figure related to this example does not show the cursor, because that | ||
figure is automatically created in a build queue, where the first mouse | ||
movement, which triggers the cursor creation, is missing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could send a first fake event. But not sure if it's worth it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the commit e989183 where I removed the following code
# In the gallery picture, we cannot see the cursor.
# The cursor is displayed after the first mouse move event.
# There is no mouse move when using automatic scripts forgenerating the
# documentation. The onmove event needs to occur after the plt.show() command,
# because this one creates a fresh and clean canvas. Without the cursor or
# its text. And after the plt.show(), we cannot execute more instructions.
# The following code draws the figure (without showing it) and calls the mouse
# event manually. The resulting figure is saved to a file. But it cannot be
# shown, because this first clears the canvas and therefore removes the cursor.
from matplotlib.backend_bases import MouseEvent
location = np.array([0, 10])
location = ax.transData.transform(location)
event = MouseEvent(name='motion_notify_event', button=None, key=None,
x=location[0], y=location[1], canvas=ax.figure.canvas)
ax.figure.canvas.draw()
cursor.onmove(event)
fig.savefig('text_cursor.png')
and added the note. Sending the fake event AND having plt.show also does not work. So I removed all that code and did not tried to add e.g. a "draw" event callback, because that might confuse the reader and one might have trouble distinguishing between stuff one needs for the core example and stuff needed for the sphinx gallery.
So I came to the conclusion: It's not worth it.
examples/widgets/text_cursor.py
Outdated
# A call demonstrating problems with the dataaxis=y parameter. | ||
# The text now looks up the matching x value for the current cursor y position | ||
# instead of vice versa. Hover you cursor to y=4. There are two x values | ||
# producing this y value: -2 and 2. The function is only unique, | ||
# but not biunique. Only one value is shown in the text. | ||
# cursor = TextCursor( | ||
# line=lin[0], | ||
# numberformat="{0:.2f}\n{1:.2f}", | ||
# dataaxis='y', offset=[10, 10], | ||
# textprops={'color': 'blue', 'fontweight': 'bold'}, | ||
# ax=ax, | ||
# useblit=True, | ||
# color='red', linewidth=2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if you want to go through this discussion and the added complexity within the example. Alternatively, you could assume that x is the independent variable as is the case for most plots, and let users figure out the other case.
If this stays, this should be moved to a separate paragraph below plt.show()
. See https://sphinx-gallery.github.io/stable/syntax.html#how-to-structure-your-python-scripts-for-sphinx-gallery
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I now have this code organized in a second plt.show() and like it's described in the sphinx gallery docs you linked. Can this stay the way I organized it now, or shall I remove the whole hint?
Follows suggestion of timhoffm Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
Suggested by timhoffm Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
Suggested by timhoffm
Suggested by timhoffm
File and class name changed
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Will do. @QuLogic all your requested changes were clear and applied. Maybe check in b1c0b24, if the new explanation is coherent. Sorry for launching an avalanche of workflows, but I don't know how to suppress workflows for the first few commits when many commits are created within a short time... |
you can't suppress the workflows unfortunately, but you have 45 commits. Can you squash these? If that is a pain we will squash them anyway before merging, so there is really not a lot of need to keep so many commits. |
You can batch commit suggested changes from the diff view, or just do the work locally. |
@jklymak I referenced some of the commits in my replies and I can only squash the commits be squash-merging them to a new branch, but I then cannot change the branch to which this PR is attached. So I'd keep the commits and squash them when merging into master. |
@QuLogic Thank you, the change-approval in the diff view is what I was looking for! |
Do I still need to change or answer something? I answered questions, applied suggested changes and left two answers, where I'm not sure open, which are: Is there something else I can do? |
Some stuff rephrased Fixed typos Changed default argument for text offset from list to tuple Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
You will need to fix the flake8 issues before this is merged. |
Sorry I had my editor set up for another project, where line length issues are ignored during linting. |
@QuLogic All checks pass now. Thank you for the hint on that! |
The title is a ax.set_title("Cursor Tracking [x|y] Position") call Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Thanks @Joschua-Conrad ! Congratulations on your first PR to Matplotlib 🎉 We hope to hear from you again. |
Thank you all for helping me with making my code mergable! |
* Base version with the new cursor and one example. * Extended example with more subexamples showing a minimum __init__ call and trouble with the dataaxis parameter * Added notice for problems occurring when using non biunique plots and dataaxis=y * Matched docstring such that Sphinx output looks like as for the other widgets. Had to modify default format string therefore. * Added whats new message * Code should pass flake8 test now. * Cursor sticks to plotted line now * fixed indention * Added test * Maybe flake8 test passes now. Had problems with backslashes escaping multiline function calls. * Moved new text cursor class definition to its example file. This leaves widgets.py unchanged in comparison to master branch * Also removed the unittest of the new TextCursor class, as it now is an example * Fixed linter errors in example sourcefile * Added reST explanation for new example. * Built the docs and removed two docstring mistakes. * Docstring and code format changes The hint on the missing cursor in example's preview is now a note in the docstring. FIxed format of multiline examples of Cursor instantiation * What's new description now describes a new example instead of a new feature * Removed what's new note * Fix typo in docstring Change suggested by smartlixx Co-authored-by: Xianxiang Li <smartlixx@gmail.com> * Unite figure and axes creation Follows suggestion of timhoffm Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> * Remove comments on sample data creation Suggested by timhoffm Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> * Renamed "lin" to "line" Suggested by timhoffm * Unpacking tuple returned by "plot()" now the moment it is returned. Suggested by timhoffm * Moved example with non-biunique functions to a new example section * Referencing the cross hair cursor demo now * Renamed the new cursor from text_cursor to annotated_cursor File and class name changed * Skipping redrawing the cursor now when moving mouse between two plot points * Remove braces Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Restructured nested if condition Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Another braces removal Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Apply new string format syntax to exception string Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Simplified search for plot datat index matching mouse cursor position Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Fix typo in docstring Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Fix typo in docstring Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Added linefeed in docstring Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Fix typo in docstring Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Renamed setpos to set_position Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Fixed all misspellings of "background" Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Nicer explanation in comment about relating passed color arguments to color of graphical elements Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Fixed bug introduced with f93091e * Fixed "overwritten" typo in docstrings Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Added linefeeds in docstring Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Fixed "cooridnates" typo Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Shortened comment Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Heavily improve readibility of docstrings Some stuff rephrased Fixed typos Changed default argument for text offset from list to tuple Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Removed unused attribute self.textdata Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Fixed linting error * Added axes title The title is a ax.set_title("Cursor Tracking [x|y] Position") call Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> Co-authored-by: Joschua Conrad <joschua.conrad@uni-ulm.de> Co-authored-by: Xianxiang Li <smartlixx@gmail.com> Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
PR Summary
Added a new example of a cursor widget, which sticks to the plotted line and displays the closest datapoint to which the cursor currently sticks.
This is the continuation of closed PR #13933, where the feature was introduced inside matplotlib's API. The example now just adds the new examples/widgets/text_cursor.py and leaves all other files of MPL unchanged.
The example is supposed to be used as cursor for interactively investigating plots and to learn about the widget API.
PR Checklist
pytest
passes).flake8
on changed files to check).flake8-docstrings
and runflake8 --docstring-convention=all
).doc/users/next_whats_new/
(follow instructions in README.rst there).doc/api/next_api_changes/
(follow instructions in README.rst there).