Search code examples
pythonternary

Is there a way to rotate the ticklabels in a ternary plot with the python ternary package?


I am working with the python ternary package. To improve the intuitive understanding of the plot, I want to rotate the tick labels on the axis to indicate in which direction the gridlines of this axis point, like in the plot below.

enter image description here

Python-ternary produces this result with the following code

from matplotlib import pyplot as plt
import ternary

figure, ax = plt.subplots(figsize=(6, 6))
tax = ternary.TernaryAxesSubplot(ax=ax, scale=100)

# draw Boundary and Gridlines
tax.boundary(linewidth=2.0)
tax.gridlines(color="black", multiple=10)

# ticks settings
tax.ticks(
    axis='lbr',
    linewidth=1,
    multiple=20,
    offset=0.02,
    fontsize=12
)

# remove mpl axes
tax.get_axes().axis('off')
tax.clear_matplotlib_ticks()
ternary.plt.show() 

enter image description here

what I have tried so far is

  • tax.ticks(axis='b',rotation=45)
  • tax.ticks(axis='b',labelrotation=45)
  • tax.ticks(axis='b').set_rotation(45)

without success. any help is appreciated!


Solution

  • The cheap solution was of course just hack the tick labels in as annotations, which can be rotated with the rotation argument. not very elegant, but it solves the problem for now

    enter image description here

    from matplotlib import pyplot as plt
    import ternary
    
    fs = 16
    
    figure, ax = plt.subplots(figsize=(6, 6))
    tax = ternary.TernaryAxesSubplot(ax=ax, scale=100)
    
    # draw Boundary and Gridlines
    tax.boundary(linewidth=2.0)
    tax.gridlines(color="black", multiple=10)
    
    # define tick locations
    tick_locs = range(10, 100, 10)
    
    # add left ticks 
    for i in tick_locs:
        tax.annotate(
            text=str(i),
            position=[-10, 100-i +2, 90],
            rotation=300,
            fontsize=fs
        )
        # add tick lines
        tax.line(
            [-3 , i+3, 90],
            [0 , i, 90],
            color='k'
        )
        
    # add bottom ticks 
    for i in tick_locs:
        tax.annotate(
            text=str(i),
            position=[i - 2, -10, 90],
            rotation=60,
            fontsize=fs
        )
        # add tick lines
        tax.line(
            [i , -3, 90],
            [i , 0, 90],
            color='k'
        )
    
    # add right ticks
    for i in tick_locs:
        tax.annotate(
            text=str(i),
            position=[105-i, i-2, 0],
            rotation=0,
            fontsize=fs
        )
        # add tick lines
        tax.line(
            [100-i , i, 0],
            [103-i , i, 0],
            color='k'
        )
    
    # remove mpl axes
    tax.clear_matplotlib_ticks()
    ternary.plt.show()