Skip to content

Commit f854261

Browse files
add text_auto to px
1 parent 9e281a0 commit f854261

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

packages/python/plotly/plotly/express/_chart_types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def density_contour(
103103
histnorm=None,
104104
nbinsx=None,
105105
nbinsy=None,
106+
text_auto=False,
106107
title=None,
107108
template=None,
108109
width=None,
@@ -172,6 +173,7 @@ def density_heatmap(
172173
histnorm=None,
173174
nbinsx=None,
174175
nbinsy=None,
176+
text_auto=False,
175177
title=None,
176178
template=None,
177179
width=None,
@@ -353,6 +355,7 @@ def bar(
353355
log_y=False,
354356
range_x=None,
355357
range_y=None,
358+
text_auto=False,
356359
title=None,
357360
template=None,
358361
width=None,
@@ -454,6 +457,7 @@ def histogram(
454457
histfunc=None,
455458
cumulative=None,
456459
nbins=None,
460+
text_auto=False,
457461
title=None,
458462
template=None,
459463
width=None,

packages/python/plotly/plotly/express/_core.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,16 @@ def infer_config(args, constructor, trace_patch, layout_patch):
17911791
):
17921792
args["histfunc"] = trace_patch["histfunc"] = "sum"
17931793

1794+
if args.get("text_auto", False) is not False:
1795+
if constructor in [go.Histogram2d, go.Histogram2dContour]:
1796+
letter = "z"
1797+
else:
1798+
letter = "y" if args["orientation"] == "v" else "x"
1799+
if args["text_auto"] is True:
1800+
trace_patch["texttemplate"] = "%{" + letter + "}"
1801+
else:
1802+
trace_patch["texttemplate"] = "%{" + letter + ":" + args["text_auto"] + "}"
1803+
17941804
if constructor in [go.Histogram2d, go.Densitymapbox]:
17951805
show_colorbar = True
17961806
trace_patch["coloraxis"] = "coloraxis1"

packages/python/plotly/plotly/express/_doc.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,11 @@
591591
"If `'complementary'`, the CCDF is plotted such that values represent data above the point.",
592592
"If `'reversed'`, a variant of the CCDF is plotted such that values represent data at or above the point.",
593593
],
594+
text_auto=[
595+
"bool or string (default `False`)",
596+
"If `True` or a string, the x or y or z values will be displayed as text, depending on the orientation",
597+
"A string like `'.2f'`, it will be interpreted as a `texttemplate` numeric formatting directive.",
598+
],
594599
)
595600

596601

packages/python/plotly/plotly/express/_imshow.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def imshow(
7979
binary_backend="auto",
8080
binary_compression_level=4,
8181
binary_format="png",
82+
text_auto=False,
8283
):
8384
"""
8485
Display an image, i.e. data on a 2D regular raster.
@@ -208,6 +209,10 @@ def imshow(
208209
since it uses lossless compression, but 'jpg' (lossy) compression can
209210
result if smaller binary strings for natural images.
210211
212+
text_auto: bool or str (default `False`)
213+
If `True` or a string, single-channel `img` values will be displayed as text",
214+
A string like `'.2f'`, it will be interpreted as a `texttemplate` numeric formatting directive.
215+
211216
Returns
212217
-------
213218
fig : graph_objects.Figure containing the displayed image
@@ -385,8 +390,21 @@ def imshow(
385390
"The length of the x vector must match the length of the second "
386391
+ "dimension of the img matrix."
387392
)
393+
394+
if text_auto is True:
395+
texttemplate = "%{z}"
396+
elif text_auto is not False:
397+
texttemplate = "%{z:" + text_auto + "}"
398+
388399
traces = [
389-
go.Heatmap(x=x, y=y, z=img[index_tup], coloraxis="coloraxis1", name=str(i))
400+
go.Heatmap(
401+
x=x,
402+
y=y,
403+
z=img[index_tup],
404+
coloraxis="coloraxis1",
405+
name=str(i),
406+
texttemplate=texttemplate,
407+
)
390408
for i, index_tup in enumerate(itertools.product(*iterables))
391409
]
392410
autorange = True if origin == "lower" else "reversed"

0 commit comments

Comments
 (0)