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

Sign up
Appearance settings

New anchored direction arrows #9078

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
jklymak merged 1 commit into matplotlib:master from idahj:NEW_AnchoredDirectionArrows
May 1, 2018

Conversation

Copy link
Contributor

@idahj idahj commented Aug 23, 2017
edited by jklymak
Loading

This is a new mpl_toolkits class to easily create a pair of orthogonal arrows to indicate directions in a 2D-plot, such as this:

simple_example

This functionality is useful for users who want to represent projection images such as microscopy images, where orientation is important. One example is the below representation of an electron microscopy image, where the directions are vital for showing what projection of the crystal is imaged. Two arrows are needed, such that the viewer can know the angle of the projection.

40679_2017_42_fig5_html

:class:~mpl_toolkits.axes_grid1.anchored_artists.AnchoredDirectionArrows uses FancyArrowPatch, TextPath and PathPath to construct the arrow pair with labels , contained in an AnchoredOffsetbox.

In the most simple example where all default values are used, only the arrow labels and the transform for the coordinate system is needed. But there are several optional parameters that can be used to tweak the direction indicator: loc, rotation of the arrow pair, dimensions, padding, separation, colors and font family.

Question 1: The user may want to create high contrast arrows with for example a light edge color and dark face color. As the AnchoredDirectionArrows consist of two FancyArrowPatch on top of each other, the edge of the top arrow will cross the bottom arrow face. In order to only have a visible edge around the outer edge of the pair, two AnchoredDirectionArrows can be constructed: The first arrow pair with edge, and the second arrow pair with no edge, but a face color without transparency. This solves the problem, but not very elegantly. Is there a robust way of merging the two FancyArrowPatches into one?

Example code:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredDirectionArrows
import matplotlib.font_manager as fm
fig, ax = plt.subplots()
ax.imshow(np.random.random((10, 10)))
# Simple example
simple_arrow = AnchoredDirectionArrows(ax.transAxes, 'X', 'Y')
ax.add_artist(simple_arrow)
# High contrast arrow
high_contrast_part_1 = AnchoredDirectionArrows(
 ax.transAxes,
 '111', r'11$\overline{2}$',
 loc=1,
 arrow_props={'ec': 'w', 'fc': 'none', 'alpha': 1,
 'lw': 2}
 )
ax.add_artist(high_contrast_part_1)
high_contrast_part_2 = AnchoredDirectionArrows(
 ax.transAxes,
 '111', r'11$\overline{2}$',
 loc=1,
 arrow_props={'ec': 'none', 'fc': 'k'},
 text_props={'ec': 'w', 'fc': 'k', 'lw': 0.4}
 )
ax.add_artist(high_contrast_part_2)
# Rotated arrow
fontprops = fm.FontProperties(family='serif')
roatated_arrow = AnchoredDirectionArrows(
 ax.transAxes,
 '30', '120',
 loc='center',
 color='w',
 angle=30,
 fontproperties=fontprops
 )
ax.add_artist(roatated_arrow)
# Altering arrow directions
a1 = AnchoredDirectionArrows(
 ax.transAxes, 'A', 'B', loc='lower center',
 length=-0.15,
 sep_x=0.03, sep_y=0.03,
 color='r'
 )
ax.add_artist(a1)
a2 = AnchoredDirectionArrows(
 ax.transAxes, 'A', ' B', loc='lower left',
 aspect_ratio=-1,
 sep_x=0.01, sep_y=-0.02,
 color='orange'
 )
ax.add_artist(a2)
a3 = AnchoredDirectionArrows(
 ax.transAxes, ' A', 'B', loc='lower right',
 length=-0.15,
 aspect_ratio=-1,
 sep_y=-0.1, sep_x=0.04,
 color='cyan'
 )
ax.add_artist(a3)

demo_anchored_arrows

Copy link
Member

@phobson phobson 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 incredibly thoroughly and well thought-out PR. I have some minor comments and a tip about the CI failure.

I think this could be a really cool feature.

parameters that can be used to alter layout. For example, the arrow pairs can
be rotated and the color can be changed. By default the labels and arrows have
the same color, but the class may also pass arguments for costumizing arrow
and text layout, these are passed to :class: `matplotlib.text.TextPath` and
Copy link
Member

