Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 524e4fc

Browse files
committed
Merge pull request plotly#453 from plotly/Trisurf_Plots
First Push for Trisurf Plots
2 parents 748db23 + 4893444 commit 524e4fc

File tree

2 files changed

+604
-1
lines changed

2 files changed

+604
-1
lines changed

plotly/tests/test_optional/test_figure_factory.py

Lines changed: 178 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from nose.tools import raises
99

1010
import numpy as np
11+
from scipy.spatial import Delaunay
1112
import pandas as pd
1213

1314

@@ -532,6 +533,182 @@ def test_dendrogram_colorscale(self):
532533
self.assert_dict_equal(dendro['data'][2], expected_dendro['data'][2])
533534

534535

536+
class TestTrisurf(NumpyTestUtilsMixin, TestCase):
537+
538+
def test_vmin_and_vmax(self):
539+
540+
# check if vmin is greater than or equal to vmax
541+
u = np.linspace(0, 2, 2)
542+
v = np.linspace(0, 2, 2)
543+
u, v = np.meshgrid(u, v)
544+
u = u.flatten()
545+
v = v.flatten()
546+
547+
x = u
548+
y = v
549+
z = u*v
550+
551+
points2D = np.vstack([u, v]).T
552+
tri = Delaunay(points2D)
553+
simplices = tri.simplices
554+
555+
pattern = (
556+
"Incorrect relation between vmin and vmax. The vmin value cannot "
557+
"be bigger than or equal to the value of vmax."
558+
)
559+
560+
self.assertRaisesRegexp(PlotlyError, pattern,
561+
tls.FigureFactory.create_trisurf,
562+
x, y, z, simplices)
563+
564+
def test_valid_colormap(self):
565+
566+
# create data for trisurf plot
567+
u = np.linspace(-np.pi, np.pi, 3)
568+
v = np.linspace(-np.pi, np.pi, 3)
569+
u, v = np.meshgrid(u, v)
570+
u = u.flatten()
571+
v = v.flatten()
572+
573+
x = u
574+
y = u*np.cos(v)
575+
z = u*np.sin(v)
576+
577+
points2D = np.vstack([u, v]).T
578+
tri = Delaunay(points2D)
579+
simplices = tri.simplices
580+
581+
# check that a valid plotly colorscale name is entered
582+
self.assertRaises(PlotlyError, tls.FigureFactory.create_trisurf,
583+
x, y, z, simplices, colormap='foo')
584+
585+
# check that colormap is a list, if not a string
586+
587+
pattern1 = (
588+
"If 'colormap' is a list, then its items must be tripets of the "
589+
"form a,b,c or 'rgbx,y,z' where a,b,c are between 0 and 1 "
590+
"inclusive and x,y,z are between 0 and 255 inclusive."
591+
)
592+
593+
self.assertRaisesRegexp(PlotlyError, pattern1,
594+
tls.FigureFactory.create_trisurf,
595+
x, y, z, simplices, colormap=3)
596+
597+
# check: if colormap is a list of rgb color strings, make sure the
598+
# entries of each color are no greater than 255.0
599+
600+
pattern2 = (
601+
"Whoops! The elements in your rgb colormap tuples "
602+
"cannot exceed 255.0."
603+
)
604+
605+
self.assertRaisesRegexp(PlotlyError, pattern2,
606+
tls.FigureFactory.create_trisurf,
607+
x, y, z, simplices,
608+
colormap=['rgb(1, 2, 3)', 'rgb(4, 5, 600)'])
609+
610+
# check: if colormap is a list of tuple colors, make sure the entries
611+
# of each tuple are no greater than 1.0
612+
613+
pattern3 = (
614+
"Whoops! The elements in your rgb colormap tuples "
615+
"cannot exceed 1.0."
616+
)
617+
618+
self.assertRaisesRegexp(PlotlyError, pattern3,
619+
tls.FigureFactory.create_trisurf,
620+
x, y, z, simplices,
621+
colormap=[(0.2, 0.4, 0.6), (0.8, 1.0, 1.2)])
622+
623+
def test_trisurf_all_args(self):
624+
625+
# check if trisurf plot matches with expected output
626+
u = np.linspace(-1, 1, 3)
627+
v = np.linspace(-1, 1, 3)
628+
u, v = np.meshgrid(u, v)
629+
u = u.flatten()
630+
v = v.flatten()
631+
632+
x = u
633+
y = v
634+
z = u*v
635+
636+
points2D = np.vstack([u, v]).T
637+
tri = Delaunay(points2D)
638+
simplices = tri.simplices
639+
640+
test_trisurf_plot = tls.FigureFactory.create_trisurf(
641+
x, y, z, simplices
642+
)
643+
644+
exp_trisurf_plot = {
645+
'data': [
646+
{
647+
'facecolor': ['rgb(143.0, 123.0, 97.000000000000014)',
648+
'rgb(255.0, 127.0, 14.000000000000007)',
649+
'rgb(143.0, 123.0, 97.000000000000014)',
650+
'rgb(31.0, 119.0, 180.0)',
651+
'rgb(143.0, 123.0, 97.000000000000014)',
652+
'rgb(31.0, 119.0, 180.0)',
653+
'rgb(143.0, 123.0, 97.000000000000014)',
654+
'rgb(255.0, 127.0, 14.000000000000007)'],
655+
'i': [3, 1, 1, 5, 7, 3, 5, 7],
656+
'j': [1, 3, 5, 1, 3, 7, 7, 5],
657+
'k': [4, 0, 4, 2, 4, 6, 4, 8],
658+
'name': '',
659+
'type': 'mesh3d',
660+
'x': np.array([-1., 0., 1., -1., 0., 1., -1., 0., 1.]),
661+
'y': np.array([-1., -1., -1., 0., 0., 0., 1., 1., 1.]),
662+
'z': np.array([ 1., -0., -1., -0., 0., 0., -1., 0., 1.])
663+
},
664+
{
665+
'line': {'color': 'rgb(50, 50, 50)', 'width': 1.5},
666+
'mode': 'lines',
667+
'type': 'scatter3d',
668+
'x': [-1.0, 0.0, 0.0, -1.0, None, 0.0, -1.0, -1.0, 0.0, None,
669+
0.0, 1.0, 0.0, 0.0, None, 1.0, 0.0, 1.0, 1.0, None, 0.0,
670+
-1.0, 0.0, 0.0, None, -1.0, 0.0, -1.0, -1.0, None, 1.0,
671+
0.0, 0.0, 1.0, None, 0.0, 1.0, 1.0, 0.0, None],
672+
'y': [0.0, -1.0, 0.0, 0.0, None, -1.0, 0.0, -1.0, -1.0, None,
673+
-1.0, 0.0, 0.0, -1.0, None, 0.0, -1.0, -1.0, 0.0, None,
674+
1.0, 0.0, 0.0, 1.0, None, 0.0, 1.0, 1.0, 0.0, None, 0.0,
675+
1.0, 0.0, 0.0, None, 1.0, 0.0, 1.0, 1.0, None],
676+
'z': [-0.0, -0.0, 0.0, -0.0, None, -0.0, -0.0, 1.0, -0.0,
677+
None, -0.0, 0.0, 0.0, -0.0, None, 0.0, -0.0, -1.0, 0.0,
678+
None, 0.0, -0.0, 0.0, 0.0, None, -0.0, 0.0, -1.0, -0.0,
679+
None, 0.0, 0.0, 0.0, 0.0, None, 0.0, 0.0, 1.0, 0.0, None]
680+
}
681+
],
682+
'layout': {
683+
'height': 800,
684+
'scene': {'aspectratio': {'x': 1, 'y': 1, 'z': 1},
685+
'xaxis': {'backgroundcolor': 'rgb(230, 230, 230)',
686+
'gridcolor': 'rgb(255, 255, 255)',
687+
'showbackground': True,
688+
'zerolinecolor': 'rgb(255, 255, 255)'},
689+
'yaxis': {'backgroundcolor': 'rgb(230, 230, 230)',
690+
'gridcolor': 'rgb(255, 255, 255)',
691+
'showbackground': True,
692+
'zerolinecolor': 'rgb(255, 255, 255)'},
693+
'zaxis': {'backgroundcolor': 'rgb(230, 230, 230)',
694+
'gridcolor': 'rgb(255, 255, 255)',
695+
'showbackground': True,
696+
'zerolinecolor': 'rgb(255, 255, 255)'}},
697+
'title': 'Trisurf Plot',
698+
'width': 800
699+
}
700+
}
701+
702+
self.assert_dict_equal(test_trisurf_plot['layout'],
703+
exp_trisurf_plot['layout'])
704+
705+
self.assert_dict_equal(test_trisurf_plot['data'][0],
706+
exp_trisurf_plot['data'][0])
707+
708+
self.assert_dict_equal(test_trisurf_plot['data'][1],
709+
exp_trisurf_plot['data'][1])
710+
711+
535712
class TestScatterPlotMatrix(NumpyTestUtilsMixin, TestCase):
536713

537714
def test_dataframe_input(self):
@@ -703,7 +880,7 @@ def test_scatter_plot_matrix(self):
703880
columns=['Numbers', 'Fruit'])
704881

705882
test_scatter_plot_matrix = tls.FigureFactory.create_scatterplotmatrix(
706-
df, diag='scatter', height=1000, width=1000, size=13,
883+
df=df, diag='scatter', height=1000, width=1000, size=13,
707884
title='Scatterplot Matrix', use_theme=False
708885
)
709886

0 commit comments

Comments
 (0)