Skip to content
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

Adds corner label convenience functions #83

Merged
merged 4 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions examples/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ def random_heatmap(scale=4):

## Boundary and Gridlines
scale = 40
fig, tax= ternary.figure(scale=scale)
fig, tax = ternary.figure(scale=scale)

left_kwargs = {'color': 'blue'}
right_kwargs = {'color': 'red'}

# Draw Boundary and Gridlines
tax.boundary(linewidth=2.0)
tax.gridlines(color="blue", multiple=5, left_kwargs=left_kwargs,
right_kwargs=right_kwargs)
right_kwargs=right_kwargs)

# Draw Boundary and Gridlines
tax.boundary(linewidth=2.0)
Expand All @@ -114,10 +114,10 @@ def random_heatmap(scale=4):
tax.bottom_axis_label("Bottom label $\\Gamma - \\Omega$",
fontsize=fontsize)
tax.get_axes().axis('off')

tax.ticks(axis='lbr', clockwise=True, multiple=5, linewidth=1)

# Remove default Matplotlib Axes
tax.get_axes().axis('off')
tax.clear_matplotlib_ticks()

### Plot Various lines
Expand All @@ -130,11 +130,9 @@ def random_heatmap(scale=4):

# Set Axis labels and Title
fontsize = 20
tax.set_title("Various Lines", fontsize=20)
tax.left_axis_label("Left label $\\alpha^2$", fontsize=fontsize)
tax.right_axis_label("Right label $\\beta^2$", fontsize=fontsize)
tax.bottom_axis_label("Bottom label $\\Gamma - \\Omega$",
fontsize=fontsize)
tax.top_corner_label("X", fontsize=fontsize)
tax.left_corner_label("Y", fontsize=fontsize)
tax.right_corner_label("Z", fontsize=fontsize)

# Draw lines parallel to the axes
tax.horizontal_line(16)
Expand All @@ -161,8 +159,9 @@ def random_heatmap(scale=4):
points = random_points(30, scale=scale)
tax.scatter(points, marker='D', color='green', label="Green Diamonds")
tax.legend()
tax.clear_matplotlib_ticks()
tax.ticks(axis='lbr', multiple=5, linewidth=1)
tax.get_axes().axis('off')
tax.clear_matplotlib_ticks()

## Sample trajectory plot
fig, tax = ternary.figure(scale=1.0)
Expand All @@ -172,6 +171,8 @@ def random_heatmap(scale=4):
tax.gridlines(multiple=0.2, color="black")
tax.plot(points, linewidth=2.0, label="Curve")
tax.legend()
tax.get_axes().axis('off')
tax.clear_matplotlib_ticks()

## Sample colored trajectory plot
fig, tax = ternary.figure(scale=1.0)
Expand All @@ -180,9 +181,10 @@ def random_heatmap(scale=4):
points = load_sample_trajectory_data()
tax.gridlines(multiple=0.2, color="black")
tax.plot_colored_trajectory(points, linewidth=2.0)
points = [(y,z,x) for (x,y,z) in points]
points = [(y, z, x) for (x, y, z) in points]
tax.plot_colored_trajectory(points, cmap="hsv", linewidth=2.0)
tax.legend()
tax.get_axes().axis('off')
tax.clear_matplotlib_ticks()
tax.ticks(axis='lbr', linewidth=1, multiple=0.1)

Expand Down
Binary file modified readme_images/various_lines.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
122 changes: 86 additions & 36 deletions ternary/ternary_axes_subplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def mpl_redraw_callback(event, tax):
Callback to properly rotate and redraw text labels when the plot is drawn
or resized.

Parameters:
Parameters
----------
event: a matplotlib event
either 'resize_event' or 'draw_event'
tax: TernaryAxesSubplot
Expand Down Expand Up @@ -64,16 +65,14 @@ def __init__(self, ax=None, scale=None, permutation=None):
self._boundary_scale = scale
# Container for the axis labels supplied by the user
self._labels = dict()
self._corner_labels = dict()
self._ticks = dict()
# Container for the redrawing of labels
self._to_remove = []
self._connect_callbacks()

def _connect_callbacks(self):
"""
Connect resize matplotlib callbacks.
"""

"""Connect resize matplotlib callbacks."""
figure = self.get_figure()
callback = partial(mpl_redraw_callback, tax=self)
event_names = ('resize_event', 'draw_event')
Expand All @@ -84,17 +83,11 @@ def __repr__(self):
return "TernaryAxesSubplot: %s" % self.ax.__hash__()

def get_axes(self):
"""
Return the underlying matplotlib AxesSubplot object
"""

"""Returns the underlying matplotlib AxesSubplot object."""
return self.ax

def get_figure(self):
"""
Return the underlying matplotlib figure object.
"""

"""Return the underlying matplotlib figure object."""
ax = self.get_axes()
return ax.get_figure()

Expand Down Expand Up @@ -122,10 +115,7 @@ def get_axis_limits(self):
# Title and Axis Labels

def set_title(self, title, **kwargs):
"""
Sets the title on the underlying matplotlib AxesSubplot
"""

