Skip to content

add text_auto to px #3518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 20, 2021
Merged
4 changes: 4 additions & 0 deletions packages/python/plotly/plotly/express/_chart_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def density_contour(
histnorm=None,
nbinsx=None,
nbinsy=None,
text_auto=False,
title=None,
template=None,
width=None,
Expand Down Expand Up @@ -172,6 +173,7 @@ def density_heatmap(
histnorm=None,
nbinsx=None,
nbinsy=None,
text_auto=False,
title=None,
template=None,
width=None,
Expand Down Expand Up @@ -353,6 +355,7 @@ def bar(
log_y=False,
range_x=None,
range_y=None,
text_auto=False,
title=None,
template=None,
width=None,
Expand Down Expand Up @@ -454,6 +457,7 @@ def histogram(
histfunc=None,
cumulative=None,
nbins=None,
text_auto=False,
title=None,
template=None,
width=None,
Expand Down
10 changes: 10 additions & 0 deletions packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,16 @@ def infer_config(args, constructor, trace_patch, layout_patch):
):
args["histfunc"] = trace_patch["histfunc"] = "sum"

if args.get("text_auto", False) is not False:
if constructor in [go.Histogram2d, go.Histogram2dContour]:
letter = "z"
else:
letter = "y" if args["orientation"] == "v" else "x"
if args["text_auto"] is True:
trace_patch["texttemplate"] = "%{" + letter + "}"
else:
trace_patch["texttemplate"] = "%{" + letter + ":" + args["text_auto"] + "}"

if constructor in [go.Histogram2d, go.Densitymapbox]:
show_colorbar = True
trace_patch["coloraxis"] = "coloraxis1"
Expand Down
5 changes: 5 additions & 0 deletions packages/python/plotly/plotly/express/_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,11 @@
"If `'complementary'`, the CCDF is plotted such that values represent data above the point.",
"If `'reversed'`, a variant of the CCDF is plotted such that values represent data at or above the point.",
],
text_auto=[
"bool or string (default `False`)",
"If `True` or a string, the x or y or z values will be displayed as text, depending on the orientation",
"A string like `'.2f'`, it will be interpreted as a `texttemplate` numeric formatting directive.",
],
)


Expand Down
20 changes: 19 additions & 1 deletion packages/python/plotly/plotly/express/_imshow.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def imshow(
binary_backend="auto",
binary_compression_level=4,
binary_format="png",
text_auto=False,
):
"""
Display an image, i.e. data on a 2D regular raster.
Expand Down Expand Up @@ -208,6 +209,10 @@ def imshow(
since it uses lossless compression, but 'jpg' (lossy) compression can
result if smaller binary strings for natural images.

text_auto: bool or str (default `False`)
If `True` or a string, single-channel `img` values will be displayed as text",
A string like `'.2f'`, it will be interpreted as a `texttemplate` numeric formatting directive.

Returns
-------
fig : graph_objects.Figure containing the displayed image
Expand Down Expand Up @@ -385,8 +390,21 @@ def imshow(
"The length of the x vector must match the length of the second "
+ "dimension of the img matrix."
)

if text_auto is True:
texttemplate = "%{z}"
elif text_auto is not False:
texttemplate = "%{z:" + text_auto + "}"

traces = [
go.Heatmap(x=x, y=y, z=img[index_tup], coloraxis="coloraxis1", name=str(i))
go.Heatmap(
x=x,
y=y,
z=img[index_tup],
coloraxis="coloraxis1",
name=str(i),
texttemplate=texttemplate,
)
for i, index_tup in enumerate(itertools.product(*iterables))
]
autorange = True if origin == "lower" else "reversed"
Expand Down