diff --git a/_posts/python/statistical/facet-trellis/2015-06-30-facet-and-trellis-plots.html b/_posts/python/statistical/facet-trellis/2015-06-30-facet-and-trellis-plots.html new file mode 100644 index 000000000000..cfee60861827 --- /dev/null +++ b/_posts/python/statistical/facet-trellis/2015-06-30-facet-and-trellis-plots.html @@ -0,0 +1,877 @@ +--- +permalink: python/facet-trellis/ +description: How to make Facet and Trellis Plots in Python with Plotly. +name: Facet and Trellis Plots +has_thumbnail: true +thumbnail: thumbnail/facet-trellis-thumbnail.jpg +layout: user-guide +language: python +title: Python Facet and Trellis Plots | plotly +display_as: statistical +has_thumbnail: true +page_type: example_index +order: 10.2 +--- +{% raw %} +
Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
+
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
+
We also have a quick-reference cheatsheet (new!) to help you get started!
Note: Facet Grids and Trellis Plots
are available in version 2.0.12+
+Run pip install plotly --upgrade
to update your Plotly version
import plotly
+plotly.__version__
+
A facet grid
is a generalization of a scatterplot matrix where we can "facet" a row and/or column by another variable. Given some tabular data, stored in a pandas.DataFrame
, we can plot one variable against another to form a regular scatter plot, and we can pick a third faceting variable to form panels along the rows and/or columns to segment the data even further, forming a bunch of panels. We can also assign a coloring rule or a heatmap based on a color variable to color the plot.
import plotly.plotly as py
+import plotly.figure_factory as ff
+
+import pandas as pd
+mpg = pd.read_table('https://raw.githubusercontent.com/plotly/datasets/master/mpg_2017.txt')
+
+grid = ff.create_facet_grid(
+ mpg,
+ x='displ',
+ y='cty',
+ facet_col='cyl',
+)
+
+py.iplot(grid, filename='facet by col')
+
import plotly.plotly as py
+import plotly.figure_factory as ff
+
+import pandas as pd
+mpg = pd.read_table('https://raw.githubusercontent.com/plotly/datasets/master/mpg_2017.txt')
+
+grid = ff.create_facet_grid(
+ mpg,
+ x='displ',
+ y='cty',
+ facet_row='cyl',
+ marker={'color': 'rgb(86, 7, 100)'},
+)
+
+py.iplot(grid, filename='facet by row')
+
import plotly.plotly as py
+import plotly.figure_factory as ff
+
+import pandas as pd
+mpg = pd.read_table('https://raw.githubusercontent.com/plotly/datasets/master/mpg_2017.txt')
+
+grid = ff.create_facet_grid(
+ mpg,
+ x='displ',
+ y='cty',
+ facet_row='cyl',
+ facet_col='drv',
+ marker={'color': 'rgb(234, 239, 155)'},
+)
+
+py.iplot(grid, filename='facet by row and col')
+
import plotly.plotly as py
+import plotly.figure_factory as ff
+
+import pandas as pd
+mtcars = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/mtcars.csv')
+
+fig = ff.create_facet_grid(
+ mtcars,
+ x='mpg',
+ y='wt',
+ facet_col='cyl',
+ color_name='cyl',
+ color_is_cat=True,
+)
+py.iplot(fig, filename='facet - color by categorical variable')
+
import plotly.plotly as py
+import plotly.figure_factory as ff
+
+import pandas as pd
+tips = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/tips.csv')
+
+fig = ff.create_facet_grid(
+ tips,
+ x='total_bill',
+ y='tip',
+ color_name='sex',
+ show_boxes=False,
+ marker={'size': 10, 'opacity': 1.0},
+ colormap={'Male': 'rgb(165, 242, 242)', 'Female': 'rgb(253, 174, 216)'}
+)
+py.iplot(fig, filename='facet - custom colormap')
+
import plotly.plotly as py
+import plotly.figure_factory as ff
+
+import pandas as pd
+mtcars = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/mtcars.csv')
+
+fig = ff.create_facet_grid(
+ mtcars,
+ x='mpg',
+ y='wt',
+ facet_col='cyl',
+ facet_col_labels='name',
+ facet_row_labels='name',
+)
+py.iplot(fig, filename='facet - label variable name')
+
import plotly.plotly as py
+import plotly.figure_factory as ff
+
+import pandas as pd
+mtcars = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/mtcars.csv')
+
+fig = ff.create_facet_grid(
+ mtcars,
+ x='wt',
+ y='mpg',
+ facet_col='cyl',
+ facet_col_labels={4: '$2^2 = 4$', 6: '$\\frac{18}{3} = 6$', 8: '$2\cdot4 = 8$'},
+ marker={'color': 'rgb(240, 100, 2)'},
+)
+
+py.iplot(fig, filename='facet - custom labels')
+
To learn more about ggplot2, check out http://ggplot2.tidyverse.org/reference/facet_grid.html
+ +import plotly.plotly as py
+import plotly.figure_factory as ff
+
+import pandas as pd
+tips = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/tips.csv')
+
+fig = ff.create_facet_grid(
+ tips,
+ x='total_bill',
+ y='tip',
+ facet_row='sex',
+ facet_col='smoker',
+ marker={'symbol': 'circle-open', 'size': 10},
+ ggplot2=True
+)
+py.iplot(fig, filename='facet - ggplot2 style')
+
import plotly.plotly as py
+import plotly.figure_factory as ff
+
+import pandas as pd
+mpg = pd.read_table('https://raw.githubusercontent.com/plotly/datasets/master/mpg_2017.txt')
+
+grid = ff.create_facet_grid(
+ mpg,
+ x='class',
+ y='displ',
+ trace_type='scattergl',
+)
+
+py.iplot(grid, filename='facet - scattergl')
+
import plotly.plotly as py
+import plotly.figure_factory as ff
+
+import pandas as pd
+tips = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/tips.csv')
+
+fig = ff.create_facet_grid(
+ tips,
+ x='total_bill',
+ y='tip',
+ facet_row='sex',
+ facet_col='smoker',
+ trace_type='histogram',
+)
+
+py.iplot(fig, filename='facet - histogram traces')
+
Facet Grids support scatter
, scattergl
, histogram
, bar
and box
trace types. More trace types coming in the future.
import plotly.plotly as py
+import plotly.figure_factory as ff
+
+import pandas as pd
+tips = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/tips.csv')
+
+fig = ff.create_facet_grid(
+ tips,
+ y='tip',
+ facet_row='sex',
+ facet_col='smoker',
+ trace_type='box',
+)
+
+py.iplot(fig, filename='facet - box traces')
+
help(ff.create_facet_grid)
+