Skip to content

Commit beb7c8c

Browse files
imshow now handles DataFrame indexes and names by default
1 parent 1db86d0 commit beb7c8c

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

Diff for: doc/python/heatmaps.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jupyter:
3636

3737
### Heatmap with `plotly.express` and `px.imshow`
3838

39-
[Plotly Express](/python/plotly-express/) is the easy-to-use, high-level interface to Plotly, which [operates on a variety of types of data](/python/px-arguments/) and produces [easy-to-style figures](/python/styling-plotly-express/). With `px.imshow`, each value of the input array is represented as a heatmap pixel.
39+
[Plotly Express](/python/plotly-express/) is the easy-to-use, high-level interface to Plotly, which [operates on a variety of types of data](/python/px-arguments/) and produces [easy-to-style figures](/python/styling-plotly-express/). With `px.imshow`, each value of the input array or data frame is represented as a heatmap pixel.
4040

4141
For more examples using `px.imshow`, see the [tutorial on displaying image data with plotly](/python/imshow).
4242

@@ -49,6 +49,14 @@ fig = px.imshow([[1, 20, 30],
4949
fig.show()
5050
```
5151

52+
```python
53+
import plotly.express as px
54+
55+
df = px.data.medals_wide(indexed=True)
56+
fig = px.imshow(df)
57+
fig.show()
58+
```
59+
5260
### Customizing the axes and labels on a heatmap
5361

5462
You can use the `x`, `y` and `labels` arguments to customize the display of a heatmap, and use `.update_xaxes()` to move the x axis tick labels to the top:
@@ -182,4 +190,4 @@ Arrays of rasterized values build by datashader can be visualized using
182190
plotly's heatmaps, as shown in the [plotly and datashader tutorial](/python/datashader/).
183191

184192
#### Reference
185-
See https://plotly.com/python/reference/#heatmap for more information and chart attribute options!
193+
See https://plotly.com/python/reference/#heatmap for more information and chart attribute options!

Diff for: doc/python/px-arguments.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ There are three common conventions for storing column-oriented data, usually in
4949
* **wide-form data** has one row per value of one of the first variable, and one column per value of the second variable. This is suitable for storing and displaying 2-dimensional data.
5050
* **mixed-form data** is a hybrid of long-form and wide-form data, with one row per value of one variable, and some columns representing values of another, and some columns representing more variables. See the [wide-form documentation](/python/wide-form/) for examples of how to use Plotly Express to visualize this kind of data.
5151

52-
Every Plotly Express function other than `imshow` can operate on long-form data, and in addition, the following 2D-Cartesian functions can operate on wide-form and mixed-form data: `px.scatter`, `px.line`, `px.area`, `px.bar`, `px.histogram`, `px.violin`, `px.box`, `px.strip`, `px.funnel`, `px.density_heatmap` and `px.density_contour`.
52+
Every Plotly Express function can operate on long-form data (other than `px.imshow` which operates only on wide-form input), and in addition, the following 2D-Cartesian functions can operate on wide-form and mixed-form data: `px.scatter`, `px.line`, `px.area`, `px.bar`, `px.histogram`, `px.violin`, `px.box`, `px.strip`, `px.funnel`, `px.density_heatmap` and `px.density_contour`.
5353

5454
By way of example here is the same data, represented in long-form first, and then in wide-form:
5555

@@ -241,4 +241,4 @@ fig = px.bar(df, x='year', y=gdp, color='continent', labels={'y':'log gdp'},
241241
hover_data=['country'],
242242
title='Evolution of world GDP')
243243
fig.show()
244-
```
244+
```

Diff for: doc/python/wide-form.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ There are three common conventions for storing column-oriented data, usually in
4848
* **wide-form data** has one row per value of one of the first variable, and one column per value of the second variable. This is suitable for storing and displaying 2-dimensional data.
4949
* **mixed-form data** is a hybrid of long-form and wide-form data, with one row per value of one variable, and some columns representing values of another, and some columns representing more variables.
5050

51-
Every Plotly Express function other than `imshow` can operate on long-form data, and in addition, the following 2D-Cartesian functions can operate on wide-form and mixed-form data: `px.scatter`, `px.line`, `px.area`, `px.bar`, `px.histogram`, `px.violin`, `px.box`, `px.strip`, `px.funnel`, `px.density_heatmap` and `px.density_contour`.
51+
Every Plotly Express function can operate on long-form data (other than `px.imshow` which operates only on wide-form input), and in addition, the following 2D-Cartesian functions can operate on wide-form and mixed-form data: `px.scatter`, `px.line`, `px.area`, `px.bar`, `px.histogram`, `px.violin`, `px.box`, `px.strip`, `px.funnel`, `px.density_heatmap` and `px.density_contour`.
5252

5353
By way of example here is the same data, represented in long-form first, and then in wide-form:
5454

@@ -302,4 +302,4 @@ fig.show()
302302

303303
fig = px.box(wide_df, orientation="h")
304304
fig.show()
305-
```
305+
```

Diff for: packages/python/plotly/plotly/express/_imshow.py

+11
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,17 @@ def imshow(
196196
labels["color"] = xarray.plot.utils.label_from_attrs(img)
197197
labels["color"] = labels["color"].replace("\n", "<br>")
198198
else:
199+
if hasattr(img, "columns") and hasattr(img.columns, "__len__"):
200+
if x is None:
201+
x = img.columns
202+
if labels.get("x", None) is None and hasattr(img.columns, "name"):
203+
labels["x"] = img.columns.name or ""
204+
if hasattr(img, "index") and hasattr(img.index, "__len__"):
205+
if y is None:
206+
y = img.index
207+
if labels.get("y", None) is None and hasattr(img.index, "name"):
208+
labels["y"] = img.index.name or ""
209+
199210
if labels.get("x", None) is None:
200211
labels["x"] = ""
201212
if labels.get("y", None) is None:

Diff for: packages/python/plotly/plotly/tests/test_core/test_px/test_imshow.py

+22
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,25 @@ def test_imshow_labels_and_ranges():
150150

151151
with pytest.raises(ValueError):
152152
fig = px.imshow([[1, 2], [3, 4], [5, 6]], x=["a"])
153+
154+
155+
def test_imshow_dataframe():
156+
df = px.data.medals_wide(indexed=False)
157+
fig = px.imshow(df)
158+
assert fig.data[0].x[0] == df.columns[0]
159+
assert fig.data[0].x[0] == "nation"
160+
assert fig.layout.xaxis.title.text is None
161+
assert fig.data[0].y[0] == df.index[0]
162+
assert fig.data[0].y[0] == 0
163+
assert fig.layout.yaxis.title.text is None
164+
165+
df = px.data.medals_wide(indexed=True)
166+
fig = px.imshow(df)
167+
assert fig.data[0].x[0] == df.columns[0]
168+
assert fig.data[0].x[0] == "gold"
169+
assert fig.layout.xaxis.title.text == df.columns.name
170+
assert fig.layout.xaxis.title.text == "medal"
171+
assert fig.data[0].y[0] == df.index[0]
172+
assert fig.data[0].y[0] == "South Korea"
173+
assert fig.layout.yaxis.title.text == df.index.name
174+
assert fig.layout.yaxis.title.text == "nation"

0 commit comments

Comments
 (0)