
上QQ阅读APP看书,第一时间看更新
How it works...
Here is the explanation of the code:
- plt.close('all') clears the space before starting a new figure.
- fig = plt.figure(1, figsize=(12, 9)) defines figure 1 with a size equal to (12, 9).
- ax1 = plt.subplot2grid((3, 3), (0, 0)) creates ax1 axes, where the first plot will be drawn, and ax1 is the first axes in a 3 x 3 grid of 9 axes. Python indexing starts from 0, 1, and 2, which represent the first, second, and third row or column.
- ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2) creates ax2 axes, which spans two columns of the 3 x 3 grid, starting at the first row and the second column.
- ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2) creates anax3 axes, which spans two columns and two rows, starting at the second row and the first column.
- ax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2) creates an ax4 axis, which spans two rows and one column, starting at the second row and the third column.
- As with the last recipe, we now plot a histogram, bubble plot, scatter plot, and a violin plot on each of the four axes respectively.
- plt.suptitle('Figure 1: Grid Plotting Demo', fontsize=20) sets the title for figure 1, with a font size of 20.
- plt.tight_layout(pad=5, w_pad=0.5, h_pad=1.0) adjusts the space between the plots so that there is no overlapping of labels.
- plt.figure(2, figsize=(12, 5)) starts the second figure with a size of (12, 5).
- The names and the values are Python lists of data to be plotted on the x and the y axis respectively for three plots to be drawn.
- As in the previous example, we plot three plots in a 1 x 3 grid, and plot one bar plot, one scatter plot and one line plot. Then, we set the title for the figure and adjust the space between the plots so that there is no overlap, just as with any other subplot.
You should see the output figures as shown here: