Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Theming chapter#28

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
jonmmease merged 9 commits intomasterfromtemplates
Jun 27, 2019
Merged
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
No chaining
  • Loading branch information
@jonmmease
jonmmease committedJun 23, 2019
commit85e24bc4f065e1fc915a454713a0d588ad08ebed
116 changes: 58 additions & 58 deletionsnotebooks/templates.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -51,11 +51,11 @@ gapminder = px.data.gapminder()
gapminder_2007 = gapminder.query("year==2007")

for template in ["plotly", "plotly_white", "plotly_dark", "ggplot2", "seaborn", "none"]:
px.scatter(gapminder_2007,
x="gdpPercap", y="lifeExp", size="pop", color="continent",
log_x=True, size_max=60,
template=template, title="Gapminder 2007: '%s' theme" % template
).show()
fig =px.scatter(gapminder_2007,
x="gdpPercap", y="lifeExp", size="pop", color="continent",
log_x=True, size_max=60,
template=template, title="Gapminder 2007: '%s' theme" % template)
fig.show()
```

### Specifying themes to graph object figures
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

"in" rather than "to" ?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Expand All@@ -71,12 +71,13 @@ fig = go.Figure(
data=go.Surface(z=z_data.values),
layout=go.Layout(
title="Mt Bruno Elevation",
width=500,
width=500,
height=500,
))
))

for template in ["plotly", "plotly_white", "plotly_dark", "ggplot2", "seaborn", "none"]:
fig.update_layout(template=template, title="Mt Bruno Elevation: '%s' theme" % template).show()
fig.update_layout(template=template, title="Mt Bruno Elevation: '%s' theme" % template)
fig.show()
```

### Specifying a default themes
Expand All@@ -93,11 +94,11 @@ pio.templates.default = "plotly_white"
gapminder = px.data.gapminder()
gapminder_2007 = gapminder.query("year==2007")

px.scatter(gapminder_2007,
x="gdpPercap", y="lifeExp", size="pop", color="continent",
log_x=True, size_max=60,
title="Gapminder 2007: current default theme"
).show()
fig =px.scatter(gapminder_2007,
x="gdpPercap", y="lifeExp", size="pop", color="continent",
log_x=True, size_max=60,
title="Gapminder 2007: current default theme")
fig.show()
```

### Disable default theming
Expand DownExpand Up@@ -125,10 +126,10 @@ large_rockwell_template = go.layout.Template(
layout=go.Layout(dict(title=dict(font=dict(family="Rockwell", size=24))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

is it possible to use the magic underscore here, liketitle_font=dict(..) ? (and to add a comment about it)

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

)

(go.Figure()
.update_layout(title="Figure Title",
template=large_rockwell_template)
).show()
fig =go.Figure()
fig.update_layout(title="Figure Title",
template=large_rockwell_template)
fig.show()
```

### The template data property
Expand All@@ -142,10 +143,10 @@ import plotly.graph_objects as go
diamond_template = go.layout.Template()
diamond_template.data.scatter = [go.Scatter(marker=dict(symbol="diamond", size=20))]

(go.Figure()
.update_layout(template=diamond_template)
.add_scatter(y=[2, 1, 3], mode="markers")
).show()
fig =go.Figure()
fig.update_layout(template=diamond_template)
fig.add_scatter(y=[2, 1, 3], mode="markers")
fig.show()
```

If a trace type property is set to a list of more than one trace, then the default properties are cycled as more traces are added to the figure. Here is an example that creates a template that cycles the default marker symbol for scatter traces, and then constructs a figure that uses this template.
Expand All@@ -159,14 +160,13 @@ symbol_template.data.scatter = [
go.Scatter(marker=dict(symbol="square", size=10)),
go.Scatter(marker=dict(symbol="circle", size=10)),
]

(go.Figure()
.update_layout(template=symbol_template)
.add_scatter(y=[1, 2, 3], mode="markers", name="first")
.add_scatter(y=[2, 3, 4], mode="markers", name="second")
.add_scatter(y=[3, 4, 5], mode="markers", name="third")
.add_scatter(y=[4, 5, 6], mode="markers", name="forth")
).show()
fig = go.Figure()
fig.update_layout(template=symbol_template)
fig.add_scatter(y=[1, 2, 3], mode="markers", name="first")
fig.add_scatter(y=[2, 3, 4], mode="markers", name="second")
fig.add_scatter(y=[3, 4, 5], mode="markers", name="third")
fig.add_scatter(y=[4, 5, 6], mode="markers", name="forth")
fig.show()
```

