Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

FIX extra comma in Sankey.add #1472

Merged
merged 1 commit into from

2 participants

Varoquaux Damon McDougall
Varoquaux
Collaborator

There was a mistake in one of my PEP8 patches: I added an extra comma. Hence, the code failed when calling it.

I fixed the error, and added a small test that just checks the code runs.

Damon McDougall
Collaborator

+1

Damon McDougall
Collaborator

It looks like you also moved the __init__ function to the top of the class. While I actually prefer that, I think I'd also prefer that change to be in its own commit.

My feeling is that the commit message should reflect the changes that are made. The commit message currently says you fixed an issue with a comma, but the init function was also moved.

Sorry to be picky -- but it does make the logs and diffs easier to read.

Damon McDougall
Collaborator

Also if anyone else disagrees, feel free to shoot me down.

Varoquaux
Collaborator

I've added this to the commit message

Damon McDougall dmcdougall merged commit c6221e2 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 136 additions and 121 deletions.
  1. +124 −121 lib/matplotlib/sankey.py
  2. +12 −0 lib/matplotlib/tests/test_sankey.py
245 lib/matplotlib/sankey.py
View
@@ -62,6 +62,129 @@ class Sankey:
`Wikipedia (6/1/2011) <http://en.wikipedia.org/wiki/Sankey_diagram>`_
"""
+
+ def __init__(self, ax=None, scale=1.0, unit='', format='%G', gap=0.25,
+ radius=0.1, shoulder=0.03, offset=0.15, head_angle=100,
+ margin=0.4, tolerance=1e-6, **kwargs):
+ """
+ Create a new Sankey instance.
+
+ Optional keyword arguments:
+
+ =============== ===================================================
+ Field Description
+ =============== ===================================================
+ *ax* axes onto which the data should be plotted
+ If *ax* isn't provided, new axes will be created.
+ *scale* scaling factor for the flows
+ *scale* sizes the width of the paths in order to
+ maintain proper layout. The same scale is applied
+ to all subdiagrams. The value should be chosen
+ such that the product of the scale and the sum of
+ the inputs is approximately 1.0 (and the product of
+ the scale and the sum of the outputs is
+ approximately -1.0).
+ *unit* string representing the physical unit associated
+ with the flow quantities
+ If *unit* is None, then none of the quantities are
+ labeled.
+ *format* a Python number formatting string to be used in
+ labeling the flow as a quantity (i.e., a number
+ times a unit, where the unit is given)
+ *gap* space between paths that break in/break away
+ to/from the top or bottom
+ *radius* inner radius of the vertical paths
+ *shoulder* size of the shoulders of output arrowS
+ *offset* text offset (from the dip or tip of the arrow)
+ *head_angle* angle of the arrow heads (and negative of the angle
+ of the tails) [deg]
+ *margin* minimum space between Sankey outlines and the edge
+ of the plot area
+ *tolerance* acceptable maximum of the magnitude of the sum of
+ flows
+ The magnitude of the sum of connected flows cannot
+ be greater than *tolerance*.
+ =============== ===================================================
+
+ The optional arguments listed above are applied to all subdiagrams so
+ that there is consistent alignment and formatting.
+
+ If :class:`Sankey` is instantiated with any keyword arguments other
+ than those explicitly listed above (``**kwargs``), they will be passed
+ to :meth:`add`, which will create the first subdiagram.
+
+ In order to draw a complex Sankey diagram, create an instance of
+ :class:`Sankey` by calling it without any kwargs::
+
+ sankey = Sankey()
+
+ Then add simple Sankey sub-diagrams::
+
+ sankey.add() # 1
+ sankey.add() # 2
+ #...
+ sankey.add() # n
+
+ Finally, create the full diagram::
+
+ sankey.finish()
+
+ Or, instead, simply daisy-chain those calls::
+
+ Sankey().add().add... .add().finish()
+
+ .. seealso::
+
+ :meth:`add`
+ :meth:`finish`
+
+
+ **Examples:**
+
+ .. plot:: mpl_examples/api/sankey_demo_basics.py
+ """
+ # Check the arguments.
+ assert gap >= 0, (
+ "The gap is negative.\nThis isn't allowed because it "
+ "would cause the paths to overlap.")
+ assert radius <= gap, (
+ "The inner radius is greater than the path spacing.\n"
+ "This isn't allowed because it would cause the paths to overlap.")
+ assert head_angle >= 0, (
+ "The angle is negative.\nThis isn't allowed "
+ "because it would cause inputs to look like "
+ "outputs and vice versa.")
+ assert tolerance >= 0, (
+ "The tolerance is negative.\nIt must be a magnitude.")
+
+ # Create axes if necessary.
+ if ax is None:
+ import matplotlib.pyplot as plt
+ fig = plt.figure()
+ ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[])
+
+ self.diagrams = []
+
+ # Store the inputs.
+ self.ax = ax
+ self.unit = unit
+ self.format = format
+ self.scale = scale
+ self.gap = gap
+ self.radius = radius
+ self.shoulder = shoulder
+ self.offset = offset
+ self.margin = margin
+ self.pitch = np.tan(np.pi * (1 - head_angle / 180.0) / 2.0)
+ self.tolerance = tolerance
+
+ # Initialize the vertices of tight box around the diagram(s).
+ self.extent = np.array((np.inf, -np.inf, np.inf, -np.inf))
+
+ # If there are any kwargs, create the first subdiagram.
+ if len(kwargs):
+ self.add(**kwargs)
+
def _arc(self, quadrant=0, cw=True, radius=1, center=(0, 0)):
"""
Return the codes and vertices for a rotated, scaled, and translated
@@ -558,7 +681,7 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='',
llpath[-1][1][1]]))
has_left_input = True
tip, label_location = self._add_input(llpath, angle, *spec)
- tips[n - i - 1, :], = tip
+ tips[n - i - 1, :] = tip
label_locations[n - i - 1, :] = label_location
# Add the right-side outputs from the top downwards.
has_right_output = False
@@ -722,124 +845,4 @@ def finish(self):
self.ax.set_aspect('equal', adjustable='datalim')
return self.diagrams
- def __init__(self, ax=None, scale=1.0, unit='', format='%G', gap=0.25,
- radius=0.1, shoulder=0.03, offset=0.15, head_angle=100,
- margin=0.4, tolerance=1e-6, **kwargs):
- """
- Create a new Sankey instance.
-
- Optional keyword arguments:
-
- =============== ===================================================
- Field Description
- =============== ===================================================
- *ax* axes onto which the data should be plotted
- If *ax* isn't provided, new axes will be created.
- *scale* scaling factor for the flows
- *scale* sizes the width of the paths in order to
- maintain proper layout. The same scale is applied
- to all subdiagrams. The value should be chosen
- such that the product of the scale and the sum of
- the inputs is approximately 1.0 (and the product of
- the scale and the sum of the outputs is
- approximately -1.0).
- *unit* string representing the physical unit associated
- with the flow quantities
- If *unit* is None, then none of the quantities are
- labeled.
- *format* a Python number formatting string to be used in
- labeling the flow as a quantity (i.e., a number
- times a unit, where the unit is given)
- *gap* space between paths that break in/break away
- to/from the top or bottom
- *radius* inner radius of the vertical paths
- *shoulder* size of the shoulders of output arrowS
- *offset* text offset (from the dip or tip of the arrow)
- *head_angle* angle of the arrow heads (and negative of the angle
- of the tails) [deg]
- *margin* minimum space between Sankey outlines and the edge
- of the plot area
- *tolerance* acceptable maximum of the magnitude of the sum of
- flows
- The magnitude of the sum of connected flows cannot
- be greater than *tolerance*.
- =============== ===================================================
-
- The optional arguments listed above are applied to all subdiagrams so
- that there is consistent alignment and formatting.
-
- If :class:`Sankey` is instantiated with any keyword arguments other
- than those explicitly listed above (``**kwargs``), they will be passed
- to :meth:`add`, which will create the first subdiagram.
-
- In order to draw a complex Sankey diagram, create an instance of
- :class:`Sankey` by calling it without any kwargs::
-
- sankey = Sankey()
-
- Then add simple Sankey sub-diagrams::
-
- sankey.add() # 1
- sankey.add() # 2
- #...
- sankey.add() # n
-
- Finally, create the full diagram::
-
- sankey.finish()
-
- Or, instead, simply daisy-chain those calls::
-
- Sankey().add().add... .add().finish()
-
- .. seealso::
-
- :meth:`add`
- :meth:`finish`
-
- **Examples:**
-
- .. plot:: mpl_examples/api/sankey_demo_basics.py
- """
- # Check the arguments.
- assert gap >= 0, (
- "The gap is negative.\nThis isn't allowed because it "
- "would cause the paths to overlap.")
- assert radius <= gap, (
- "The inner radius is greater than the path spacing.\n"
- "This isn't allowed because it would cause the paths to overlap.")
- assert head_angle >= 0, (
- "The angle is negative.\nThis isn't allowed "
- "because it would cause inputs to look like "
- "outputs and vice versa.")
- assert tolerance >= 0, (
- "The tolerance is negative.\nIt must be a magnitude.")
-
- # Create axes if necessary.
- if ax is None:
- import matplotlib.pyplot as plt
- fig = plt.figure()
- ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[])
-
- self.diagrams = []
-
- # Store the inputs.
- self.ax = ax
- self.unit = unit
- self.format = format
- self.scale = scale
- self.gap = gap
- self.radius = radius
- self.shoulder = shoulder
- self.offset = offset
- self.margin = margin
- self.pitch = np.tan(np.pi * (1 - head_angle / 180.0) / 2.0)
- self.tolerance = tolerance
-
- # Initialize the vertices of tight box around the diagram(s).
- self.extent = np.array((np.inf, -np.inf, np.inf, -np.inf))
-
- # If there are any kwargs, create the first subdiagram.
- if len(kwargs):
- self.add(**kwargs)
12 lib/matplotlib/tests/test_sankey.py
View
@@ -0,0 +1,12 @@
+from matplotlib.sankey import Sankey
+
+
+def test_sankey():
+ # lets just create a sankey instance and check the code runs
+ sankey = Sankey()
+ sankey.add()
+
+
+if __name__ == '__main__':
+ import nose
+ nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
Something went wrong with that request. Please try again.