Choose a reason for hiding this comment

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

the space afer :class: might be a problem (or might not, I'm not sure)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

be rotated and the color can be changed. By default the labels and arrows have
the same color, but the class may also pass arguments for costumizing arrow
and text layout, these are passed to :class: `matplotlib.text.TextPath` and
`matplotlib.patches.FancyArrowPatch`. Location, arrow lenght, width and head
Copy link
Member

Choose a reason for hiding this comment

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

lenght -> length

Please clarify if the length and width adjustments apply to both the line and the head

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Clarification attempted. Hope it is better now.

>>> arrows = AnchoredDirectionArrows(ax.transAxes,'East','South', \
loc='lower left', color='k', aspect_ratio=-1, sep_x=0.02,sep_y=-0.01, \
text_props={'ec':'w','fc':'k'}, fontproperties=fontprops)
"""
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure why AppVeyor is complaining about SyntaxError: invalid escape sequence \o right here

Copy link
Member

Choose a reason for hiding this comment

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

Because it's complaining about the whole string and not the actual problem, which is on line 516.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Works now with raw string removed.

>>> arrows = AnchoredDirectionArrows(ax.transAxes,'East','South', \
loc='lower left', color='k', aspect_ratio=-1, sep_x=0.02,sep_y=-0.01, \
text_props={'ec':'w','fc':'k'}, fontproperties=fontprops)
"""
Copy link
Member

Choose a reason for hiding this comment

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

Because it's complaining about the whole string and not the actual problem, which is on line 516.

>>> fig, ax = plt.subplots()
>>> ax.imshow(np.random.random((10,10)))
>>> arrows = AnchoredDirectionArrows(ax.transAxes, '111', \
r'11$\overline{2}$')
Copy link
Member

Choose a reason for hiding this comment

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

This is a raw string inside a non-rawstring, so Python 3 is complaining.

Copy link
Member

Choose a reason for hiding this comment

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

Use ... for wrapping and align the wrapped line correctly:

 >>> arrows = AnchoredDirectionArrows(ax.transAxes, '111',
 ... r'11$\overline{2}$')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> from mpl_toolkits.axes_grid1.anchored_artists import \
AnchoredDirectionArrows
Copy link
Member

Choose a reason for hiding this comment

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

Use ... for wrapping:

 >>> from mpl_toolkits.axes_grid1.anchored_artists import \
 ... AnchoredDirectionArrows

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed


>>> import matplotlib.font_manager as fm
>>> fontprops = fm.FontProperties(family='monospace')
>>> arrows = AnchoredDirectionArrows(ax.transAxes,'East','South', \
Copy link
Member

Choose a reason for hiding this comment

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

Use ... for wrapping here too; add spaces after commas.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Copy link
Contributor Author

idahj commented Aug 25, 2017

Docstring fixes and clarifications are now committed. All tests are now fine.

@dstansby dstansby added this to the 2.2 (next feature release) milestone Oct 8, 2017
Copy link
Member

phobson commented Apr 9, 2018

Bump.

@idahj I'm going to try to get a second reviewer on this. so sorry it fell by the wayside.

idahj reacted with thumbs up emoji

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

I don't see what the harm is putting this in the toolkit. OTOH, it would be nice to not have the toolkit after a while.

This needs a rebase before it can be merged.

Copy link
Member

jklymak commented Apr 28, 2018

ping @idahj this can be merged after a rebase. Can you do the rebase, or would you like a dev to push to this PR?

Copy link
Contributor Author

idahj commented Apr 29, 2018

It should be OK now

Copy link
Member

jklymak commented Apr 29, 2018

Sorry, can you also squash the 17 commits?

Copy link
Contributor Author

idahj commented Apr 29, 2018

What is the easiest way of doing that?

Copy link
Member

jklymak commented Apr 29, 2018

  1. backup your branch (assuming you are on your branch git checkout -b backupbranch)
  2. checkout your branch again and git log. Look for the last entry that is not part of your changes, and note the hash...
  3. git rebase -i thehash

Only push if this isn't a huge error. If it makes a mess, then reset this branch to your backup branch:
git reset --hard backupbranch and you will be back where you started.

@tacaswell tacaswell modified the milestones: needs sorting, v3.0 Apr 30, 2018
Copy link
Member

wow, this is cool! Sorry this fell through the cracks.

commit a05a486
Merge: 6ec4f8f e751286
Author: Ida Hjorth <idahjorth@gmail.com>
Date: Sun Apr 29 19:07:32 2018 +0200
 Merge pull request #3 from magnunor/idahj_anchored_directionarrows
 Idahj anchored directionarrows
commit e751286
Merge: 3ba79fb 6ec4f8f
Author: Magnus Nord <magnunor@gmail.com>
Date: Sun Apr 29 09:00:34 2018 +0200
 Merge branch 'NEW_AnchoredDirectionArrows' of github.com:idahj/matplotlib into idahj_anchored_directionarrows
commit 6ec4f8f
Author: Ida <idahjorth@gmail.com>
Date: Fri Aug 25 13:14:26 2017 +0100
 AnchoredDirectionArrows: Docstring, what's new
commit 19f4a38
Author: Ida <idahjorth@gmail.com>
Date: Thu Aug 24 14:27:23 2017 +0100
 AnchoredDirectionArrows: Demo docstring
commit d024684
Author: Ida <idahjorth@gmail.com>
Date: Thu Aug 24 14:09:52 2017 +0100
 AnchoredDirectionArrows: Fix error
commit 27b6bc5
Author: Ida <idahjorth@gmail.com>
Date: Thu Aug 24 13:58:58 2017 +0100
 AnchoredDirectionArrows: make variable not private
commit c0762d2
Author: Ida <idahjorth@gmail.com>
Date: Thu Aug 24 13:02:38 2017 +0100
 AnchoredDirectionArrows: Add docstring in demo
commit 88d04d0
Author: Ida <idahjorth@gmail.com>
Date: Wed Aug 23 14:19:06 2017 +0100
 AnchoredDirectionArrows: docstring, what's new
commit efcca59
Author: Magnus Nord <Magnus.Nord@glasgow.ac.uk>
Date: Wed Aug 23 13:31:32 2017 +0100
 AnchoredDirectionArrows: add test which uses many of the arguments
commit e200c44
Author: Magnus Nord <Magnus.Nord@glasgow.ac.uk>
Date: Wed Aug 23 12:06:05 2017 +0100
 AnchoredDirectionArrows: minor docstring changes
commit b6b0ea2
Author: Ida <idahjorth@gmail.com>
Date: Tue Aug 22 13:28:56 2017 +0100
 AnchoredDirectionArrows: PEP8
commit 5fde65a
Author: Ida <idahjorth@gmail.com>
Date: Tue Aug 22 13:23:25 2017 +0100
 AnchoredDirectionArrows: Add demo
commit 23f7d5b
Author: Ida <idahjorth@gmail.com>
Date: Tue Aug 22 11:16:41 2017 +0100
 AnchoredDirectionArrows: Add working unit test
commit 459ae07
Author: Ida <idahjorth@gmail.com>
Date: Tue Aug 22 10:37:44 2017 +0100
 AnchoredDirectionArrow: first attempt unit test
 Currently not working
commit f4ec0e2
Author: Ida <idahjorth@gmail.com>
Date: Wed Aug 16 19:31:05 2017 +0100
 Cleanup, bug fix, begin merge overlapping arrows
commit 75e52b0
Author: Magnus Nord <magnunor@gmail.com>
Date: Mon Aug 14 23:05:30 2017 +0100
 AnchoredDirectionArrows: PEP8 fixes, and improve docstring
commit 4fff775
Author: Ida <idahjorth@gmail.com>
Date: Mon Aug 14 22:04:22 2017 +0100
 Add AnchoredDirectionArrows class in axes_grid1
@idahj idahj force-pushed the NEW_AnchoredDirectionArrows branch from a05a486 to 4ba7f54 Compare May 1, 2018 08:39
Copy link
Contributor Author

idahj commented May 1, 2018

Commits are now squashed

@jklymak jklymak merged commit 77ca117 into matplotlib:master May 1, 2018
Copy link
Member

jklymak commented May 1, 2018

Thanks so much @idahj Many appologies for the saga!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@QuLogic QuLogic QuLogic left review comments

@phobson phobson phobson approved these changes

@jklymak jklymak jklymak approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
v3.0.0
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /