Contour Ternary - figure factory ( Can't set scale on color bar)
Cannot set the color bar limits. The color scale it's automatically given, but if I want to have the same color scale on two or more plots to compare I cannot do this.
EDIT: To clarify this is only an issue for this plot in figure factory - I have browsed the back end and can't identify any code for the user to set the limits of the color bar to scale it, I believe this is missing. This is an issue when plotting multiple ternary plots for comparison to see how a feature has changed with respect to another variable (which would be required for academic publishing of these types of figures).
Dear @is95t
I am facing the same issue! Have you found a solution?
Dear @nicolaskruchten Would you give a hand to sort out this issue to be able t:
- set the limits of the color bar and set even a specific values which can have different interval e.g. [0, 5, 10, 50, 100, 200]
- adding values to the contour lines.
I played with this a little bit and there doesn't seem to be a straightforward way to do what you're looking for, sorry :( The Figure Factories are fairly rigid recipes for producing figures and they then to produce figures that are hard to modify ex post. In this case there is not one trace that has a tweakable color scale, there appears to be one trace per contour with a hardcoded color.
If you really need to customize things, you're best off looking at how the FF works under the hood and copy-pasting the code and modifying it to taste. The code in question is here https://github.com/plotly/plotly.py/blob/master/packages/python/plotly/plotly/figure_factory/_ternary_contour.py
I searched the plotly forum for a solution to your question.s I found one for the colorbar ticks:
import plotly.figure_factory as ff
import numpy as np
Al = np.array([0. , 0. , 0., 0., 1./3, 1./3, 1./3, 2./3, 2./3, 1.])
Cu = np.array([0., 1./3, 2./3, 1., 0., 1./3, 2./3, 0., 1./3, 0.])
Y = 1 - Al - Cu
enthalpy = 2.e6 * (Al - 0.01) * Cu * (Al - 0.52) * (Cu - 0.48) * (Y - 1)**2 - 5000
fig = ff.create_ternary_contour(np.array([Al, Y, Cu]), enthalpy,
pole_labels=['Al', 'Y', 'Cu'],
interp_mode='cartesian',
ncontours=20,
colorscale='Viridis',
showscale=True,
title='Mixing enthalpy of ternary alloy')
fig.update_traces(marker_colorbar_tickvals=[-20000, -17000, -11000, -5000, 408],
marker_colorbar_ticktext=[-20000, -17000, -11000, -5000, 408], selector=dict(marker_showscale=True))
To add values to the contour lines is a nightmare, as you can see from this post: https://community.plotly.com/t/latexify-ternary-countour-plot-colobar/64470/5
Thank you @nicolaskruchten and @empet for the replies. I'll have a look at the solution and see if that works. Also happy to copy the backend and have a look to see if I can find a solution there. Thanks for the help and I'll report back with how that turns out
@empet I had a look at the solution you found, unfortunately this doesn't solve the problem. The solution will allow you to label ticks on the color bar but will not scale it. What we are looking for is to set the color scale of the bar (and plot), so that the color scale can be the same across multiple plots. For example:
plot 1: contour values 150 - 250 plot 2: contour values 250 - 300 plot 3 contour values 300 - 350
I want to scale the colors on the plot and bar to a scale of 150 to 350 so that the plots are comparable.
When I have some time I will have a look though the backend and see if I can find a workaround
What you want is not to scale the color range, because scaling (in mathematics) means multiplying all values in a range by the same scalar a>0. You want to extend or to reduce this color range. To perform such an action on the z-values in this particular contour plot, you must inspect the fig.data. fig.data in a ternary contour consists in n>2 traces. The first n-1 have mode="lines", and the last one, mode="markers". in fig.data[-1] you can set marker_cmin, and marker_cmax as the modified color range:
Example:
import plotly.figure_factory as ff
import numpy as np
from plotly.subplots import make_subplots
import plotly.graph_objects as go
Al = np.array([0. , 0. , 0., 0., 1./3, 1./3, 1./3, 2./3, 2./3, 1.])
Cu = np.array([0., 1./3, 2./3, 1., 0., 1./3, 2./3, 0., 1./3, 0.])
Y = 1 - Al - Cu
enthalpy1 = 2.e6 * (Al - 0.01) * Cu * (Al - 0.52) * (Cu - 0.48) * (Y - 1)**2 - 5000
fig1 = ff.create_ternary_contour(np.array([Al, Y, Cu]), enthalpy1,
pole_labels=['Al', 'Y', 'Cu'],
interp_mode='cartesian',
ncontours=20,
colorscale='Viridis',
showscale=True,
title='Enthalpy 1')
print(enthalpy1. min(), enthalpy1.max())
-20021.827160493824 408.0
Let us set cmin, and cmax in fig1.data[-1]:
fig1.update_traces(marker_cmin=-25000, marker_cmax=1000, selector=dict(mode="markers"))
Now compare the range of color value in the initial fig1, and after this update.
In a subplot you should set the same coloraxis for each subplot and automaticaly the marker_color values are mapped on the colorscale with cmin = minimum (among all marker_color value), and correspondingly for cmax:
We define here a subplot with two cells, each one containing a ternary contour (the same as above but each one with a different enthalpy, i.e. a different marker_color range).
import plotly.figure_factory as ff
import numpy as np
from plotly.subplots import make_subplots
import plotly.graph_objects as go
Al = np.array([0. , 0. , 0., 0., 1./3, 1./3, 1./3, 2./3, 2./3, 1.])
Cu = np.array([0., 1./3, 2./3, 1., 0., 1./3, 2./3, 0., 1./3, 0.])
Y = 1 - Al - Cu
enthalpy1 = 2.e6 * (Al - 0.01) * Cu * (Al - 0.52) * (Cu - 0.48) * (Y - 1)**2 - 5000
#define the first ternary contour
fig1 = ff.create_ternary_contour(np.array([Al, Y, Cu]), enthalpy1,
pole_labels=['Al', 'Y', 'Cu'],
interp_mode='cartesian',
ncontours=20,
colorscale='Viridis',
#showscale=False,
title='Enthalpy 1')
#define the second ternary contour of different range for marker_color (i.e. range of enthalpy2)
enthalpy2 =2.5*1e6* (Al - 0.01) * Cu * (Al - 0.52) * (Cu - 0.48) * (Y - 1)**2 - 8000
print(enthalpy2.min(), enthalpy2.max())
fig2 = ff.create_ternary_contour(np.array([Al, Y, Cu]), enthalpy2,
pole_labels=['Al', 'Y', 'Cu'],
interp_mode='cartesian',
ncontours=15,
colorscale='Viridis',
title='Enthalpy 2')
# the last trace in each fig must be modified because it has a colorscale set by default, and we want to set it in
#the common coloraxis
def change_last_trace(fig):
#function that modifies the definition of the last trace in a fig representing a ternary contour
aux = fig.data[-1]
return go.Scatterternary(a=aux["a"], b=aux["b"], c=aux["c"], mode="markers", marker_color=aux["marker"]["color"],
marker_coloraxis="coloraxis", opacity=0)
#define subplot fig, figs
figs = make_subplots(rows=1, cols=2, specs=[[{"type": "ternary"}, {"type": "ternary"}]], horizontal_spacing=0.1)
# add the first n-1 traces in fig1 to the subplot cell (1m1)
for k in range(len(fig1.data)-1):
figs.add_trace(fig1.data[k], row=1, col=1)
#and finally the modified last trace in fig1
figs.add_trace(change_last_trace(fig1), row=1, col=1)
#proceed similarly for fig2
for k in range(len(fig2.data)-1):
figs.add_trace(fig2.data[k], row=1, col=2)
figs.add_trace(change_last_trace(fig2), row=1, col=2)
figs.update_layout( height=400, showlegend=False, coloraxis=dict(colorscale="viridis", colorbar_thickness=24))
The range of enthalpy1 (i.e. z-values for a normal contour plot) is [-20021.82, 408], while for enthalpy2 is
[-26777.28, -1240.], and the cmin on colorbar is -26777, while cmax= 408, but 408 is small compared with the absolute value of the listed tick values and is displayed only 0, i.e. we achieved what you required.

Thanks a lot @empet Is there any way to add contour line's values in the ternary plot?
@azayoud I posted this link https://community.plotly.com/t/latexify-ternary-countour-plot-colobar/64470/5 in my previous answer, yesterday :)
Would see this plot, I mean adding this line's values with one command, not adding text to the plot.
https://ms609.github.io/Ternary/articles/Ternary_files/figure-html/contours-1.png https://ms609.github.io/Ternary/articles/Ternary.html
I don't understand your question. You say: " I mean adding this line's values with one command", but I cannot see any particular line.
@empet thank you for your kind replay.
Please, see this plot, is it possible to add the values of contour's lines as well?
Yes, you can add values of some contour lines, but with a hard work. How this can be done is explained in an answer to a question posted on the Plotly forum. I paste this link https://community.plotly.com/t/latexify-ternary-countour-plot-colobar/64470/5 a third time in this topic. Here it is explained how to add the corresponding values to the contour lines.
@empet thank you for the response and apologies for my delayed response. I implemented you solution, however, I have an issue. This is the figure I get, with the two plots side by side and the shape of the contours are correct:

However, when plotting indiviually you can see that the coloring is not correct. For the first figure the largest value is 3500, but on the side by side plot it looks like ~4500 (left most figure of side by side)

for reference this is the right most figure of the side by side:

It seems that the color bar is representing the whole range, but the coloring on the figures is incorrect? Visually the left most figure should just go up to blue (~3500) on the scale not into the yellow/green shades.
Hope that makes sense, I will review and see if I can spot anything.
Thanks
To get an answer just take your fig and
for k in range(len(fig.data)):
print(fig.data[k])
to inspect each trace that belongs to fig.data, in order to realize what is going on.
This kind of plot consists in a lot of tricks, which can be discovered by such a thorough inspection.
@empet I can see what you're saying, however I have some concerns about this solution. In the first part you implement
fig.update_traces(marker_cmin=2500, marker_cmax=4500, selector=dict(mode="markers"))
However, this just updates the color bar number, not the contours. For example here is my plot:

When I implement the min/max, we get this:

You can see the number on the bar have changes, but this now implies that the lowest value data on the plot is 2500 which is incorrect. What I am looking for (and what I incorrectly referred to as scaling) is to have the colors of the contours across two plots represented by one scale. So that if the first plot the data is between 0 and 100, and in the other plot the data is between 100 and 200, the colors on both plot represent 0 to 200, not just reassigning the numbers on a colorbar. I will take another rummage through the backend to see if I can make some progress here.
@is95t
When a range of values, [a, b], is mapped to a colorscale, and cmin, cmax are not set, the colorbar exhibits tick values in the interval [a,b].
In a subplot with two heatmaps or contour plots, for example, if the range of z-values is [20, 50] for the trace in the first subplot cell, while in the trace from the second subplot cell is [28, 63], then defining a common coloraxis, the range [20, 63] is mapped to the same colorscale. In this case the colors exhibited by the first plot do not coincide with the colors associated in a single (separate) plot, because in a subplot, the range [20, 50] is mapped only on a "continuous" subinterval of colors in the colorscale. This is common way of mapping values to colorscales, typical for all plotting libraries. That's why the colors and colorbar tickvals are different for single contour plot and subplots of contours, with a common coloraxis.
@empet. thank you for the response on this. I am a little confused by you reply.
I am not trying to match the colors from the subplotted figures to the single plotted figures.
My point is that for the first figure of the subplot:
-
If we look at this figure in the single plot, we see values for z-values in the range [3000, 3500]
-
Now if we look at the subpotted figure (the ternary on the left) and used the colorbar for the z-values, it would imply the z-values for the data are in the range ~[3250, 4400]
Does that make sense? Sorry if I caused confusion before.
@is95t To test your hypothesis, I performed the following experiment:
import plotly.figure_factory as ff
import numpy as np
A = np.array([0, .3 ,.25, .34 ,0, .4 ,.65, 0.05, 0, 1, .47, .2, .7]) #pos 10 1, 0, 0
B = np.array([1, .1, .45, .56, 0, .5, .3, 0.75, .85, 0, .33, .3, .13])
C = np.array([0, .6 ,.3, .1, 1, .1, .05, .2, .15, 0, .2, .5, .17])
z=np.array([1.27036107, 1.27893858, 0.52255697, 1.50035059, 0.84853798,
1.27722501, 1.20920733, 0.88965008, 0.59293362, 0.9223051 ,
1.57173859, 1.33606612, 1.08977333])
fig = ff.create_ternary_contour(np.array([A, B, C]), z,
interp_mode='cartesian',
ncontours=16,
colorscale='Viridis',
showscale=True,
title='')
myticks = np.round(np.linspace(z.min(), z.max(), 6), 3)
fig.update_traces(marker_colorbar_tickvals=myticks,
marker_colorbar_ticktext=myticks, selector=dict(mode="markers"))

fig.update_traces(marker_cmin=-1, marker_cmax=1.8, selector=dict(mode="markers"))

The ticklabels are correctly assigned, but not the colors. The special construction of this kind of plot fails somewhere. Notice that after the second update, the colors are the same across the fig, and only the tick position is changed. I have no suggestion on how to remove this bug :(
Hi - we are trying to tidy up the stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for a while, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. Alternatively, if it's a request for tech support, please post in our community forum. Thank you - @gvwilson