You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Issues with corner label positions under panning/zooming #203
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()
@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.
'''
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)
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:
Go to Inside python-ternary/ternary/ternary_axes_subplot.py --> class TernaryAxesSubplot(object): --> def _redraw_labels(self):
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
...
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
...
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
...
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
...
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
...
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:
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'.)
I still did not update the title coordinate. The title is still attached to the axes coordinate.
Activity
bmondal94 commentedon Feb 1, 2023
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 calltax._redraw_labels()before that.Solution 2: Instead of calling
plt.show(), you can calltax.show().tax.show()inherentlly calltax._redraw_labels()before rendering the image.carlosgmartin commentedon Feb 1, 2023
@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 commentedon Feb 2, 2023
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:
Here is an example of data-coordinate vs axis-coordinate (orginal source)
Although you can move or zoom other texts but not the
colored text in axes coordstext.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) :
Solution:
Inside python-ternary/ternary/ternary_axes_subplot.py --> class TernaryAxesSubplot(object): --> def _redraw_labels(self):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 functionInside python-ternary/ternary/ternary_axes_subplot.py --> class TernaryAxesSubplot(object): --> def left_corner_label():and update the following part of the functionInside python-ternary/ternary/ternary_axes_subplot.py --> class TernaryAxesSubplot(object): --> def right_corner_label():and update the following part of the functionInside python-ternary/ternary/ternary_axes_subplot.py --> class TernaryAxesSubplot(object): --> def bottom_axis_label():and update the following part of the functionInside python-ternary/ternary/ternary_axes_subplot.py --> class TernaryAxesSubplot(object): --> def right_axis_label():and update the following part of the functionInside python-ternary/ternary/ternary_axes_subplot.py --> class TernaryAxesSubplot(object): --> def left_axis_label():and update the following part of the functionBTW, 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:
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'.)
I still did not update the title coordinate. The title is still attached to the axes coordinate.
Sorry, for the long answer.
bmondal94 commentedon Feb 2, 2023
BTW, there is an alternative solution if you do not want to change the source code. (source code is taken from)