Skip to content

Commit b3dbceb

Browse files
emmanuellejonmmease
authored andcommittedMar 4, 2019
Updated method for ternary plot (#1418)
New implementation of the ternary contour figure factory that renders contours using filled line traces on top of the the proper ternary contour axes.
1 parent 9421c14 commit b3dbceb

File tree

4 files changed

+542
-419
lines changed

4 files changed

+542
-419
lines changed
 

‎.circleci/create_conda_optional_env.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if [ ! -d $HOME/miniconda/envs/circle_optional ]; then
1616
# Create environment
1717
# PYTHON_VERSION=3.6
1818
$HOME/miniconda/bin/conda create -n circle_optional --yes python=$PYTHON_VERSION \
19-
requests six pytz retrying psutil pandas decorator pytest mock nose poppler xarray
19+
requests six pytz retrying psutil pandas decorator pytest mock nose poppler xarray scikit-image
2020

2121
# Install orca into environment
2222
$HOME/miniconda/bin/conda install --yes -n circle_optional -c plotly plotly-orca

‎plotly/figure_factory/_ternary_contour.py

+492-396
Large diffs are not rendered by default.

‎plotly/tests/test_optional/test_figure_factory/test_figure_factory.py

+48-22
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
shapely = optional_imports.get_module('shapely')
1414
shapefile = optional_imports.get_module('shapefile')
1515
gp = optional_imports.get_module('geopandas')
16+
sk_measure = optional_imports.get_module('skimage.measure')
1617

1718

1819
class TestDistplot(NumpyTestUtilsMixin, TestCase):
@@ -2908,15 +2909,15 @@ def test_wrong_coordinates(self):
29082909
with self.assertRaises(ValueError,
29092910
msg='Barycentric coordinates should be positive.'):
29102911
_ = ff.create_ternary_contour(np.stack((a, b)), z)
2911-
mask = a + b < 1.
2912+
mask = a + b <= 1.
29122913
a = a[mask]
29132914
b = b[mask]
29142915
with self.assertRaises(ValueError):
29152916
_ = ff.create_ternary_contour(np.stack((a, b, a, b)), z)
29162917
with self.assertRaises(ValueError,
29172918
msg='different number of values and points'):
29182919
_ = ff.create_ternary_contour(np.stack((a, b, 1 - a - b)),
2919-
np.concatenate((z, [1])))
2920+
np.concatenate((z, [1])))
29202921
# Different sums for different points
29212922
c = a
29222923
with self.assertRaises(ValueError):
@@ -2927,46 +2928,71 @@ def test_wrong_coordinates(self):
29272928
_ = ff.create_ternary_contour(np.stack((a, b, 2 - a - b)), z)
29282929

29292930

2930-
def test_tooltip(self):
2931+
def test_simple_ternary_contour(self):
29312932
a, b = np.mgrid[0:1:20j, 0:1:20j]
29322933
mask = a + b < 1.
29332934
a = a[mask].ravel()
29342935
b = b[mask].ravel()
29352936
c = 1 - a - b
29362937
z = a * b * c
2937-
fig = ff.create_ternary_contour(np.stack((a, b, c)), z,
2938-
tooltip_mode='percents')
2939-
fig = ff.create_ternary_contour(np.stack((a, b, c)), z,
2940-
tooltip_mode='percent')
2941-
2942-
with self.assertRaises(ValueError):
2943-
fig = ff.create_ternary_contour(np.stack((a, b, c)), z,
2944-
tooltip_mode='wrong_mode')
2938+
fig = ff.create_ternary_contour(np.stack((a, b, c)), z)
2939+
fig2 = ff.create_ternary_contour(np.stack((a, b)), z)
2940+
np.testing.assert_array_almost_equal(fig2['data'][0]['a'],
2941+
fig['data'][0]['a'],
2942+
decimal=3)
29452943

29462944

2947-
def test_simple_ternary_contour(self):
2945+
def test_colorscale(self):
29482946
a, b = np.mgrid[0:1:20j, 0:1:20j]
29492947
mask = a + b < 1.
29502948
a = a[mask].ravel()
29512949
b = b[mask].ravel()
29522950
c = 1 - a - b
29532951
z = a * b * c
2954-
fig = ff.create_ternary_contour(np.stack((a, b, c)), z)
2955-
fig2 = ff.create_ternary_contour(np.stack((a, b)), z)
2956-
np.testing.assert_array_equal(fig2['data'][0]['z'],
2957-
fig['data'][0]['z'])
2952+
z /= z.max()
2953+
fig = ff.create_ternary_contour(np.stack((a, b, c)), z,
2954+
showscale=True)
2955+
fig2 = ff.create_ternary_contour(np.stack((a, b, c)), z,
2956+
showscale=True, showmarkers=True)
2957+
assert isinstance(fig.data[-1]['marker']['colorscale'], tuple)
2958+
assert isinstance(fig2.data[-1]['marker']['colorscale'], str)
2959+
assert fig.data[-1]['marker']['cmax'] == 1
2960+
assert fig2.data[-1]['marker']['cmax'] == 1
29582961

29592962

2960-
def test_contour_attributes(self):
2963+
def check_pole_labels(self):
29612964
a, b = np.mgrid[0:1:20j, 0:1:20j]
29622965
mask = a + b < 1.
29632966
a = a[mask].ravel()
29642967
b = b[mask].ravel()
29652968
c = 1 - a - b
29662969
z = a * b * c
2967-
contour_dict = {'ncontours': 10,
2968-
'showscale': True}
2970+
pole_labels = ['A', 'B', 'C']
2971+
fig = ff.create_ternary_contour(np.stack((a, b, c)), z,
2972+
pole_labels=pole_labels)
2973+
assert fig.layout.ternary.aaxis.title.text == pole_labels[0]
2974+
assert fig.data[-1].hovertemplate[0] == pole_labels[0]
2975+
2976+
2977+
def test_optional_arguments(self):
2978+
a, b = np.mgrid[0:1:20j, 0:1:20j]
2979+
mask = a + b <= 1.
2980+
a = a[mask].ravel()
2981+
b = b[mask].ravel()
2982+
c = 1 - a - b
2983+
z = a * b * c
2984+
ncontours = 7
2985+
args = [dict(showmarkers=False, showscale=False),
2986+
dict(showmarkers=True, showscale=False),
2987+
dict(showmarkers=False, showscale=True),
2988+
dict(showmarkers=True, showscale=True)]
2989+
2990+
for arg_set in args:
2991+
fig = ff.create_ternary_contour(np.stack((a, b, c)), z,
2992+
interp_mode='cartesian',
2993+
ncontours=ncontours,
2994+
**arg_set)
2995+
# This test does not work for ilr interpolation
2996+
print(len(fig.data))
2997+
assert (len(fig.data) == ncontours + 2 + arg_set['showscale'])
29692998

2970-
fig = ff.create_ternary_contour(np.stack((a, b, c)), z, **contour_dict)
2971-
for key, value in contour_dict.items():
2972-
assert fig['data'][0][key] == value

‎tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ deps=
7373
optional: pillow==5.2.0
7474
optional: matplotlib==2.2.3
7575
optional: xarray==0.10.9
76+
optional: scikit-image==0.13.1
7677

7778
; CORE ENVIRONMENTS
7879
[testenv:py27-core]

0 commit comments

Comments
 (0)
Please sign in to comment.