Skip to content

Commit b8da283

Browse files
Merge pull request #2502 from plotly/bar_fix
Bar fix
2 parents 6c5cfe2 + 940c679 commit b8da283

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

doc/python/pandas-backend.md

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

3737
### Introduction
3838

39-
The popular [Pandas](https://pandas.pydata.org/) data analysis and manipulation tool provides plotting functions on its `DataFrame` and `Series` objects, which have historically produced `matplotlib` plots. Since version 0.25, Pandas has provided a mechanism to use different backends, and as of version 4.8 of `plotly`, you can now use a [Plotly Express-powered](/python/plotly-express/) backend for Pandas plotting.
39+
The popular [Pandas](https://pandas.pydata.org/) data analysis and manipulation tool provides [plotting functions on its `DataFrame` and `Series` objects](https://pandas.pydata.org/pandas-docs/stable/user_guide/visualization.html), which have historically produced `matplotlib` plots. Since version 0.25, Pandas has provided a mechanism to use different backends, and as of version 4.8 of `plotly`, you can now use a [Plotly Express-powered](/python/plotly-express/) backend for Pandas plotting. This means you can now produce interactive plots directly from a data frame, without even needing to import Plotly.
4040

41-
To activate it, you just need to set `pd.options.plotting.backend` to `"plotly"` and call `.plot()` to get a `plotly.graph_objects.Figure` object back, just like if you had called Plotly Express directly:
41+
To activate this backend, you will need to [have Plotly installed](/python/getting-started/), and then just need to set `pd.options.plotting.backend` to `"plotly"` and call `.plot()` to get a `plotly.graph_objects.Figure` object back, just like if you had called Plotly Express directly:
4242

4343
```python
4444
import pandas as pd
@@ -64,9 +64,9 @@ fig.show()
6464

6565
### A Note on API Compatibility
6666

67-
> The Plotly plotting backend for Pandas is *not intended* to be a drop-in replacement for the default; it does not implement all or even most of the same keyword arguments, such as `subplots=True` etc.
67+
> The Plotly plotting backend for Pandas is *not intended* to be a drop-in replacement for the default; it does not implement all or even most of the same keyword arguments, such as `subplots=True` etc.
6868
69-
The Plotly plotting backend for Pandas is a more convenient way to invoke certain [Plotly Express](/python/plotly-express/) functions by chaining a `.plot()` call without having to import Plotly Express directly. Plotly Express, as of version 4.8 with [wide-form data support](/python/wide-form/) implements behaviour for the `x` and `y` keywords that are very simlar to the `matplotlib` backend.
69+
The Plotly plotting backend for Pandas is a more convenient way to invoke certain [Plotly Express](/python/plotly-express/) functions by chaining a `.plot()` call without having to import Plotly Express directly. Plotly Express, as of version 4.8 with [wide-form data support](/python/wide-form/) in addition to its robust long-form data support, implements behaviour for the `x` and `y` keywords that are very simlar to the `matplotlib` backend.
7070

7171
In practice, this means that the following two ways of making a chart are identical and support the same additional arguments, because they call the same underlying code:
7272

@@ -85,7 +85,7 @@ fig2 = px.bar(df)
8585
fig2.show()
8686
```
8787

88-
To achieve a similar effect to `subplots=True`, the [Plotly Express `facet_row` and `facet_col` options](/python/facet-plots/) can be used, the same was as they work when directly calling [Plotly Express with wide-form data](/python/wide-form/):
88+
To achieve a similar effect to `subplots=True`, for example, the [Plotly Express `facet_row` and `facet_col` options](/python/facet-plots/) can be used, the same was as they work when directly calling [Plotly Express with wide-form data](/python/wide-form/):
8989

9090
```python
9191
import pandas as pd
@@ -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()`.
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.
102102

103103
```python
104104
import pandas as pd
@@ -107,7 +107,7 @@ pd.options.plotting.backend = "plotly"
107107
np.random.seed(1)
108108

109109
df = pd.DataFrame(dict(
110-
a=np.random.normal(loc=1, scale=2, size=100),
110+
a=np.random.normal(loc=1, scale=2, size=100),
111111
b=np.random.normal(loc=2, scale=1, size=100)
112112
))
113113
fig = df.plot.scatter(x="a", y="b")
@@ -157,7 +157,7 @@ pd.options.plotting.backend = "plotly"
157157
np.random.seed(1)
158158

159159
df = pd.DataFrame(dict(
160-
a=np.random.normal(loc=1, scale=2, size=100),
160+
a=np.random.normal(loc=1, scale=2, size=100),
161161
b=np.random.normal(loc=2, scale=1, size=100)
162162
))
163163
fig = df.plot.hist()
@@ -171,7 +171,7 @@ pd.options.plotting.backend = "plotly"
171171
np.random.seed(1)
172172

173173
df = pd.DataFrame(dict(
174-
a=np.random.normal(loc=1, scale=2, size=100),
174+
a=np.random.normal(loc=1, scale=2, size=100),
175175
b=np.random.normal(loc=2, scale=1, size=100)
176176
))
177177
fig = df.plot.box()
@@ -189,7 +189,7 @@ pd.options.plotting.backend = "plotly"
189189
np.random.seed(1)
190190

191191
df = pd.DataFrame(dict(
192-
a=np.random.normal(loc=1, scale=2, size=100),
192+
a=np.random.normal(loc=1, scale=2, size=100),
193193
b=np.random.normal(loc=2, scale=1, size=100)
194194
))
195195
fig = df.boxplot()

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -1069,11 +1069,8 @@ def process_args_into_dataframe(args, wide_mode, var_name, value_name):
10691069
constants[col_name] = argument.value
10701070
else:
10711071
ranges.append(col_name)
1072-
# ----------------- argument is a col name ----------------------
1073-
elif isinstance(argument, str) or isinstance(
1074-
argument, int
1075-
): # just a column name given as str or int
1076-
1072+
# ----------------- argument is likely a col name ----------------------
1073+
elif isinstance(argument, str) or not hasattr(argument, "__len__"):
10771074
if (
10781075
field_name == "hover_data"
10791076
and hover_data_is_dict
@@ -1135,7 +1132,7 @@ def process_args_into_dataframe(args, wide_mode, var_name, value_name):
11351132
else:
11361133
col_name = str(argument)
11371134
df_output[col_name] = df_input[argument].values
1138-
# ----------------- argument is a column / array / list.... -------
1135+
# ----------------- argument is likely a column / array / list.... -------
11391136
else:
11401137
if df_provided and hasattr(argument, "name"):
11411138
if argument is df_input.index:

packages/python/plotly/plotly/tests/test_core/test_px/test_pandas_backend.py

+12
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,15 @@ def test_pandas_equiv(pandas_fn, px_fn):
2828
pd.options.plotting.backend = "plotly"
2929
df = pd.DataFrame(np.random.randn(100, 4), columns=list("ABCD")).cumsum()
3030
assert pandas_fn(df) == px_fn(df)
31+
32+
33+
@pytest.mark.skipif(
34+
not hasattr(pd.options.plotting, "backend"),
35+
reason="Currently installed pandas doesn't support plotting backends.",
36+
)
37+
def test_pandas_example():
38+
pd.options.plotting.backend = "plotly"
39+
ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000))
40+
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list("ABCD"))
41+
fig = df.iloc[5].plot.bar()
42+
assert len(fig.data) == 1

0 commit comments

Comments
 (0)