![]() ![]() Way 6: Using subplot_mosaic( ) - Only Matplotlib 3.3 This is the most recent method only available in Matplotlib 3.3 If you want a subplot to span multiple rows or columns, you specify the length of the span using the keywords rowspan or colspan. So, for a 3 x 3 grid, both the row and the column indices will be 0, 1, and 2. Then you specify the starting location of the subplot using a tuple of indices in the order (row, column) where the indices start at 0. You have to first specify the overall grid size like (3, 3) in the example code below. Here, you create subplots at specified locations within the overall grid. This method is useful for generating complex grids where the subplots span multiple rows or columns. The first two indices are the number of total rows and columns, respectively while the third number specifies the subplot. So, for a 2 x 2 grid, the upper row will use the numbers (2, 2, 1), (2, 2, 2) and the second row will use the numbers (2, 2, 3), (2, 2, 4), respectively. If you want multiple subplots to share the same x or y-axis, you can specify the corresponding axis while creating the subplot as demonstrated below. The following example creates a 2 x 2 grid. In this method, you first create the figure object and then add the subplots manually one after the other. The first and the second indices in the slice notation correspond to the row (i) and column (j) number, respectively. So, for 2 rows and 2 columns, the indices will be 0 and 1. When you have more than 1 row and 1 column, you need two indices to access the individual subplots as shown in the code below. Now you will have to use the keyword sharex. ![]() To plot them in two rows, you can use nrows=2, ncols=1. Subplots spanning multiple rows: In the above figure, the subplots were plotted in a columnar way. Since both the above subplots have the same y-axis limits, you can remove the redundant y-axis values from the right-hand side subplot using the keyword sharey=True. The tuple (ax1, ax2) below represents the axis handles to individual subplots. Note: If you don’t like the indices notation, you can also use names for your axes as shown below, and then use them directly for plotting. Subplots created using “subplots( )” module fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(9, 3)) ot(x, y_1, '-', c='orange', label='sin(x)') ot(x, y_2, '-', c='magenta', label='cos(x)') axes.legend(fontsize=16, frameon=False) axes.legend(fontsize=16, frameon=False) fig.suptitle('Subplots without shared y-axis') Since the axes object contains two subplots, you can access them using indices and because indexing starts at 0 in Python. Let’s now create our very first two subplots with a single row and two columns. We use our immortal sin and cosine curves for □∈(0, 3□). %matplotlib inline # To enable inline plotting in Jupyter Notebook import numpy as np import matplotlib.pyplot as plt ('fivethirtyeight') # For better style Let’s first import some basic modules and use a fancy style sheet to give an artistic touch to our figures. Way 1: Using subplots( ) Plotting single rows or columns ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |