Skip to content

Commit 94bef10

Browse files
addressing PR comments
1 parent db87dda commit 94bef10

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

Diff for: doc/python/categorical-colors.md

+17-13
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jupyter:
3131
language: python
3232
layout: base
3333
name: Categorical Colors
34-
order: 27
34+
order: 28
3535
permalink: python/categorical-color/
3636
thumbnail: thumbnail/heatmap_colorscale.jpg
3737
v4upgrade: true
@@ -57,7 +57,8 @@ For example, in the `tips` dataset, the `smoker` column contains strings:
5757
```python
5858
import plotly.express as px
5959
df = px.data.tips()
60-
fig = px.scatter(df, x="total_bill", y="tip", color="smoker", title="String 'smoker' values mean categorical colors")
60+
fig = px.scatter(df, x="total_bill", y="tip", color="smoker",
61+
title="String 'smoker' values mean categorical colors")
6162

6263
fig.show()
6364
```
@@ -67,7 +68,8 @@ The `size` column, however, contains numbers:
6768
```python
6869
import plotly.express as px
6970
df = px.data.tips()
70-
fig = px.scatter(df, x="total_bill", y="tip", color="size", title="Numeric 'size' values mean continous color")
71+
fig = px.scatter(df, x="total_bill", y="tip", color="size",
72+
title="Numeric 'size' values mean continous color")
7173

7274
fig.show()
7375
```
@@ -78,7 +80,8 @@ Converting this column to strings is very straightforward, but note that the ord
7880
import plotly.express as px
7981
df = px.data.tips()
8082
df["size"] = df["size"].astype(str)
81-
fig = px.scatter(df, x="total_bill", y="tip", color="size", title="String 'size' values mean categorical colors")
83+
fig = px.scatter(df, x="total_bill", y="tip", color="size",
84+
title="String 'size' values mean categorical colors")
8285