"""Sets the title on the underlying matplotlib AxesSubplot."""
ax = self.get_axes()
ax.set_title(title, **kwargs)

Expand All @@ -136,8 +126,6 @@ def left_axis_label(self, label, position=None, rotation=60, offset=0.08,

Parameters
----------
ax: Matplotlib AxesSubplot, None
The subplot to draw on.
label: String
The axis label
position: 3-Tuple of floats, None
Expand All @@ -156,13 +144,12 @@ def left_axis_label(self, label, position=None, rotation=60, offset=0.08,

def right_axis_label(self, label, position=None, rotation=-60, offset=0.08,
**kwargs):

"""
Sets the label on the right axis.

Parameters
----------
ax: Matplotlib AxesSubplot, None
The subplot to draw on.
label: String
The axis label
position: 3-Tuple of floats, None
Expand All @@ -176,7 +163,7 @@ def right_axis_label(self, label, position=None, rotation=-60, offset=0.08,
"""

if not position:
position = (2./5 + offset, 3./5, 0)
position = (2. / 5 + offset, 3. / 5, 0)
self._labels["right"] = (label, position, rotation, kwargs)

def bottom_axis_label(self, label, position=None, rotation=0, offset=0.02,
Expand All @@ -186,8 +173,6 @@ def bottom_axis_label(self, label, position=None, rotation=0, offset=0.02,

Parameters
----------
ax: Matplotlib AxesSubplot, None
The subplot to draw on.
label: String
The axis label
position: 3-Tuple of floats, None
Expand All @@ -201,8 +186,77 @@ def bottom_axis_label(self, label, position=None, rotation=0, offset=0.02,
"""

if not position:
position = (1./2, -offset / 2., 1./2)
self._labels["bottom"] = (label, position, rotation, kwargs)
position = (0.5, -offset / 2., 0.5)
self._corner_labels["bottom"] = (label, position, rotation, kwargs)

def right_corner_label(self, label, position=None, rotation=0, offset=0.08,
**kwargs):
"""
Sets the label on the right corner (complements left axis).

Parameters
----------
label: String
The axis label
position: 3-Tuple of floats, None
The position of the text label
rotation: float, 0
The angle of rotation of the label
offset: float,
Used to compute the distance of the label from the axis
kwargs:
Any kwargs to pass through to matplotlib.
"""

if not position:
position = (1, offset / 2, 0)
self._labels["right"] = (label, position, rotation, kwargs)

def left_corner_label(self, label, position=None, rotation=0, offset=0.08,
**kwargs):
"""
Sets the label on the left corner (complements right axis.)

Parameters
----------
label: string
The axis label
position: 3-Tuple of floats, None
The position of the text label
rotation: float, 0
The angle of rotation of the label
offset: float,
Used to compute the distance of the label from the axis
kwargs:
Any kwargs to pass through to matplotlib.
"""

if not position:
position = (-offset / 2, offset / 2, 0)
self._corner_labels["left"] = (label, position, rotation, kwargs)

def top_corner_label(self, label, position=None, rotation=0, offset=0.2,
**kwargs):
"""
Sets the label on the bottom axis.

Parameters
----------
label: String
The axis label
position: 3-Tuple of floats, None
The position of the text label
rotation: float, 0
The angle of rotation of the label
offset: float,
Used to compute the distance of the label from the axis
kwargs:
Any kwargs to pass through to matplotlib.
"""

if not position:
position = (-offset / 2, 1 + offset, 0)
self._corner_labels["bottom"] = (label, position, rotation, kwargs)

def annotate(self, text, position, **kwargs):
ax = self.get_axes()
Expand Down Expand Up @@ -271,10 +325,7 @@ def show(self):
# Axis ticks

def clear_matplotlib_ticks(self, axis="both"):
"""
Clears the default matplotlib ticks.
"""

"""Clears the default matplotlib ticks."""
ax = self.get_axes()
plotting.clear_matplotlib_ticks(ax=ax, axis=axis)

Expand Down Expand Up @@ -321,17 +372,16 @@ def resize_drawing_canvas(self, scale=None):
plotting.resize_drawing_canvas(ax, scale=scale)

def _redraw_labels(self):
"""
Redraw axis labels, typically after draw or resize events.
"""

"""Redraw axis labels, typically after draw or resize events."""
ax = self.get_axes()
# Remove any previous labels
for mpl_object in self._to_remove:
mpl_object.remove()
self._to_remove = []
# Redraw the labels with the appropriate angles
for (label, position, rotation, kwargs) in self._labels.values():
label_data = list(self._labels.values())
label_data.extend(self._corner_labels.values())
for (label, position, rotation, kwargs) in label_data:
transform = ax.transAxes
x, y = project_point(position)
# Calculate the new angle.
Expand All @@ -347,7 +397,7 @@ def _redraw_labels(self):
def convert_coordinates(self, points, axisorder='blr'):
"""
Convert data coordinates to simplex coordinates for plotting
in the case that axis limits have been applied
in the case that axis limits have been applied.
"""
return convert_coordinates_sequence(points,self._boundary_scale,
self._axis_limits, axisorder)
Expand Down