Skip to content

Commit a669408

Browse files
committed
naming edits
- changed `text` to `annotation_text` and `table_text` so the `text` argument can still be used for hover - cleaned up arg names and order
1 parent 84ade7c commit a669408

File tree

1 file changed

+63
-54
lines changed

1 file changed

+63
-54
lines changed

plotly/tools.py

Lines changed: 63 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,7 +1578,7 @@ def _validate_streamline(x, y):
15781578
"evenly spaced array")
15791579

15801580
@staticmethod
1581-
def _validate_annotated_heatmap(z, text):
1581+
def _validate_annotated_heatmap(z, annotation_text):
15821582
"""
15831583
Annotated heatmap specific validations
15841584
@@ -1590,10 +1590,10 @@ def _validate_annotated_heatmap(z, text):
15901590
:raises: (PlotlyError) If z and text matrices are not the same
15911591
dimmensions.
15921592
"""
1593-
if text:
1594-
FigureFactory._validate_equal_length(z, text)
1593+
if annotation_text:
1594+
FigureFactory._validate_equal_length(z, annotation_text)
15951595
for lst in range(len(z)):
1596-
if len(z[lst]) != len(text[lst]):
1596+
if len(z[lst]) != len(annotation_text[lst]):
15971597
raise exceptions.PlotlyError("z and text should have the "
15981598
"same dimmensions")
15991599

@@ -2562,8 +2562,10 @@ def create_dendrogram(X, orientation="bottom", labels=None,
25622562
'data': dendrogram.data}
25632563

