0

I have the following Python script using Matplotlib and the ternary library. It generates a ternary plot with some labels on the vertices and axes:

import matplotlib.pyplot as plt
import ternary

# Cleaning up
plt.close('all')

# Open figure
scale = 100
fig, tax = ternary.figure(scale=scale)
tax.ticks(axis='lbr', multiple=10, linewidth=1, offset=0.025)
tax.get_axes().axis('off')
tax.clear_matplotlib_ticks()

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

# Vertex labels
tax.left_corner_label('A')
tax.right_corner_label('B')
tax.top_corner_label('C')

# Axis labels
tax.left_axis_label("$\longleftarrow$ A [%]",    va='center', ha='center', offset=0.15)
tax.bottom_axis_label("B [%] $\longrightarrow$", va='center', ha='center', offset=0.11)
tax.right_axis_label("$\longleftarrow$ C [%]",   va='center', ha='center', offset=0.15)

# Save figure
fig.savefig('ternary_plot_1.png')
fig.savefig('ternary_plot_2.png')

I call the savefig command twice directly after each other, and the resulting images look as those below. Notably the first does not show the labels on vertices and axis, while the second does. Why is that?

It behaves the same when saving as .eps or .svg. I have never seen this behavior with 'normal' plots. I suspect it has something to do with the way these labels are implemented in the ternary libary, but why would savefig behave differently when called a second time?

ternary_plot_1.png ternary_plot_2.png

Rob H.
  • 81
  • 1
  • 7
  • I get `module 'ternary' has no attribute 'figure'`. Are you using the latest version of both? – Roelant Mar 11 '21 at 13:03
  • 1
    I bet the issue is [this line in the repository](https://github.com/marcharper/python-ternary/blob/master/ternary/ternary_axes_subplot.py#L89). The labels are drawn on each `draw_event`. `fig.savefig` triggers a draw event but then the labels are drawn *after* the save. What happens if you prefix the save with `fig.canvas.draw()`? – Paul Brodersen Mar 11 '21 at 13:11
  • @Roelant I just installed ternary a few hours ago, so yes. – Rob H. Mar 11 '21 at 13:44
  • @PaulBrodersen That indeed resolves it. Placing that before saving the figures fixes it. So that means....? – Rob H. Mar 11 '21 at 13:45
  • 3
    ... that the library could be designed a bit better? I don't know why `tax._redraw_labels` isn't simply called at the end of `tax.left_axis_label`, etc. Presumably, the author knows the reason. I would raise an issue on github, or propose a pull request with a different draw logic. – Paul Brodersen Mar 11 '21 at 13:51
  • 1
    Thanks Paul. Appears to be a known issue aparetly: https://github.com/marcharper/python-ternary/issues/36 – Rob H. Mar 11 '21 at 16:08

0 Answers0