- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to plot multiple figures as subplots in Python Plotly?
Plotly is an open-source Python library for creating charts. You can use the features available in Plotly to set multiple figures as subplots.
In this tutorial, we will use plotly.graph_objects to generate the figures. It contains a lot of methods to customize chart and render a chart into HTML format. For example, plotly.subplots() method can be used to add subplots.
Follow the steps given below to create subplots with Plotly express.
Step 1
Import plotly.graphs_objs module and alias as go.
import plotly.graphs_objs as go
Step 2
Import make_subplots to create subplots.
from plotly.subplots import make_subplots
Step 3
Create subplots for 3 rows and 1 column.
fig = make_subplots(rows=3, cols=1)
Step 4
Create the append_trace() method to append the Scatter plots.
fig.append_trace(go.Scatter( x=[1,2,3,4,5], y=[5,6,7,8,9], ), row=1, col=1) fig.append_trace(go.Scatter( x=[3,4,5,6,7], y=[10,11,12,9,8], ), row=2, col=1) fig.append_trace(go.Scatter( x=[4,5,6,7,8], y=[6,7,8,9,10] ), row=3, col=1)
Step 5
Use the update_layout() method to set the layout size.
fig.update_layout(height=400, width=400, title_text="Subplots")
Example
Here is the complete code to plot multiple figures as subplots −
from plotly.subplots import make_subplots import plotly.graph_objects as go fig = make_subplots(rows=3, cols=1) fig.append_trace(go.Scatter( x=[1,2,3,4,5], y=[5,6,7,8,9], ), row=1, col=1) fig.append_trace(go.Scatter( x=[3,4,5,6,7], y=[10,11,12,9,8], ), row=2, col=1) fig.append_trace(go.Scatter( x=[4,5,6,7,8], y=[6,7,8,9,10] ), row=3, col=1) fig.update_layout(height=450, width=716, title_text="Subplots") fig.show()
Output
It will show the following output on the browser −
- Related Articles
- How to plot pie charts as subplots with custom size in Python Plotly?
- How to create subplots with Python Plotly?
- How to plot multiple lines on the same Y-axis in Python Plotly?
- How to apply different titles for each different subplots using Plotly in Python?
- How to draw a multiple line chart using Plotly Express in Python Plotly?
- How to plot on secondary Y-Axis with Python Plotly?
- How to add multiple graphs to a Plotly Dash app on a single browser page in Python Plotly?
- How to add multiple text labels from DataFrame columns in Python Plotly?
- Python Plotly – How to simultaneously apply color/shape/size in a Scatter Plot?
- How to save multiple plots into a single HTML file in Python Plotly?
- Python Plotly – How to manually set the color of points in a scatter plot?
- Drawing multiple figures in parallel in Python with Matplotlib
- Plotting a horizontal line on multiple subplots in Python using pyplot
- Python Plotly – How to hide legend entries in a Plotly figure?
- How to show multiple ggplot2 plots with Plotly using R?
