marcharper / python-ternary

:small_red_triangle: Ternary plotting library for python with matplotlib
MIT License
731 stars 157 forks source link

Issues with corner label positions under panning/zooming #203

Open carlosgmartin opened 1 year ago

carlosgmartin commented 1 year ago

I'm having some issues with the corner labels:

from matplotlib import pyplot as plt
import ternary

scale = 10
fig, ax = plt.subplots(constrained_layout=True)
fig, tax = ternary.figure(scale=scale, ax=ax)
tax.heatmap(data, scale, cmap='gray')
tax.get_axes().axis('off')
tax.clear_matplotlib_ticks()
tax.right_corner_label(1)
tax.top_corner_label(2)
tax.left_corner_label(3)
plt.show()

When the plot is first shown, the corner labels are missing:

Only after panning or zooming once do the labels appear (they also appear in the saved figure, if I save it):

Furthermore, the corner labels are positioned incorrectly under panning or zooming:

I am using macOS 11.7, python 3.10.9, matplotlib 3.6.3, and python-ternary 1.0.8.

bmondal94 commented 1 year ago

Explanation: This is a rendering issue. You can find it here: Known-Issues.

Solution 1: If you want to use plt.show() at the end, then call tax._redraw_labels() before that.

...
tax.right_corner_label(1)
tax.top_corner_label(2)
tax.left_corner_label(3)

tax._redraw_labels()  # <------- This you have to add before calling plt.show()
plt.show()

Solution 2: Instead of calling plt.show(), you can call tax.show(). tax.show() inherentlly call tax._redraw_labels() before rendering the image.

...
tax.right_corner_label(1)
tax.top_corner_label(2)
tax.left_corner_label(3)

# plt.show <----- This is commented. Instead tax.show() is called.
tax.show()
carlosgmartin commented 1 year ago

@bmondal94 Thanks. The labels are still incorrectly positioned after panning/zooming. They should be positioned at the vertices of the triangle, but appear to be fixed to the UI layer.

bmondal94 commented 1 year ago
'''
The labels are still incorrectly positioned after panning/zooming. They should be positioned at the 
vertices of the triangle, but appear to be fixed to the UI layer.
'''

Dear @carlosgmartin , Thanks for the question again. Explanation: The plotting is done using data coordinates but the axes labels are done using axes coordinates. That's why the labels remain fixed on the axes when you move or zoom. Same for the axes title. Below is the snippet of the code:

Inside python-ternary/ternary/ternary_axes_subplot.py --> class TernaryAxesSubplot(object): --> def _redraw_labels(self):

transform = ax.transAxes
text = ax.text(x, y, label, rotation=new_rotation,transform=transform, horizontalalignment="center",**kwargs)

Here is an example of data-coordinate vs axis-coordinate (orginal source)

import matplotlib.pyplot as plt

fig = plt.figure()
fig.suptitle('bold figure suptitle', fontsize=14, fontweight='bold')
ax = fig.add_subplot()
fig.subplots_adjust(top=0.85)
ax.set_title('axes title')
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
ax.text(3, 8, 'boxed italics text in data coords', style='italic',
        bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})
ax.text(2, 6, r'an equation: $E=mc^2$', fontsize=15)
ax.text(3, 2, 'Unicode: Institut f\374r Festk\366rperphysik')
ax.text(0.95, 0.01, 'colored text in axes coords',
        verticalalignment='bottom', horizontalalignment='right',
        transform=ax.transAxes,
        color='green', fontsize=15)
ax.plot([2], [1], 'o')
ax.annotate('annotate', xy=(2, 1), xytext=(3, 4),
            arrowprops=dict(facecolor='black', shrink=0.05))
ax.set(xlim=(0, 10), ylim=(0, 10))
plt.show()

Although you can move or zoom other texts but not the colored text in axes coords text.

Now, coming back to the ternary plot problem; In some sense, you can think that your actual 'plot-figure' is not attached to the 'axes lables'. BTW, the ticks in the 'plot-figure' were drawn in data-coordinate. Just for fun (original source) :

import ternary

scale = 100
figure, tax = ternary.figure(scale=scale)

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

# Set Axis labels and Title
fontsize = 12
tax.set_title("Various Lines\n", fontsize=fontsize, pad=20)
tax.right_corner_label("X", fontsize=fontsize)
tax.top_corner_label("Y", fontsize=fontsize)
tax.left_corner_label("Z", fontsize=fontsize)
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)

# Draw lines parallel to the axes
tax.horizontal_line(16)
tax.left_parallel_line(10, linewidth=2., color='red', linestyle="--")
tax.right_parallel_line(20, linewidth=3., color='blue')

# Draw an arbitrary line, ternary will project the points for you
p1 = (22, 8, 10)
p2 = (2, 22, 16)
tax.line(p1, p2, linewidth=3., marker='s', color='green', linestyle=":")

tax.ticks(axis='lbr', multiple=5, linewidth=1, offset=0.025)
# tax.get_axes().axis('off')
# tax.clear_matplotlib_ticks()
tax.show()

Solution:

  1. Go to Inside python-ternary/ternary/ternary_axes_subplot.py --> class TernaryAxesSubplot(object): --> def _redraw_labels(self):
  2. Update the function
    def _redraw_labels(self):
        """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
        label_data = list(self._labels.values())
        label_data.extend(self._corner_labels.values())
        ScaleMatrix = np.array([[self._scale,0],[0,self._scale],[0,0]]) # <--- This matrix is the scaling matrix
        for (label, position, rotation, kwargs) in label_data:
            # transform = ax.transAxes  # <---- Comment the change to axes-coordinate
            # x, y = project_point(position) # <--- Transform the triangular-coordinates to Eucledian coordinate
            transform = ax.transData # <---- I want to be in the data coordinate
            x, y = project_point(np.dot(position,ScaleMatrix)) # <--- Before the coordinate transformation, scale the data using the scaling matrix
            # Calculate the new angle.
            position = np.array([x, y])
            new_rotation = ax.transData.transform_angles(
                np.array((rotation,)), position.reshape((1, 2)))[0]
            text = ax.text(x, y, label, rotation=new_rotation,
                           transform=transform, horizontalalignment="center",
                           **kwargs)
            text.set_rotation_mode("anchor")
            self._to_remove.append(text)

    Simple enough. But we are not finished yet. Because the axes labels use offset values to nicely position the labels. But as now we are rescaling the data we have to take care of rescaling the offset values as well. After a few trials and error, I found the best options are:

  3. Go to Inside python-ternary/ternary/ternary_axes_subplot.py --> class TernaryAxesSubplot(object): --> def top_corner_label(): and update the following part of the function
    ...
    if not position:
            #position = (-offset / 2, 1 + offset, 0)  # <--- Old poition
            position = (-offset*0.5/2, 1 + offset*0.5, 0) # <-- New position
    ...
  4. Go to Inside python-ternary/ternary/ternary_axes_subplot.py --> class TernaryAxesSubplot(object): --> def left_corner_label(): and update the following part of the function
    ...
    if not position:
            #position = (-offset / 2, offset / 2, 0) # <--- Old poition
            position = (-0.5*offset / 2, -2*offset / 2, 0) # <-- New position
    ...
  5. Go to Inside python-ternary/ternary/ternary_axes_subplot.py --> class TernaryAxesSubplot(object): --> def right_corner_label(): and update the following part of the function
    if not position:
            #position =  (1, offset / 2, 0) # <--- Old poition
            position = (1+ 2.5*offset /2, -2*offset / 2, 0) # <-- New position
    ...
  6. Go to Inside python-ternary/ternary/ternary_axes_subplot.py --> class TernaryAxesSubplot(object): --> def bottom_axis_label(): and update the following part of the function
    if not position:
            #position =  (0.5, -offset / 2., 0.5) # <--- Old poition
            position = (0.5, -15*offset / 2., 0.5) # <-- New position
    ...
  7. Go to Inside python-ternary/ternary/ternary_axes_subplot.py --> class TernaryAxesSubplot(object): --> def right_axis_label(): and update the following part of the function
    if not position:
            #position =  (2. / 5 + offset, 3. / 5, 0) # <--- Old poition
            position = (2. / 5 + 1.5*offset, 3. / 5, 0) # <-- New position
    ...
  8. Go to Inside python-ternary/ternary/ternary_axes_subplot.py --> class TernaryAxesSubplot(object): --> def left_axis_label(): and update the following part of the function
    if not position:
            #position =  (-offset, 3./5, 2./5) # <--- Old poition
            position = (-1.5*offset, 3./5, 2./5) # <-- New position
    ...

    BTW, I do not know why most of the offsets are divided by 2. So, I kept the 'previous factors' as it is and did minimal changes. Final images: original image: Figure_1 after moving to the left: (the 'axes' labels now move with the 'plot-figure'. In some sense, the 'axes-labels' are now attached to the 'plot-figures'.) Figure_2

I still did not update the title coordinate. The title is still attached to the axes coordinate.

Sorry, for the long answer.

bmondal94 commented 1 year ago

BTW, there is an alternative solution if you do not want to change the source code. (source code is taken from)

import ternary

scale = 40
figure, tax = ternary.figure(scale=scale)

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

#---------- do not label using tax functions --------------
# # Set Axis labels and Title
# fontsize = 12
# tax.set_title("Various Lines\n", fontsize=fontsize, pad=20)
# tax.right_corner_label("X", fontsize=fontsize)
# tax.top_corner_label("Y", fontsize=fontsize)
# tax.left_corner_label("Z", fontsize=fontsize)
# 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)

# ------ get the matplotlib axis and add the text on it using data coordinate------------------
# This is just one example
BOTTOMPOS = ternary.helpers.project_point([0.5,-0.2]) * scale  # <-- The project_point() changes the triangular coordinate to Eucledian coordinate.
tax.get_axes().text(BOTTOMPOS[0],BOTTOMPOS[1],"Bottom label $\\Gamma - \\Omega$")

# Draw lines parallel to the axes
tax.horizontal_line(16)
tax.left_parallel_line(10, linewidth=2., color='red', linestyle="--")
tax.right_parallel_line(20, linewidth=3., color='blue')

# Draw an arbitrary line, ternary will project the points for you
p1 = (22, 8, 10)
p2 = (2, 22, 16)
tax.line(p1, p2, linewidth=3., marker='s', color='green', linestyle=":")

tax.ticks(axis='lbr', multiple=5, linewidth=1, offset=0.025)
tax.get_axes().axis('off')
tax.clear_matplotlib_ticks()
tax.show()