Python

Plotly.Figure_Factory.Create_Facet_Grid

In this short tutorial, we will discuss the method of creating the facet grid using the figure_factory module and the facet_grid() function.

Function Syntax and Parameter List

The function’s syntax is as shown in the following code:

plotly.figure_factory.create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, color_name=None, colormap=None, color_is_cat=False, facet_row_labels=None, facet_col_labels=None, height=None, width=None, trace_type='scatter', scales='fixed', dtick_x=None, dtick_y=None, show_boxes=True, ggplot2=False, binsize=1, **kwargs)

The following are some important parameters that you need to know:

  1. df – Specifies the data frame used in the plot.
  2. x – Sets the values for the x axis.
  3. y – Sets the data used for the y axis.
  4. color_name – Sets the column used as the colormap variable.

Facet Grid with Plotly Figure_Factory

The following code shows how to use the facet_grid function to create a facet grid plot for the Plotly tops data:

from plotly.figure_factory import create_facet_grid

import plotly.express as px

df = px.data.tips()

fig = create_facet_grid(df, x='total_bill', y='tip', facet_col='sex')

fig.show()

The given code returns a facet grid as shown in the following:

To color the marks by the sex, we can specify the color_name parameter to the sex column within the data frame.

An example is as follows:

from plotly.figure_factory import create_facet_grid

import plotly.express as px

df = px.data.tips()

fig = create_facet_grid(df, x='total_bill', y='tip', facet_col='sex', color_name='sex')

fig.show()

The output figure is as shown in the following:

You can also create multiple facet grids by specifying the facet_row parameter as shown in the following:

from plotly.figure_factory import create_facet_grid

import plotly.express as px

df = px.data.tips()

fig = create_facet_grid(df, x='total_bill', y='tip', facet_col='sex', facet_row='smoker', color_name='sex')

fig.show()

Output Result:

Conclusion

In this post, we explored how to create the faceted grids using the figure_factory module and facet_grid() function. Feel free to explore the docs for more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list