python-ternary icon indicating copy to clipboard operation
python-ternary copied to clipboard

labels not working

Open jamesafoster opened this issue 9 years ago • 24 comments

The following code does not produce labels for me. I get the plot, but without any labels.

scale, fontsize = 1, 12
import matplotlib.pyplot as pyplot
figure, ax = pyplot.subplots()
tax = ternary.TernaryAxesSubplot(ax=ax)

tax.set_title("Evolution of 6-6-3 signaler games under ambiguity", 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)

tax.boundary(color="black", linewidth=2.0)
tax.gridlines(multiple=0.1, color="blue")

tax.plot( ((0.1,0.8,0.1),(0.8,0.1,0.1)) )

#tax.scatter( x, marker='s')
tax.legend()
tax.ticks(axis='lbr', color="black", linewidth=1, multiple=0.1)

tax.show()

jamesafoster avatar Nov 28 '15 20:11 jamesafoster

On the latest branch, the following (nearly identical) code works for me:

import ternary
import matplotlib.pyplot as pyplot

scale, fontsize = 1, 12
figure, ax = pyplot.subplots()
tax = ternary.TernaryAxesSubplot(ax=ax)

tax.set_title("Evolution of 6-6-3 signaler games under ambiguity", 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)

tax.boundary(linewidth=2.0)
tax.gridlines(multiple=0.1, color="blue")

tax.plot( ((0.1,0.8,0.1),(0.8,0.1,0.1)) )

#tax.scatter( x, marker='s')
tax.legend()
tax.ticks(axis='lbr', linewidth=1, multiple=0.1)

tax.show()

Image: http://postimg.org/image/qmh1f7mpl/full/

Are you using an older version of matplotlib? Someone else reported something similar a while ago. I just used mpl 1.4.2 on python 2.7 and python 3.4.3 (the Ubuntu packaged versions).

marcharper avatar Nov 28 '15 22:11 marcharper

Hi, I had similar issue with the label, I'm using spyder(anaconda) for mac on python 3.4

hareyakana avatar Nov 29 '15 13:11 hareyakana

I appreciate the reports but I really need more information. The matplotlib version seems to be important -- this may be an upstream issue, or something that can be worked around.

I don't have a Mac to test with.

marcharper avatar Nov 29 '15 16:11 marcharper

matpotlib-1.5 ipython-4.0.1 spyder-2.3.7

by adding _redraw_labels, i manage to get the labels for the 3 axis. not sure if this is intended

d.set_title(r"source flavour composition $\nu_e,\nu_\mu,\nu_\tau$",fontsize=20)
d.left_axis_label(r"$|\nu_e|$",fontsize=15)
d.right_axis_label(r"$|\nu_\mu|$",fontsize=15)
d.bottom_axis_label(r"$|\nu_\tau|$",fontsize=15)
d._redraw_labels()

hareyakana avatar Nov 29 '15 17:11 hareyakana

That is helpful -- the labels have to be redrawn on resizing and other operations so that the angles are right. I can add more rendering hooks.

If you show the image and resize the window do the labels appear? Perhaps there is a difference in rendering or event handling based on the backend or mpl version.

marcharper avatar Nov 29 '15 18:11 marcharper

figure_2

figure, d=t.figure(scale=1)

d.boundary(linewidth=2.0)
d.gridlines(multiple=0.1,color="blue",linewidth=0.8)
d.gridlines(multiple=0.02,color="green",linewidth=0.4)

d.set_title(r"source flavour composition $\nu_e,\nu_\mu,\nu_\tau$",fontsize=20)
d.left_axis_label(r"$|\nu_e|$",fontsize=15)
d.right_axis_label(r"$|\nu_\mu|$",fontsize=15)
d.bottom_axis_label(r"$|\nu_\tau|$",fontsize=15)
#d._redraw_labels()

d.ticks(axis='rlb',multiple=0.1,axes_colors={'l': 'g', 'r':'b', 'b': 'r'},clockwise=False,label=ticks)
p.axis('off')
point=[(1/3,1/3,1/3)]
d.scatter(point, marker='D', color='green', label="Green Diamonds",)
d.resize_drawing_canvas(scale=1.05)
d.legend
d.show()

after i uncomment _redraw_labels() line, I was able to get the label shown figure_1

hareyakana avatar Nov 29 '15 21:11 hareyakana

my ipython console renders the image in a separate interactive window, I could do it inline but would prefer a separate window to check on the plots. I have try tinker with the offset with various value but it just does not show the label without the _redraw_labels line.