8386
fig.show()
8487
```
@@ -91,7 +94,8 @@ df = px.data.tips()
9194
df["size"] = df["size"].astype(str) #convert to string
9295
df["size"] = df["size"].astype(float) #convert back to numeric
9396

94-
fig = px.scatter(df, x="total_bill", y="tip", color="size", title="Numeric 'size' values mean continous color")
97+
fig = px.scatter(df, x="total_bill", y="tip", color="size",
98+
title="Numeric 'size' values mean continous color")
9599

96100
fig.show()
97101
```
@@ -128,7 +132,7 @@ fig = px.line(df, y="lifeExp", x="year", color="continent", line_group="country"
128132
fig.show()
129133
```
130134

131-
### Explicity Constructing a Color Sequence
135+
### Explicitly Constructing a Color Sequence
132136

133137
The Plotly Express `color_discrete_sequence` argument accepts explicitly-constructed color sequences as well, as lists of CSS colors:
134138

@@ -183,12 +187,7 @@ Plotly Express lets you specify an ordering over categorical variables with `cat
183187
import plotly.express as px
184188
df = px.data.gapminder().query("year == 2007")
185189
fig = px.bar(df, y="continent", x="pop", color="continent", orientation="h", hover_name="country",
186-
color_discrete_map={
187-
"Europe": "red",
188-
"Asia": "green",
189-
"Americas": "blue",
190-
"Oceania": "goldenrod",
191-
"Africa": "magenta"},
190+
color_discrete_sequence=["red", "green", "blue", "goldenrod", "magenta"],
192191
category_orders={"continent": ["Oceania", "Europe", "Asia", "Africa", "Americas"]},
193192
title="Explicit color sequence with explicit ordering"
194193
)
@@ -200,7 +199,12 @@ fig.show()
200199
import plotly.express as px
201200
df = px.data.gapminder().query("year == 2007")
202201
fig = px.bar(df, y="continent", x="pop", color="continent", orientation="h", hover_name="country",
203-
color_discrete_sequence=["red", "green", "blue", "goldenrod", "magenta"],
202+
color_discrete_map={
203+
"Europe": "red",
204+
"Asia": "green",
205+
"Americas": "blue",
206+
"Oceania": "goldenrod",
207+
"Africa": "magenta"},
204208
category_orders={"continent": ["Oceania", "Europe", "Asia", "Africa", "Americas"]},
205209
title="Explicit color mapping with explicit ordering"
206210
)

Diff for: doc/python/colorscales.md

+21-6
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ In the same way as the X or Y position of a mark in cartesian coordinates can be
4646

4747
This document explains the following four continuous-color-related concepts:
4848

49-
- **color scales** represent a mapping between the range 0 to 1 and some color domain within which colors are to be interpolated (unlike [categorical color sequences](/python/categorical-color/) which are never interpolated). Color scale defaults depend on the `layout.colorscales` attributes of the active [template](/python/templates/), and can be explicitly specified using the `color_continuous_scale` argument for many [Plotly Express](/python/plotly-express/) functions or the `colorscale` argument in various `graph_objects` such as `layout.coloraxis` or `marker.colorscale` in `go.Scatter` traces. For example `[(0,"blue"), (1,"red")]` is a simple color scale that interpolated between blue and red via purple, which can also be implicitly represented as `["blue", "red"]` and happens to be one of the [built-in color scales](/python/builtin-colorscales) and therefore referred to as `"bluered"` or `plotly.colors.sequential.Bluered`.
50-
- **color ranges** represent the minimum to maximum range of data to be mapped onto the 0 to 1 input range of the color scale. Color ranges default to the range of the input data and can be explicitly specified using either the `range_color` or `color_continous_midpoint` arguments for many Plotly Express functions, or `cmin`/`cmid`/`cmax` or `zmin`/`zmid`/`zmax` for various `graph_objects` such as `layout.coloraxis.cmin` or `marker.cmin` in `go.Scatter` traces. For example, if a color range of `[100, 200]` is used with the color scale above, then any mark with a color value of 100 or less will be blue, and 200 or more will be red. Marks with values in between will be various shades of purple.
51-
- **color bars** are legend-like visible representations of the color range and color scale with optional tick labels and tick marks. Color bars can be configured with attributes inside `layout.coloraxis.colorbar` or in places like `marker.colorbar` in `go.Scatter` traces.
52-
- **color axes** connect color scales, color ranges and color bars to a trace's data. By default, any colorable attribute in a trace is attached to its own local color axis, but color axes may also be shared across attributes and traces by setting e.g. `marker.coloraxis` in `go.Scatter` traces. Local color axis attributes are configured within traces e.g. `marker.showscale` whereas shared color axis attributes are configured within the Layout e.g. `layout.coloraxis.showscale`.
49+
- **color scales** represent a mapping between the range 0 to 1 and some color domain within which colors are to be interpolated (unlike [categorical color sequences](/python/categorical-color/) which are never interpolated). Color scale defaults depend on the `layout.colorscales` attributes of the active [template](/python/templates/), and can be explicitly specified using the `color_continuous_scale` argument for many [Plotly Express](/python/plotly-express/) functions or the `colorscale` argument in various `graph_objects` such as `layout.coloraxis` or `marker.colorscale` in `go.Scatter` traces or `colorscale` in `go.Heatmap` traces. For example `[(0,"blue"), (1,"red")]` is a simple color scale that interpolated between blue and red via purple, which can also be implicitly represented as `["blue", "red"]` and happens to be one of the [built-in color scales](/python/builtin-colorscales) and therefore referred to as `"bluered"` or `plotly.colors.sequential.Bluered`.
50+
- **color ranges** represent the minimum to maximum range of data to be mapped onto the 0 to 1 input range of the color scale. Color ranges default to the range of the input data and can be explicitly specified using either the `range_color` or `color_continous_midpoint` arguments for many Plotly Express functions, or `cmin`/`cmid`/`cmax` or `zmin`/`zmid`/`zmax` for various `graph_objects` such as `layout.coloraxis.cmin` or `marker.cmin` in `go.Scatter` traces or `cmin` in `go.Heatmap` traces. For example, if a color range of `[100, 200]` is used with the color scale above, then any mark with a color value of 100 or less will be blue, and 200 or more will be red. Marks with values in between will be various shades of purple.
51+
- **color bars** are legend-like visible representations of the color range and color scale with optional tick labels and tick marks. Color bars can be configured with attributes inside `layout.coloraxis.colorbar` or in places like `marker.colorbar` in `go.Scatter` traces or `colorbar` in `go.Heatmap` traces.
52+
- **color axes** connect color scales, color ranges and color bars to a trace's data. By default, any colorable attribute in a trace is attached to its own local color axis, but color axes may also be shared across attributes and traces by setting e.g. `marker.coloraxis` in `go.Scatter` traces or `coloraxis` in `go.Heatmap` traces. Local color axis attributes are configured within traces e.g. `marker.showscale` whereas shared color axis attributes are configured within the Layout e.g. `layout.coloraxis.showscale`.
5353

5454

5555
### Continuous Color with Plotly Express
@@ -61,7 +61,8 @@ For example, in the `tips` dataset, the `size` column contains numbers:
6161
```python
6262
import plotly.express as px
6363
df = px.data.tips()
64-
fig = px.scatter(df, x="total_bill", y="tip", color="size", title="Numeric 'size' values mean continous color")
64+
fig = px.scatter(df, x="total_bill", y="tip", color="size",
65+
title="Numeric 'size' values mean continous color")
6566

6667
fig.show()
6768
```
@@ -72,7 +73,21 @@ Converting this column to strings is very straightforward:
7273
import plotly.express as px
7374
df = px.data.tips()
7475
df["size"] = df["size"].astype(str)
75-
fig = px.scatter(df, x="total_bill", y="tip", color="size", title="String 'size' values mean discrete colors")
76+
fig = px.scatter(df, x="total_bill", y="tip", color="size",
77+
title="String 'size' values mean discrete colors")
78+
79+
fig.show()
80+
```
81+
82+
If you have stringified numbers you can convert back just as easily:
83+
84+
```python
85+
import plotly.express as px
86+
df = px.data.tips()
87+
df["size"] = df["size"].astype(str)
88+
df["size"] = df["size"].astype(float)
89+
fig = px.scatter(df, x="total_bill", y="tip", color="size",
90+
title="Numeric 'size' values mean continous color")
7691

7792
fig.show()
7893
```

0 commit comments

Comments
 (0)