marcharper / python-ternary

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

labels not working #36

Open jamesafoster opened 8 years ago

jamesafoster commented 8 years ago

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()
marcharper commented 8 years ago

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).

hareyakana commented 8 years ago

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

marcharper commented 8 years ago

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.

hareyakana commented 8 years ago

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()
marcharper commented 8 years ago

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.

hareyakana commented 8 years ago

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 commented 8 years ago

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.

marcharper commented 8 years ago

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?

hareyakana commented 8 years ago

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?

jamesafoster commented 8 years ago

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 notifications@github.com 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.

marcharper commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

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.

jamesafoster commented 8 years ago

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 <notifications@github.com> 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>.
> 
hareyakana commented 8 years ago

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

marcharper commented 8 years ago

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

jamesafoster commented 8 years ago

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 <notifications@github.com> 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>.
> 
marcharper commented 8 years ago

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.

jamesafoster commented 8 years ago

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 notifications@github.com 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.

marcharper commented 8 years ago

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.

jamesafoster commented 8 years ago

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 notifications@github.com 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.

marcharper commented 8 years ago

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

michaelbuehlmann commented 5 years ago

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
marcharper commented 5 years ago

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.