25642564
@staticmethod
2565-
def create_annotated_heatmap(z, x=None, y=None, text=[], hover_text=[],
2566-
fontcolor=[], showscale=False, **kwargs):
2565+
def create_annotated_heatmap(z, x=None, y=None, annotation_text=None,
2566+
colorscale=None, fontcolor=None,
2567+
showscale=False, reversescale=False,
2568+
**kwargs):
25672569
"""
25682570
BETA function that creates annotated heatmaps
25692571
@@ -2602,16 +2604,22 @@ def create_annotated_heatmap(z, x=None, y=None, text=[], hover_text=[],
26022604
"""
26032605
# TODO: protected until #282
26042606
from plotly.graph_objs import graph_objs
2605-
FigureFactory._validate_annotated_heatmap(z, text)
2606-
annotations = _AnnotatedHeatmap(z, x, y, text, fontcolor,
2607+
2608+
# Avoiding mutables in the call signature
2609+
annotation_text = \
2610+
annotation_text if annotation_text is not None else []
2611+
fontcolor = fontcolor if fontcolor is not None else []
2612+
FigureFactory._validate_annotated_heatmap(z, annotation_text)
2613+
annotations = _AnnotatedHeatmap(z, x, y, annotation_text,
2614+
colorscale, fontcolor, reversescale,
26072615
**kwargs).make_annotations()
26082616

26092617
if x or y:
26102618
trace = dict(type='heatmap',
26112619
z=z,
26122620
x=x,
26132621
y=y,
2614-
text=hover_text,
2622+
colorscale=colorscale,
26152623
showscale=showscale,
26162624
**kwargs)
26172625
layout = dict(annotations=annotations,
@@ -2621,8 +2629,7 @@ def create_annotated_heatmap(z, x=None, y=None, text=[], hover_text=[],
26212629
else:
26222630
trace = dict(type='heatmap',
26232631
z=z,
2624-
text=hover_text,
2625-
hoverinfo='text',
2632+
colorscale=colorscale,
26262633
showscale=showscale,
26272634
**kwargs)
26282635
layout = dict(annotations=annotations,
@@ -2637,8 +2644,7 @@ def create_annotated_heatmap(z, x=None, y=None, text=[], hover_text=[],
26372644
return graph_objs.Figure(data=data, layout=layout)
26382645

26392646
@staticmethod
2640-
def create_table(text, colorscale=[[0, '#66b2ff'], [.5, '#e6e6e6'],
2641-
[1, '#ffffff']], fontcolor=['#000000'],
2647+
def create_table(table_text, colorscale=None, fontcolor=['#000000'],
26422648
height_constant=30, index=False, index_title='',
26432649
hoverinfo='none', **kwargs):
26442650
"""
@@ -2698,17 +2704,23 @@ def create_table(text, colorscale=[[0, '#66b2ff'], [.5, '#e6e6e6'],
26982704
"""
26992705
# TODO: protected until #282
27002706
from plotly.graph_objs import graph_objs
2701-
FigureFactory._validate_table(text, fontcolor)
2702-
table_matrix = _Table(text, colorscale, fontcolor, index, index_title,
2703-
**kwargs).get_table_matrix()
2704-
annotations = _Table(text, colorscale, fontcolor, index, index_title,
2705-
**kwargs).make_table_annotations()
2707+
2708+
colorscale = \
2709+
colorscale if colorscale is not None else [[0, '#66b2ff'],
2710+
[.5, '#e6e6e6'],
2711+
[1, '#ffffff']]
2712+
FigureFactory._validate_table(table_text, fontcolor)
2713+
table_matrix = _Table(table_text, colorscale, fontcolor, index,
2714+
index_title, **kwargs).get_table_matrix()
2715+
annotations = _Table(table_text, colorscale, fontcolor, index,
2716+
index_title, **kwargs).make_table_annotations()
27062717

27072718
trace = dict(type='heatmap',
27082719
z=table_matrix,
27092720
opacity=.7,
27102721
colorscale=colorscale,
27112722
showscale=False,
2723+
hoverinfo=hoverinfo,
27122724
**kwargs)
27132725

27142726
data = [trace]
@@ -3669,10 +3681,10 @@ class _AnnotatedHeatmap(FigureFactory):
36693681
"""
36703682
Refer to TraceFactory.create_annotated_heatmap() for docstring
36713683
"""
3672-
def __init__(self, z, x=[], y=[], text=[],
3673-
fontcolor=[], colorscale=[],
3674-
reversescale=False, **kwargs):
3684+
def __init__(self, z, x, y, annotation_text, colorscale,
3685+
fontcolor, reversescale, **kwargs):
36753686
from plotly.graph_objs import graph_objs
3687+
36763688
self.z = z
36773689
if x:
36783690
self.x = x
@@ -3682,14 +3694,11 @@ def __init__(self, z, x=[], y=[], text=[],
36823694
self.y = y
36833695
else:
36843696
self.y = range(len(z))
3685-
if text:
3686-
self.text = text
3687-
else:
3688-
self.text = self.z
3689-
if colorscale:
3690-
self.colorscale = colorscale
3697+
if annotation_text:
3698+
self.annotation_text = annotation_text
36913699
else:
3692-
self.colorscale = 'RdBu'
3700+
self.annotation_text = self.z
3701+
self.colorscale = colorscale if colorscale is not None else 'Reds'
36933702
self.reversescale = reversescale
36943703
self.fontcolor = fontcolor
36953704

@@ -3768,7 +3777,7 @@ def make_annotations(self):
37683777
for m, val in enumerate(row):
37693778
annotations.append(
37703779
graph_objs.Annotation(
3771-
text=str(self.text[n][m]),
3780+
text=str(self.annotation_text[n][m]),
37723781
x=self.x[m],
37733782
y=self.y[n],
37743783
xref='x1',
@@ -3784,24 +3793,24 @@ class _Table(FigureFactory):
37843793
"""
37853794
Refer to TraceFactory.create_table() for docstring
37863795
"""
3787-
def __init__(self, text, colorscale, fontcolor, index, index_title='',
3788-
**kwargs):
3796+
def __init__(self, table_text, colorscale, fontcolor, index,
3797+
index_title='', **kwargs):
37893798
from plotly.graph_objs import graph_objs
3790-
if _pandas_imported and isinstance(text, pd.DataFrame):
3791-
headers = text.columns.tolist()
3792-
text_index = text.index.tolist()
3793-
text = text.values.tolist()
3794-
text.insert(0, headers)
3799+
if _pandas_imported and isinstance(table_text, pd.DataFrame):
3800+
headers = table_text.columns.tolist()
3801+
table_text_index = table_text.index.tolist()
3802+
table_text = table_text.values.tolist()
3803+
table_text.insert(0, headers)
37953804
if index:
3796-
text_index.insert(0, index_title)
3797-
for i in range(len(text)):
3798-
text[i].insert(0, text_index[i])
3799-
self.text = text
3805+
table_text_index.insert(0, index_title)
3806+
for i in range(len(table_text)):
3807+
table_text[i].insert(0, table_text_index[i])
3808+
self.table_text = table_text
38003809
self.colorscale = colorscale
38013810
self.fontcolor = fontcolor
38023811
self.index = index
3803-
self.x = range(len(text[0]))
3804-
self.y = range(len(text))
3812+
self.x = range(len(table_text[0]))
3813+
self.y = range(len(table_text))
38053814

38063815
def get_table_matrix(self):
38073816
'''
@@ -3810,14 +3819,14 @@ def get_table_matrix(self):
38103819
:rtype (list[list]) table_matrix: z matrix to make heatmap with striped
38113820
table coloring.
38123821
'''
3813-
header = [0] * len(self.text[0])
3814-
odd_row = [.5] * len(self.text[0])
3815-
even_row = [1] * len(self.text[0])
3816-
table_matrix = [None] * len(self.text)
3822+
header = [0] * len(self.table_text[0])
3823+
odd_row = [.5] * len(self.table_text[0])
3824+
even_row = [1] * len(self.table_text[0])
3825+
table_matrix = [None] * len(self.table_text)
38173826
table_matrix[0] = header
3818-
for i in range(1, len(self.text), 2):
3827+
for i in range(1, len(self.table_text), 2):
38193828
table_matrix[i] = odd_row
3820-
for i in range(2, len(self.text), 2):
3829+
for i in range(2, len(self.table_text), 2):
38213830
table_matrix[i] = even_row
38223831
if self.index:
38233832
for array in table_matrix:
@@ -3835,15 +3844,15 @@ def get_table_fontcolor(self):
38353844
:rtype (list) all_font_color: list of fontcolors for each row in table.
38363845
'''
38373846
if len(self.fontcolor) == 1:
3838-
all_font_color = self.fontcolor*len(self.text)
3847+
all_font_color = self.fontcolor*len(self.table_text)
38393848
elif len(self.fontcolor) == 3:
3840-
all_font_color = range(len(self.text))
3849+
all_font_color = range(len(self.table_text))
38413850
all_font_color[0] = self.fontcolor[0]
3842-
for i in range(1, len(self.text), 2):
3851+
for i in range(1, len(self.table_text), 2):
38433852
all_font_color[i] = self.fontcolor[1]
3844-
for i in range(2, len(self.text), 2):
3853+
for i in range(2, len(self.table_text), 2):
38453854
all_font_color[i] = self.fontcolor[2]
3846-
elif len(self.fontcolor) == len(self.text):
3855+
elif len(self.fontcolor) == len(self.table_text):
38473856
all_font_color = self.fontcolor
38483857
return all_font_color
38493858

@@ -3858,7 +3867,7 @@ def make_table_annotations(self):
38583867
table_matrix = _Table.get_table_matrix(self)
38593868
all_font_color = _Table.get_table_fontcolor(self)
38603869
annotations = []
3861-
for n, row in enumerate(self.text):
3870+
for n, row in enumerate(self.table_text):
38623871
for m, val in enumerate(row):
38633872
annotations.append(
38643873
graph_objs.Annotation(

0 commit comments

Comments
 (0)