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
Any progress on the contour plots? I am going to submit a publication that features python ternary soon, and some contour plots would be helpful. Should I just do this myself, or have you made progress?
I've played with it -- so far I've tried to use mpl's transformations and contour functions. The main difference versus ternary's current API is how mpl expects the data (numpy matrices in a specific format), which is somewhat unusual for ternary data. This is the easiest way to add contour functionality, with the downside being a lack of control of how the contour plots are actually made (other than passing through standard mpl parameters). If that's good enough for your purposes I can try to patch it in sometime soon.
I've also played with passing through to mpl's heatmapping functions as well in a similar manner (so that heatmaps can also be generated from arbitrary data rather than specifying values on a partition as the library current expects).
I recommend just creating a listed colormap with a couple of colors, that creates nice looking contours. I can dig out the code if this reply is not sufficient
@ojtwist Sorry for not responding to the request above. All I did back then was to project the data with ternary's functions and then use matplotlib's contour function. You could try something similar. I had issues getting the axes right.
Another approach would be to manually compute the contours and use ternary's curve plotting functions.
The "right" way to do this is using matplotlib's transformations (indeed the entire library could benefit), but that's a lot more work, and like @btweinstein I haven't personally encountered a situation where a contour plot would have been better than a heatmap.
I haven't personally encountered a situation where a contour plot would have been better than a heatmap
For statistical inference, it's common to show the boundaries of credible regions, regions that contain a specified fraction of the probability in a ternary distribution. From a statistical perspective, what's interesting is the probability in a region, not the density. So from a statistical perspective, contours are very interesting. I think this would be a valuable addition.
I'm sure there are useful scenarios for contour plots, but I don't have a personal need for them at the moment nor a lot of extra time to spend implementing this functionality. I'm happy to review and merge a PR that adds some or all of the functionality.
In the meantime, if you are able to compute the boundaries of the credible region(s) then you could easily plot them using tax.plot(). I would also guess that a heatmap would suffice to illustrate the credible regions.
I wasn't really asking you to implement contours (though I was hopeful!), just making the point that there are well-motivated and not-uncommon use cases. I may look into this.
I would also guess that a heatmap would suffice to illustrate the credible regions.
My first reaction to that was no, a heatmap of the PDF (what is typically available) isn't readily useful for this. However, one could build a mapping from density to P(region with larger density) and plot a heatmap of that. It would essentially portray credible region boundaries for all choices of contained probability. I've never seen anyone do that, but it's an interesting idea that I'll try to look into.
I was able to create "contours" by using a colormap with only a couple of colors, i.e. in the below image. It's not an ideal solution, but it may be helpful for you.
I was able to create "contours" by using a colormap with only a couple of colors, i.e. in the below image. It's not an ideal solution, but it may be helpful for you.
Hi!
is it possible to get the code for making this? I would much appreciate it. thanks!
Firstly, brilliant bit of kit @marcharper.. I'm currently trying to generate a contour plot from a scatter plot. So I'm curious if @btweinstein might be able to share some of the code you used to create the contours? Would be really cool to generate that plot for the data I'm looking at. This is work in progress for a publication to get this out there. Any help would be much appreciated!
I have a function, colorbar_hack, that creates the colorbar next to the diagram. I think that's the best thing I can offer. You can google how to create a colormap with less colors
@btweinstein Thanks very much for rooting around your old PhD stuff.. my PhD data is strewn about somewhere on a few HDD's, so kudos! I will give the colour mapping a go, I was hoping to show some sort of basic 'clustering' in the most basic sense by showing contours. Well see where I get!
A hacky way to plot the actual contours for some points would be to use matplotlib's contour function to compute the contours (example here), transform the data from numpy arrays to the format ternary expects (lists of vectors), and then replot with tax.plot(), which should then project the points correctly.
I recently needed to plot contours in a ternary plot as well. Thanks to the code linked by @marcharper, I created the following (semi minimal example) utilizing the matplotlib contour function to calculate contour lines from the shannon_entropy function from your examples.
My paper including ternary contours was finally accepted (PR for citation #176). Thanks to this nice project I created beautiful plots including contours based on the minimal example provided earlier #147. The result can be seen from the graphical abstract.
I'm in need of help in the contour plots,I just need to extract all the data points from the contour plot that I made.Can you please help me with this.
Activity
btweinstein commentedon Jan 11, 2016
Any progress on the contour plots? I am going to submit a publication that features python ternary soon, and some contour plots would be helpful. Should I just do this myself, or have you made progress?
marcharper commentedon Jan 12, 2016
I've played with it -- so far I've tried to use mpl's transformations and contour functions. The main difference versus
ternary's current API is how mpl expects the data (numpy matrices in a specific format), which is somewhat unusual for ternary data. This is the easiest way to add contour functionality, with the downside being a lack of control of how the contour plots are actually made (other than passing through standard mpl parameters). If that's good enough for your purposes I can try to patch it in sometime soon.I've also played with passing through to mpl's heatmapping functions as well in a similar manner (so that heatmaps can also be generated from arbitrary data rather than specifying values on a partition as the library current expects).
btweinstein commentedon Jan 12, 2016
I am finding that a listed colormap with a small amount of colors works well enough. Don't worry about it! Thanks for the info though.
olivier-s-j commentedon Oct 26, 2016
@marcharper would you be so kind to share the code you mention to create a contour plot?
btweinstein commentedon Oct 26, 2016
I recommend just creating a listed colormap with a couple of colors, that creates nice looking contours. I can dig out the code if this reply is not sufficient
olivier-s-j commentedon Oct 29, 2016
Thx for the help. I've tired it with a discrete colormap but it does not give the result I am looking for. Code for contour lines would be helpful.
marcharper commentedon Dec 3, 2016
@ojtwist Sorry for not responding to the request above. All I did back then was to project the data with ternary's functions and then use matplotlib's contour function. You could try something similar. I had issues getting the axes right.
Another approach would be to manually compute the contours and use ternary's curve plotting functions.
The "right" way to do this is using matplotlib's transformations (indeed the entire library could benefit), but that's a lot more work, and like @btweinstein I haven't personally encountered a situation where a contour plot would have been better than a heatmap.
tloredo commentedon Feb 13, 2018
For statistical inference, it's common to show the boundaries of credible regions, regions that contain a specified fraction of the probability in a ternary distribution. From a statistical perspective, what's interesting is the probability in a region, not the density. So from a statistical perspective, contours are very interesting. I think this would be a valuable addition.
marcharper commentedon Feb 17, 2018
I'm sure there are useful scenarios for contour plots, but I don't have a personal need for them at the moment nor a lot of extra time to spend implementing this functionality. I'm happy to review and merge a PR that adds some or all of the functionality.
In the meantime, if you are able to compute the boundaries of the credible region(s) then you could easily plot them using
tax.plot(). I would also guess that a heatmap would suffice to illustrate the credible regions.tloredo commentedon Feb 17, 2018
I wasn't really asking you to implement contours (though I was hopeful!), just making the point that there are well-motivated and not-uncommon use cases. I may look into this.
My first reaction to that was no, a heatmap of the PDF (what is typically available) isn't readily useful for this. However, one could build a mapping from density to P(region with larger density) and plot a heatmap of that. It would essentially portray credible region boundaries for all choices of contained probability. I've never seen anyone do that, but it's an interesting idea that I'll try to look into.
btweinstein commentedon Feb 17, 2018
I was able to create "contours" by using a colormap with only a couple of colors, i.e. in the below image. It's not an ideal solution, but it may be helpful for you.

tloredo commentedon Feb 18, 2018
Thanks; the same idea had occurred to me!
marcharper commentedon Apr 16, 2018
@btweinstein Do you mind if I add that plot as an example to the readme?
btweinstein commentedon Apr 18, 2018
Of course, go ahead! Sorry for the slow reply
samarthiith commentedon Nov 28, 2018
Hi!
is it possible to get the code for making this? I would much appreciate it. thanks!
bsburnham commentedon Mar 8, 2019
Firstly, brilliant bit of kit @marcharper.. I'm currently trying to generate a contour plot from a scatter plot. So I'm curious if @btweinstein might be able to share some of the code you used to create the contours? Would be really cool to generate that plot for the data I'm looking at. This is work in progress for a publication to get this out there. Any help would be much appreciated!
btweinstein commentedon Mar 10, 2019
Sure, all you have to do is use a colormap with less colors. Let me dig around from the code from my PhD and see if I can find where I created this...
btweinstein commentedon Mar 10, 2019
https://github.com/btweinstein/python-ternary/blob/master/ternary/colormapping.py
I have a function, colorbar_hack, that creates the colorbar next to the diagram. I think that's the best thing I can offer. You can google how to create a colormap with less colors
bsburnham commentedon Mar 12, 2019
@btweinstein Thanks very much for rooting around your old PhD stuff.. my PhD data is strewn about somewhere on a few HDD's, so kudos! I will give the colour mapping a go, I was hoping to show some sort of basic 'clustering' in the most basic sense by showing contours. Well see where I get!
Thanks again for your help!
marcharper commentedon Mar 16, 2019
A hacky way to plot the actual contours for some points would be to use
matplotlib's contour function to compute the contours (example here), transform the data fromnumpyarrays to the formatternaryexpects (lists of vectors), and then replot withtax.plot(), which should then project the points correctly.maximweb commentedon Aug 13, 2020
I recently needed to plot contours in a ternary plot as well. Thanks to the code linked by @marcharper, I created the following (semi minimal example) utilizing the matplotlib contour function to calculate contour lines from the
shannon_entropyfunction from your examples.Hope this helps all who had the same issue.
EDIT: Pull request: #147
marcharper commentedon Aug 13, 2020
Thanks @maximweb ! Feel free to open a PR to add this to the examples.
maximweb commentedon Jul 30, 2021
My paper including ternary contours was finally accepted (PR for citation #176). Thanks to this nice project I created beautiful plots including contours based on the minimal example provided earlier #147. The result can be seen from the graphical abstract.
marcharper commentedon Aug 10, 2021
Thanks for sharing your work @maximweb ! Looks great :)
Mukilesh-K commentedon Sep 15, 2021
I'm in need of help in the contour plots,I just need to extract all the data points from the contour plot that I made.Can you please help me with this.

maximweb commentedon Sep 28, 2021
@Mukilesh-K
Is this a python-ternary related issue?
Maybe the procedure I suggested in my ternary contour example could help?