Combining Multiple Charts and Subplots
When you want to compare different datasets or highlight multiple perspectives within the same visualization, combining charts using subplots is an invaluable technique. Subplots let you display several charts β such as scatter plots, bar charts, or line graphs β side by side or stacked within a single figure. This approach makes it much easier to spot patterns, contrasts, or correlations between different variables at a glance. For example, you might want to show the distribution of two variables with a scatter plot while summarizing their counts in a bar chart, all within one view for direct comparison.
1234567891011121314151617181920212223import plotly.graph_objs as go from plotly.subplots import make_subplots from IPython.display import display, HTML # Create a subplot figure with 1 row and 2 columns fig = make_subplots(rows=1, cols=2, subplot_titles=("Scatter Plot", "Bar Chart")) # Add a scatter plot to the first subplot fig.add_trace( go.Scatter(x=[1, 2, 3, 4], y=[10, 15, 13, 17], mode="markers", name="Scatter"), row=1, col=1 ) # Add a bar chart to the second subplot fig.add_trace( go.Bar(x=["A", "B", "C", "D"], y=[5, 7, 3, 8], name="Bar"), row=1, col=2 ) fig.update_layout(title_text="Multiple Charts with Subplots") html = fig.to_html(full_html=False, include_plotlyjs="cdn") display(HTML(html))
To build this combined figure, you first create a subplot layout by calling make_subplots, specifying the number of rows and columns you want. In this case, the figure has one row and two columns, so the charts appear side by side. The subplot_titles parameter labels each subplot for quick identification. You then add each chart type using add_trace, targeting a specific row and column. The scatter plot is placed in the first column, while the bar chart is positioned in the second column. Each trace can have its own data and chart type, allowing for diverse visualizations within the same figure. Finally, you can set a shared title or further adjust the layout as needed.
1234567891011121314151617181920212223242526272829303132333435363738import plotly.graph_objs as go from plotly.subplots import make_subplots from IPython.display import display, HTML # Create subplots with custom titles and axis labels fig = make_subplots( rows=1, cols=2, subplot_titles=("Age vs. Score", "Category Counts") ) # Scatter plot with axis labels fig.add_trace( go.Scatter( x=[18, 22, 27, 35], y=[80, 85, 90, 95], mode="markers", name="Scores" ), row=1, col=1 ) fig.update_xaxes(title_text="Age", row=1, col=1) fig.update_yaxes(title_text="Score", row=1, col=1) # Bar chart with axis labels fig.add_trace( go.Bar( x=["Group A", "Group B", "Group C"], y=[20, 14, 23], name="Counts" ), row=1, col=2 ) fig.update_xaxes(title_text="Group", row=1, col=2) fig.update_yaxes(title_text="Count", row=1, col=2) fig.update_layout(title_text="Customized Subplot Titles and Axis Labels") html = fig.to_html(full_html=False, include_plotlyjs="cdn") display(HTML(html))
When arranging subplots, make sure each chart is clearly labeled with titles and axis descriptions. This helps viewers understand what each subplot represents without confusion. Always use the subplot_titles parameter to name each chart, and set axis labels for both x and y axes using update_xaxes and update_yaxes. Keep your layouts balanced β avoid overcrowding β and align related charts in a way that makes comparisons straightforward. Referencing the previous examples, you can see how different chart types and clear labeling combine to make your visualizations more informative and accessible.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 11.11
Combining Multiple Charts and Subplots
Swipe to show menu
When you want to compare different datasets or highlight multiple perspectives within the same visualization, combining charts using subplots is an invaluable technique. Subplots let you display several charts β such as scatter plots, bar charts, or line graphs β side by side or stacked within a single figure. This approach makes it much easier to spot patterns, contrasts, or correlations between different variables at a glance. For example, you might want to show the distribution of two variables with a scatter plot while summarizing their counts in a bar chart, all within one view for direct comparison.
1234567891011121314151617181920212223import plotly.graph_objs as go from plotly.subplots import make_subplots from IPython.display import display, HTML # Create a subplot figure with 1 row and 2 columns fig = make_subplots(rows=1, cols=2, subplot_titles=("Scatter Plot", "Bar Chart")) # Add a scatter plot to the first subplot fig.add_trace( go.Scatter(x=[1, 2, 3, 4], y=[10, 15, 13, 17], mode="markers", name="Scatter"), row=1, col=1 ) # Add a bar chart to the second subplot fig.add_trace( go.Bar(x=["A", "B", "C", "D"], y=[5, 7, 3, 8], name="Bar"), row=1, col=2 ) fig.update_layout(title_text="Multiple Charts with Subplots") html = fig.to_html(full_html=False, include_plotlyjs="cdn") display(HTML(html))
To build this combined figure, you first create a subplot layout by calling make_subplots, specifying the number of rows and columns you want. In this case, the figure has one row and two columns, so the charts appear side by side. The subplot_titles parameter labels each subplot for quick identification. You then add each chart type using add_trace, targeting a specific row and column. The scatter plot is placed in the first column, while the bar chart is positioned in the second column. Each trace can have its own data and chart type, allowing for diverse visualizations within the same figure. Finally, you can set a shared title or further adjust the layout as needed.
1234567891011121314151617181920212223242526272829303132333435363738import plotly.graph_objs as go from plotly.subplots import make_subplots from IPython.display import display, HTML # Create subplots with custom titles and axis labels fig = make_subplots( rows=1, cols=2, subplot_titles=("Age vs. Score", "Category Counts") ) # Scatter plot with axis labels fig.add_trace( go.Scatter( x=[18, 22, 27, 35], y=[80, 85, 90, 95], mode="markers", name="Scores" ), row=1, col=1 ) fig.update_xaxes(title_text="Age", row=1, col=1) fig.update_yaxes(title_text="Score", row=1, col=1) # Bar chart with axis labels fig.add_trace( go.Bar( x=["Group A", "Group B", "Group C"], y=[20, 14, 23], name="Counts" ), row=1, col=2 ) fig.update_xaxes(title_text="Group", row=1, col=2) fig.update_yaxes(title_text="Count", row=1, col=2) fig.update_layout(title_text="Customized Subplot Titles and Axis Labels") html = fig.to_html(full_html=False, include_plotlyjs="cdn") display(HTML(html))
When arranging subplots, make sure each chart is clearly labeled with titles and axis descriptions. This helps viewers understand what each subplot represents without confusion. Always use the subplot_titles parameter to name each chart, and set axis labels for both x and y axes using update_xaxes and update_yaxes. Keep your layouts balanced β avoid overcrowding β and align related charts in a way that makes comparisons straightforward. Referencing the previous examples, you can see how different chart types and clear labeling combine to make your visualizations more informative and accessible.
Thanks for your feedback!