while my code i pasted here, is there any way I could change the orientation of the ticks? with the clockwise=True but maintaining the same orientation as clockwise=False. I have check some of the past closed issues but can't get my head around it.

hareyakana avatar Nov 29 '15 21:11 hareyakana

I added the label draw callback to the "draw_event" of mpl -- hopefully that causes the labels to be drawn initially as expected. The label positioning does not take much into account: there's no easy way to predict what the latex will render to as far as I know, what all the font-sizes are, etc., so you'll have to adjust the offsets as needed.

I'm not sure what you are asking regarding the tick orientation -- the library implements the two standard choices. Can you be more specific?

marcharper avatar Nov 29 '15 22:11 marcharper

something similar to issue#31

I am trying to produce something similar to this https://inspirehep.net/record/1343979/files/flav_scan-eps-converted-to.png

but I can't seem to get for example the bottom axis to go 0 to 1 instead of 1 to 0. When I try to add clockwise=True to ticks. It does go 0 to 1 but the orientation changes.

from issue #31,`I can't seem to understand the phrase u mentioned "You can pass clockwise=True to ticks to reverse the orientation (which changes the tick angles)." right after the second plot image. would you elaborate on that?

hareyakana avatar Nov 29 '15 23:11 hareyakana

Resizing didn’t help, but…adding _redraw_labels() DID!

I haven’t pulled the latest github version, so this is using the version I had with my original comment.

j

On Nov 29, 2015, at 2:16 PM, Marc Harper, PhD [email protected] wrote:

I added the label draw callback to the "draw_event" of mpl -- hopefully that causes the labels to be drawn initially as expected. The label positioning does not take much into account: there's no easy way to predict what the latex will render to as far as I know, what all the font-sizes are, etc., so you'll have to adjust the offsets as needed.

I'm not sure what you are asking regarding the tick orientation -- the library implements the two standard choices. Can you be more specific?

— Reply to this email directly or view it on GitHub https://github.com/marcharper/python-ternary/issues/36#issuecomment-160475931.

jamesafoster avatar Nov 30 '15 17:11 jamesafoster

@hareyakana You can manually give the tick labels as an argument to tax.ticks(ticks=[]) to change the default labeling order. If you need something other than the two orientations for some reason them you'll have to transform the data before plotting.

marcharper avatar Nov 30 '15 17:11 marcharper

@jamesafoster that's bizarre. I'm glad there is a workaround for now. My guess is that various combinations of mpl backend and version trigger events differently, and so sometimes label drawing doesn't occur.

marcharper avatar Nov 30 '15 20:11 marcharper

If either of you can check the github master branch that would be most appreciated. If it's fixed I'll push a new pypi version.

marcharper avatar Nov 30 '15 20:11 marcharper

I still don’t get labels unless I re-size (yes, that works now…I think I may not have tested it correctly before), or redraw the axes.