Note that because we built the template with a list of 3 scatter trace graph objects (one each for the diamond, square, and circle symbols), the forth scatter trace in the figure cycles around and takes on the defaults specified in the first template trace (The diamond symbol).
Expand All@@ -184,16 +184,15 @@ import plotly.graph_objects as go

annotation_template = go.layout.Template()
annotation_template.layout.annotationdefaults = go.layout.Annotation(font=dict(color="crimson"))

(go.Figure()
.update_layout(
fig = go.Figure()
fig.update_layout(
template=annotation_template,
annotations=[
go.layout.Annotation(text="Look Here", x=1, y=1),
go.layout.Annotation(text="Look There", x=2, y=2)
]
)
).show()
fig.show()
```

### Including array elements in a theme
Expand DownExpand Up@@ -223,9 +222,9 @@ draft_template.layout.annotations = [
)
]

(go.Figure()
.update_layout(template=draft_template)
).show()
fig=go.Figure()
fig.update_layout(template=draft_template)
fig.show()
```

### Customizing theme array elements in a figure
Expand All@@ -252,17 +251,17 @@ draft_template.layout.annotations = [
)
]

(go.Figure()
.update_layout(
template=draft_template,
annotations=[
go.layout.Annotation(
templateitemname="draft watermark",
text="CONFIDENTIAL",
)
]
)
).show()
fig =go.Figure()
fig.update_layout(
template=draft_template,
annotations=[
go.layout.Annotation(
templateitemname="draft watermark",
text="CONFIDENTIAL",
)
]
)
fig.show()
```

### Registering themes as named templates
Expand DownExpand Up@@ -291,9 +290,9 @@ pio.templates["draft"] = go.layout.Template(
]
)

(go.Figure()
.update_layout(template="draft")
).show()
fig =go.Figure()
fig.update_layout(template="draft")
fig.show()
```

It is also possible to set your own custom template as the default so that you do not need to pass it by name when constructing graph object figures or calling plotly express functions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Github's syntax highlighting is confused about this block... does it render OK in Jupyter?

Expand All@@ -320,7 +319,8 @@ pio.templates["draft"] = go.layout.Template(
)
pio.templates.default = "draft"

go.Figure().show()
fig = go.Figure()
fig.show()
```

### Combining themes
Expand DownExpand Up@@ -352,7 +352,8 @@ pio.templates["draft"] = go.layout.Template(
)
pio.templates.default = "plotly+draft"

go.Figure().show()
fig = go.Figure()
fig.show()
```

Combining themes is also supported by plotly express
Expand DownExpand Up@@ -381,12 +382,11 @@ pio.templates.default = "plotly+draft"

gapminder = px.data.gapminder()
gapminder_2007 = gapminder.query("year==2007")

px.scatter(gapminder_2007,
x="gdpPercap", y="lifeExp", size="pop", color="continent",
log_x=True, size_max=60,
title="Gapminder 2007: current default theme"
).show()
fig = px.scatter(gapminder_2007,
x="gdpPercap", y="lifeExp", size="pop", color="continent",
log_x=True, size_max=60,
title="Gapminder 2007: current default theme")
fig.show()
```

<!-- #region -->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

what does this "region" thing do?

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp