Skip to content

Commit 449457a

Browse files
Merge branch 'nocolor' into issue-2618
2 parents bbe54bd + f908ae3 commit 449457a

14 files changed

+132
-20
lines changed

Diff for: CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
56
## [4.9.0] - unreleased
67

78
### Added
@@ -14,6 +15,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1415

1516
- trendline traces are now of type `scattergl` when `render_mode="webgl"` in Plotly Express ([#2614](https://github.com/plotly/plotly.py/pull/2614))
1617

18+
### Updated
19+
20+
- Added all cartesian-2d Plotly Express functions, plus `imshow` to Pandas backend with `kind` option
21+
- `plotly.express.imshow` now uses data frame index and columns names and values to populate axis parameters by default ([#2539](https://github.com/plotly/plotly.py/pull/2539))
22+
23+
1724

1825
## [4.8.2] - 2020-06-26
1926

@@ -22,6 +29,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2229
- Updated Plotly.js to version 1.54.5. See the [plotly.js CHANGELOG](https://github.com/plotly/plotly.js/blob/v1.54.5/CHANGELOG.md) for more information.
2330
- `add_traces()` now accepts bare `int`-like values for `rows`/`cols` as well as lists thereof ([#2546](https://github.com/plotly/plotly.py/pull/2546)), with thanks to [@MCBoarder289](https://github.com/MCBoarder289) for the contribution!
2431

32+
2533
### Fixed
2634

2735
- `row`/`col` now accept `int`-like values, not strictly `int` values ([#2451](https://github.com/plotly/plotly.py/pull/2451)), with thanks to [@MCBoarder289](https://github.com/MCBoarder289) for the contribution!

Diff for: doc/python/dropdowns.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ fig.update_scenes(
147147
aspectmode="manual"
148148
)
149149

150-
# Add drowdowns
150+
# Add dropdowns
151151
button_layer_1_height = 1.08
152152
fig.update_layout(
153153
updatemenus=[

Diff for: doc/python/facet-plots.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.2'
9-
jupytext_version: 1.3.4
9+
jupytext_version: 1.4.2
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.7.0
23+
version: 3.7.7
2424
plotly:
2525
description: How to make Facet and Trellis Plots in Python with Plotly.
2626
display_as: statistical
@@ -103,7 +103,7 @@ fig.show()
103103

104104
### Customize Subplot Figure Titles
105105

106-
Since subplot figure titles are [annotations](https://plotly.com/python/text-and-annotations/#simple-annotation), you can use the `for_each_annotation` function to customize them.
106+
Since subplot figure titles are [annotations](https://plotly.com/python/text-and-annotations/#simple-annotation), you can use the `for_each_annotation` function to customize them, for example to remove the equal-sign (`=`).
107107

108108
In the following example, we pass a lambda function to `for_each_annotation` in order to change the figure subplot titles from `smoker=No` and `smoker=Yes` to just `No` and `Yes`.
109109

@@ -115,8 +115,25 @@ fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
115115
fig.show()
116116
```
117117

118+
### Controlling Facet Spacing
119+
120+
The `facet_row_spacing` and `facet_col_spacing` arguments can be used to control the spacing between rows and columns. These values are specified in fractions of the plotting area in paper coordinates and not in pixels, so they will grow or shrink with the `width` and `height` of the figure.
121+
122+
The defaults work well with 1-4 rows or columns at the default figure size with the default font size, but need to be reduced to around 0.01 for very large figures or figures with many rows or columns. Conversely, if activating tick labels on all facets, the spacing will need to be increased.
123+
118124
```python
125+
import plotly.express as px
119126

127+
df = px.data.gapminder().query("continent == 'Africa'")
128+
129+
fig = px.line(df, x="year", y="lifeExp", facet_col="country", facet_col_wrap=7,
130+
facet_row_spacing=0.04, # default is 0.07 when facet_col_wrap is used
131+
facet_col_spacing=0.04, # default is 0.03
132+
height=600, width=800,
133+
title="Life Expectancy in Africa")
134+
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
135+
fig.update_yaxes(showticklabels=True)
136+
fig.show()
120137
```
121138

122139
### Synchronizing axes in subplots with `matches`
@@ -138,4 +155,4 @@ for i in range(1, 4):
138155
fig.add_trace(go.Scatter(x=x, y=np.random.random(N)), 1, i)
139156
fig.update_xaxes(matches='x')
140157
fig.show()
141-
```
158+
```

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/hover-text-and-formatting.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ df_2007 = px.data.gapminder().query("year==2007")
228228
fig = px.scatter(df_2007, x="gdpPercap", y="lifeExp", log_x=True, color='continent'
229229
)
230230
print("plotly express hovertemplate:", fig.data[0].hovertemplate)
231-
fig.update_traces(hovertemplate='GDP: %{x} <br>Life Expectany: %{y}') #
231+
fig.update_traces(hovertemplate='GDP: %{x} <br>Life Expectancy: %{y}') #
232232
fig.update_traces(hovertemplate=None, selector={'name':'Europe'}) # revert to default hover
233233
print("user_defined hovertemplate:", fig.data[0].hovertemplate)
234234
fig.show()
@@ -404,4 +404,4 @@ fig.show()
404404

405405
#### Reference
406406

407-
See https://plotly.com/python/reference/ for more information and chart attribute options!
407+
See https://plotly.com/python/reference/ for more information and chart attribute options!

Diff for: doc/python/legend.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,10 @@ fig.add_trace(go.Streamtube(
428428
))
429429
# Update all traces together
430430
fig.update_traces(showlegend=True, showscale=False)
431-
fig.update_layout(width=600, title_text='Exporation of a vector field using several traces')
431+
fig.update_layout(width=600, title_text='Exploration of a vector field using several traces')
432432
fig.show()
433433
```
434434

435435
#### Reference
436436

437-
See https://plotly.com/python/reference/#layout-legend for more information!
437+
See https://plotly.com/python/reference/#layout-legend for more information!

Diff for: doc/python/pandas-backend.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fig.show()
9898

9999
### Supported Methods
100100

101-
The Plotly backend supports the following `kind`s of Pandas plots: `scatter`, `line`, `area`, `bar`, `barh`, `hist` and `box`, via the call pattern `df.plot(kind='scatter')` or `df.plot.scatter()`. These delegate to the corresponding Plotly Express functions.
101+
The Plotly backend supports the following `kind`s of Pandas plots: `scatter`, `line`, `area`, `bar`, `barh`, `hist` and `box`, via the call pattern `df.plot(kind='scatter')` or `df.plot.scatter()`. These delegate to the corresponding Plotly Express functions. In addition, the following are valid options to the `kind` argument of `df.plot()`: `violin`, `strip`, `funnel`, `density_heatmap`, `density_contour` and `imshow`, even though the call pattern `df.plot.violin()` is not supported for these kinds of charts, per the Pandas API.
102102

103103
```python
104104
import pandas as pd
@@ -198,4 +198,4 @@ fig.show()
198198

199199
### What about Cufflinks?
200200

201-
There also exists an independent third-party wrapper library around Plotly called [Cufflinks](https://github.com/santosjorge/cufflinks), which provides similar functionality (with an API closer to that of Pandas' default `matplotlib` backend) by adding a `.iplot()` method to Pandas dataframes, as it was developed before Pandas supported configurable backends. Issues and questions regarding Cufflinks should be [raised in the Cufflinks repository](https://github.com/santosjorge/cufflinks/issues/new).
201+
There also exists an independent third-party wrapper library around Plotly called [Cufflinks](https://github.com/santosjorge/cufflinks), which provides similar functionality (with an API closer to that of Pandas' default `matplotlib` backend) by adding a `.iplot()` method to Pandas dataframes, as it was developed before Pandas supported configurable backends. Issues and questions regarding Cufflinks should be [raised in the Cufflinks repository](https://github.com/santosjorge/cufflinks/issues/new).

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

+1-1
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

Diff for: packages/python/plotly/plotly/__init__.py

+33-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,20 @@ def plot(data_frame, kind, **kwargs):
8080
To activate, set pandas.options.plotting.backend="plotly"
8181
See https://github.com/pandas-dev/pandas/blob/master/pandas/plotting/__init__.py
8282
"""
83-
from .express import scatter, line, area, bar, box, histogram
83+
from .express import (
84+
scatter,
85+
line,
86+
area,
87+
bar,
88+
box,
89+
histogram,
90+
violin,
91+
strip,
92+
funnel,
93+
density_contour,
94+
density_heatmap,
95+
imshow,
96+
)
8497

8598
if kind == "scatter":
8699
new_kwargs = {k: kwargs[k] for k in kwargs if k not in ["s", "c"]}
@@ -96,9 +109,27 @@ def plot(data_frame, kind, **kwargs):
96109
if kind == "box":
97110
new_kwargs = {k: kwargs[k] for k in kwargs if k not in ["by"]}
98111
return box(data_frame, **new_kwargs)
99-
if kind in "hist":
112+
if kind in ["hist", "histogram"]:
100113
new_kwargs = {k: kwargs[k] for k in kwargs if k not in ["by", "bins"]}
101114
return histogram(data_frame, **new_kwargs)
115+
if kind == "violin":
116+
return violin(data_frame, **kwargs)
117+
if kind == "strip":
118+
return strip(data_frame, **kwargs)
119+
if kind == "funnel":
120+
return funnel(data_frame, **kwargs)
121+
if kind == "density_contour":
122+
return density_contour(data_frame, **kwargs)
123+
if kind == "density_heatmap":
124+
return density_heatmap(data_frame, **kwargs)
125+
if kind == "imshow":
126+
return imshow(data_frame, **kwargs)
127+
if kind == "heatmap":
128+
raise ValueError(
129+
"kind='heatmap' not supported plotting.backend='plotly'. "
130+
"Please use kind='imshow' or kind='density_heatmap'."
131+
)
132+
102133
raise NotImplementedError(
103134
"kind='%s' not yet supported for plotting.backend='plotly'" % kind
104135
)

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,12 @@
231231
],
232232
facet_row_spacing=[
233233
"float between 0 and 1",
234-
"Spacing between facet rows, in paper units.",
234+
"Spacing between facet rows, in paper units. Default is 0.03 or 0.0.7 when facet_col_wrap is used.",
235+
],
236+
facet_col_spacing=[
237+
"float between 0 and 1",
238+
"Spacing between facet columns, in paper units Default is 0.02.",
235239
],
236-
facet_col_spacing=["int", "Spacing between facet columns, in paper units",],
237240
animation_frame=[
238241
colref_type,
239242
colref_desc,

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"

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

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@
2222
(lambda df: df.boxplot(), px.box),
2323
(lambda df: df.hist(), px.histogram),
2424
(lambda df: df["A"].hist(), lambda df: px.histogram(df["A"])),
25+
(lambda df: df.plot(kind="line"), px.line),
26+
(lambda df: df.plot(kind="area"), px.area),
27+
(lambda df: df.plot(kind="bar"), px.bar),
28+
(lambda df: df.plot(kind="box"), px.box),
29+
(lambda df: df.plot(kind="hist"), px.histogram),
30+
(lambda df: df.plot(kind="histogram"), px.histogram),
31+
(lambda df: df.plot(kind="violin"), px.violin),
32+
(lambda df: df.plot(kind="strip"), px.strip),
33+
(lambda df: df.plot(kind="funnel"), px.funnel),
34+
(lambda df: df.plot(kind="density_contour"), px.density_contour),
35+
(lambda df: df.plot(kind="density_heatmap"), px.density_heatmap),
36+
(lambda df: df.plot(kind="imshow"), px.imshow),
2537
],
2638
)
2739
def test_pandas_equiv(pandas_fn, px_fn):

0 commit comments

Comments
 (0)