I get the following message: /Users/jamesafoster/anaconda/lib/python3.4/site-packages/matplotlib/axes/_axes.py:475: UserWarning: No labelled objects found. Use label='...' kwarg on individual plots. warnings.warn("No labelled objects found. "

To be honest, I’m not sure I downloaded the correct build. On gitgub, I think I’m looking at teh master branch, and I downloaded and installed that.

Thanks for looking into this!

j

James A. Foster
Professor, University of Idaho
Institute for Bioinformatics & Evolutionary STudies (IBEST)


> On Nov 30, 2015, at 12:19 PM, Marc Harper, PhD <[email protected]> wrote:
> 
> If either of you can check the github master branch that would be most appreciated. If it's fixed I'll push a new pypi version.
> 
> —
> Reply to this email directly or view it on GitHub <https://github.com/marcharper/python-ternary/issues/36#issuecomment-160749250>.
> 

jamesafoster avatar Nov 30 '15 21:11 jamesafoster

I have tried with the latest master branch and it does not show the axis label without _redraw_labels()

hareyakana avatar Nov 30 '15 22:11 hareyakana

Ok, thanks. Unfortunately I still cannot reproduce the issue, so for now you'll have to call _redraw_labels() manually.

marcharper avatar Dec 01 '15 03:12 marcharper

In our earlier exchange, you had one example of code that moved labels out from the ternary plot tick marks. But I lost it! How did you do that??

This is what I have, and I’m trying to make it look better.

Thanks for any pointers.

James A. Foster
Professor, University of Idaho
Institute for Bioinformatics & Evolutionary STudies (IBEST)




> On Nov 29, 2015, at 9:39 AM, hareyakana <[email protected]> wrote:
> 
> matpotlib-1.5
> ipython-4.0.1
> spyder-2.3.7
> 
> by adding _redraw_labels, i manage to get the labels for the 3 axis. not sure if this is intended
> 
> d.set_title(r"source flavour composition $\nu_e,\nu_\mu,\nu_\tau$",fontsize=20)
> d.left_axis_label(r"$|\nu_e|$",fontsize=15)
> d.right_axis_label(r"$|\nu_\mu|$",fontsize=15)
> d.bottom_axis_label(r"$|\nu_\tau|$",fontsize=15)
> d._redraw_labels()
> —
> Reply to this email directly or view it on GitHub <https://github.com/marcharper/python-ternary/issues/36#issuecomment-160435267>.
> 

jamesafoster avatar Dec 19 '15 04:12 jamesafoster

I just resize the window after plotting until the spacing looks right; you can also set the image size before calling pyplot.savefig(). Unfortunately this isn't something that is easy to do automatically because of differences in font size and screen size. If anyone has ideas, please share.

marcharper avatar Dec 19 '15 18:12 marcharper

Thanks for that!

You also had a call that moved the tick labels and the labels on the edges OUT further. I think it was a call to an underlying matplotlib method, but I can’t find or remember what it was. Do you? (I’m new with matplotlib, so I suspect the answer is not specific to ternary and is probably known to most people who use matplotlib).

j

On Dec 19, 2015, at 10:32 AM, Marc Harper [email protected] wrote:

I just resize the window after plotting until the spacing looks right; you can also set the image size before calling pyplot.savefig(). Unfortunately this isn't something that is easy to do automatically because of differences in font size and screen size. If anyone has ideas, please share.

— Reply to this email directly or view it on GitHub https://github.com/marcharper/python-ternary/issues/36#issuecomment-166012787.

jamesafoster avatar Dec 19 '15 18:12 jamesafoster

I don't recall anything like that, but some of the ternary methods do take an offset parameter that should move the labels outward a bit. Resizing the image window after pyplot.show() does have a similar effect in many cases. You can also reduce the font size.

I would prefer the library to do this automatically, but AFAIK there is not a good way to determine the rendering size of a label beforehand. I'll look into again.

marcharper avatar Dec 19 '15 18:12 marcharper

Thanks, Marc. Don’t spend too much time on my account. I’ll follow up these hints.

j

On Dec 19, 2015, at 10:57 AM, Marc Harper [email protected] wrote:

I don't recall anything like that, but some of the ternary methods do take an offset parameter that should move the labels outward a bit. Resizing the image window after pyplot.show does have a similar effect in many cases. You can also reduce the font size.

I would prefer the library to do this automatically, but AFAIK there is not a good way to determine the rendering size of a label beforehand. I'll look into again.

— Reply to this email directly or view it on GitHub https://github.com/marcharper/python-ternary/issues/36#issuecomment-166014080.

jamesafoster avatar Dec 19 '15 18:12 jamesafoster

Let us know if you find anything useful. This is something the library could probably handle better.

marcharper avatar Dec 19 '15 19:12 marcharper

I ran into a similar issue that labels are not drawn under certain circumstances. It seems to be related to the fact that rasterized outputs are treated differently by matplotlib than vector images.

The following code produces the expected output if rendered to .png, but the labels are missing from the diagrams if it's exported to .pdf, unless _redraw_labels() is called manually before saving.

I assume that when exporting to *.pdf, *.svg and *.eps, there is no draw_event emitted by matplotlib. Is there a better way to do this?

fig, axes = plt.subplots(1,2,figsize=(5, 2))
fig, tax1 = ternary.figure(scale=4, ax=axes[0])
fig, tax2 = ternary.figure(scale=4, ax=axes[1])

for tax in [tax1, tax2]:
    tax.boundary()
    tax.bottom_axis_label("a")
    tax.right_axis_label("b")
    tax.left_axis_label("c")
for ax in axes:
    ax.axis("off")
tax2._redraw_labels()
fig.savefig("./testtriangles.pdf", bbox_inches='tight')  # Only labels of tax2 are drawn
fig.savefig("./testtriangles.png", bbox_inches='tight')  # All labels are drawn

michaelbuehlmann avatar Dec 19 '18 10:12 michaelbuehlmann

For now my recommendation is to use _redraw_labels(). Unfortunately we're not also wrapping fig because the solution in that case would be to override savefig to always redraw first. And that won't work for tax because it could be part of a larger plot or there could be multiple figures.

marcharper avatar Dec 20 '18 15:12 marcharper