| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,9 @@ | |||
| 6 | 6 | Show the marginal distributions of a scatter plot as histograms at the sides of | |
| 7 | 7 | the plot. | |
| 8 | 8 | ||
| 9 | + This layout has a central scatter plot, with marginal histograms above and to the | ||
| 10 | + right of the central plot. | ||
| 11 | + | ||
| 9 | 12 | For a nice alignment of the main Axes with the marginals, two options are shown | |
| 10 | 13 | below: | |
| 11 | 14 | ||
@@ -55,27 +58,27 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy): | |||
| 55 | 58 | ||
| 56 | 59 | ||
| 57 | 60 | # %% | |
| 61 | + # Defining the Axes positions using subplot_mosaic | ||
| 62 | + # ------------------------------------------------ | ||
| 58 | 63 | # | |
| 59 | - # Defining the Axes positions using a gridspec | ||
| 60 | - # -------------------------------------------- | ||
| 64 | + # We use the `~.pyplot.subplot_mosaic` function to define the positions and | ||
| 65 | + # names of the three axes; the empty axes is specified by `'.'`. We manually | ||
| 66 | + # specify the size of the figure, and can make the different axes have | ||
| 67 | + # different sizes by specifying the *width_ratios* and *height_ratios* | ||
| 68 | + # arguments. The *layout* argument is set to `'constrained'` to optimize the | ||
| 69 | + # spacing between the axes. | ||
| 61 | 70 | # | |
| 62 | - # We define a gridspec with unequal width- and height-ratios to achieve desired | ||
| 63 | - # layout. Also see the :ref:`arranging_axes` tutorial. | ||
| 64 | - | ||
| 65 | - # Start with a square Figure. | ||
| 66 | - fig = plt.figure(figsize=(6, 6)) | ||
| 67 | - # Add a gridspec with two rows and two columns and a ratio of 1 to 4 between | ||
| 68 | - # the size of the marginal Axes and the main Axes in both directions. | ||
| 69 | - # Also adjust the subplot parameters for a square plot. | ||
| 70 | - gs = fig.add_gridspec(2, 2, width_ratios=(4, 1), height_ratios=(1, 4), | ||
| 71 | - left=0.1, right=0.9, bottom=0.1, top=0.9, | ||
| 72 | - wspace=0.05, hspace=0.05) | ||
| 73 | - # Create the Axes. | ||
| 74 | - ax = fig.add_subplot(gs[1, 0]) | ||
| 75 | - ax_histx = fig.add_subplot(gs[0, 0], sharex=ax) | ||
| 76 | - ax_histy = fig.add_subplot(gs[1, 1], sharey=ax) | ||
| 77 | - # Draw the scatter plot and marginals. | ||
| 78 | - scatter_hist(x, y, ax, ax_histx, ax_histy) | ||
| 71 | + # Note that there are other ways to accomplish the same layout using a gridspec | ||
| 72 | + # and only adding the three desired axes, or by using `~.pyplot.subplots` and | ||
| 73 | + # removing the unused axes in the upper right. The arguments are similar to the ones | ||
| 74 | + # used here. | ||
| 75 | + | ||
| 76 | + fig, axs = plt.subplot_mosaic([['histx', '.'], | ||
| 77 | + ['scatter', 'histy']], | ||
| 78 | + figsize=(6, 6), | ||
| 79 | + width_ratios=(4, 1), height_ratios=(1, 4), | ||
| 80 | + layout='constrained') | ||
| 81 | + scatter_hist(x, y, axs['scatter'], axs['histx'], axs['histy']) | ||
| 79 | 82 | ||
| 80 | 83 | ||
| 81 | 84 | # %% | |
| Back | FazBrowse Home | New Git URL |
0 commit comments