Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Merged
merged 49 commits into from
May 18, 2021

Conversation

Joschua-Conrad
Copy link
Contributor

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.

text_cursor_screenshot

PR Checklist

  • [N/A] Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • [N/A] New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • [N/A] New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • [N/A] API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

… widgets. Had to modify default formatstring therefore.
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
examples/widgets/text_cursor.py Outdated Show resolved Hide resolved
Change suggested by smartlixx

Co-authored-by: Xianxiang Li <smartlixx@gmail.com>
Copy link
Member

@timhoffm timhoffm left a 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.

Comment on lines 12 to 15
.. 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.
Copy link
Member

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.

Copy link
Contributor Author

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 Show resolved Hide resolved
examples/widgets/text_cursor.py Outdated Show resolved Hide resolved
examples/widgets/text_cursor.py Outdated Show resolved Hide resolved
examples/widgets/text_cursor.py Outdated Show resolved Hide resolved
Comment on lines 299 to 311
# 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)
Copy link
Member

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

Copy link
Contributor Author

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?

Joschua-Conrad and others added 7 commits April 21, 2021 10:24
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
Joschua-Conrad and others added 2 commits April 27, 2021 08:58
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
@Joschua-Conrad
Copy link
Contributor Author

Please squash this when merging.

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...

@jklymak
Copy link
Member

jklymak commented Apr 27, 2021

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.

@QuLogic
Copy link
Member

QuLogic commented Apr 28, 2021

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 batch commit suggested changes from the diff view, or just do the work locally.

@Joschua-Conrad
Copy link
Contributor Author

Joschua-Conrad commented Apr 28, 2021

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.

@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.

@Joschua-Conrad
Copy link
Contributor Author

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 batch commit suggested changes from the diff view, or just do the work locally.

@QuLogic Thank you, the change-approval in the diff view is what I was looking for!

@Joschua-Conrad
Copy link
Contributor Author

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?

examples/widgets/annotated_cursor.py Outdated Show resolved Hide resolved
examples/widgets/annotated_cursor.py Outdated Show resolved Hide resolved
examples/widgets/annotated_cursor.py Outdated Show resolved Hide resolved
examples/widgets/annotated_cursor.py Outdated Show resolved Hide resolved
examples/widgets/annotated_cursor.py Outdated Show resolved Hide resolved
examples/widgets/annotated_cursor.py Outdated Show resolved Hide resolved
examples/widgets/annotated_cursor.py Outdated Show resolved Hide resolved
examples/widgets/annotated_cursor.py Outdated Show resolved Hide resolved
examples/widgets/annotated_cursor.py Outdated Show resolved Hide resolved
Joschua-Conrad and others added 2 commits May 5, 2021 12:21
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>
@Joschua-Conrad Joschua-Conrad requested a review from QuLogic May 5, 2021 10:30
@QuLogic
Copy link
Member

QuLogic commented May 6, 2021

You will need to fix the flake8 issues before this is merged.

@Joschua-Conrad
Copy link
Contributor Author

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.

@Joschua-Conrad
Copy link
Contributor Author

You will need to fix the flake8 issues before this is merged.

@QuLogic All checks pass now. Thank you for the hint on that!

examples/widgets/annotated_cursor.py Show resolved Hide resolved
The title is a ax.set_title("Cursor Tracking [x|y] Position") call

Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
@Joschua-Conrad Joschua-Conrad requested a review from QuLogic May 16, 2021 15:42
@QuLogic QuLogic merged commit 4723f8a into matplotlib:master May 18, 2021
@QuLogic
Copy link
Member

QuLogic commented May 18, 2021

Thanks @Joschua-Conrad ! Congratulations on your first PR to Matplotlib 🎉 We hope to hear from you again.

@Joschua-Conrad
Copy link
Contributor Author

Thank you all for helping me with making my code mergable!

@QuLogic QuLogic added this to the v3.5.0 milestone May 18, 2021
jklymak pushed a commit to jklymak/matplotlib that referenced this pull request Jun 13, 2021
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.