.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/pitch_plots/plot_twitter_powerpoint.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_pitch_plots_plot_twitter_powerpoint.py: =============================== Twitter and Powerpoint friendly =============================== In these examples, we aim to size the pitches so that they are not cropped by Twitter and fit exactly in a Powerpoint slide (16:9 aspect ratio). I am not sure if this is good or bad for increasing your social media reach. It could increase likes/retweets by making the media more accessible. On the other hand the algorithms might pick up engagement from people clicking to enlarge cropped photos. For Twitter, the following aspect ratios prevent images getting cropped (width then height): * A single image: ``16 by 9`` * Two images: ``7 by 8`` * One ``7 by 8`` and two ``7 by 4`` * Four images: ``7 by 4`` .. GENERATED FROM PYTHON SOURCE LINES 25-29 .. code-block:: Python import matplotlib.pyplot as plt from mplsoccer import Pitch, VerticalPitch .. GENERATED FROM PYTHON SOURCE LINES 30-35 16 by 9 horizontal ------------------ I created a function to calculate the maximum dimensions you can get away with while having a set figure size. Let's use this to create the largest pitch possible with a 16:9 figure aspect ratio. .. GENERATED FROM PYTHON SOURCE LINES 35-57 .. code-block:: Python FIGWIDTH = 16 FIGHEIGHT = 9 NROWS = 1 NCOLS = 1 # here we want the maximum side in proportion to the figure dimensions # (height in this case) to take up all of the image MAX_GRID = 1 # pitch with minimal padding (2 each side) pitch = Pitch(pad_top=2, pad_bottom=2, pad_left=2, pad_right=2, pitch_color='#22312b') # calculate the maximum grid_height/ width GRID_WIDTH, GRID_HEIGHT = pitch.grid_dimensions(figwidth=FIGWIDTH, figheight=FIGHEIGHT, nrows=NROWS, ncols=NCOLS, max_grid=MAX_GRID, space=0) # plot using the mplsoccer grid function fig, ax = pitch.grid(figheight=FIGHEIGHT, grid_width=GRID_WIDTH, grid_height=GRID_HEIGHT, title_height=0, endnote_height=0) fig.set_facecolor('#22312b') .. image-sg:: /gallery/pitch_plots/images/sphx_glr_plot_twitter_powerpoint_001.png :alt: plot twitter powerpoint :srcset: /gallery/pitch_plots/images/sphx_glr_plot_twitter_powerpoint_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 58-63 16 by 9 horizontal grass ------------------------ Now let's get the largest pitch possible for a 16:9 figure but with grassy stripes. See `Caley Graphics `_ for some inspiration on how you might add titles on the pitch. .. GENERATED FROM PYTHON SOURCE LINES 63-88 .. code-block:: Python FIGWIDTH = 16 FIGHEIGHT = 9 NROWS = 1 NCOLS = 1 MAX_GRID = 1 # here we setup the padding to get a 16:9 aspect ratio for the axis # note 80 is the StatsBomb width and 120 is the StatsBomb length # this will extend the (axis) grassy effect to the figure edges PAD_TOP = 2 PAD_BOTTOM = 2 PAD_SIDES = (((80 + PAD_BOTTOM + PAD_TOP) * FIGWIDTH / FIGHEIGHT) - 120) / 2 pitch = Pitch(pad_top=PAD_TOP, pad_bottom=PAD_BOTTOM, pad_left=PAD_SIDES, pad_right=PAD_SIDES, pitch_color='grass', stripe=True, line_color='white') # calculate the maximum grid_height/ width GRID_WIDTH, GRID_HEIGHT = pitch.grid_dimensions(figwidth=FIGWIDTH, figheight=FIGHEIGHT, nrows=NROWS, ncols=NCOLS, max_grid=1, space=0) # plot fig, ax = pitch.grid(figheight=FIGHEIGHT, grid_width=GRID_WIDTH, grid_height=GRID_HEIGHT, title_height=0, endnote_height=0) .. image-sg:: /gallery/pitch_plots/images/sphx_glr_plot_twitter_powerpoint_002.png :alt: plot twitter powerpoint :srcset: /gallery/pitch_plots/images/sphx_glr_plot_twitter_powerpoint_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 89-93 16 by 9: three vertical pitches ------------------------------- Three vertical pitches fits nicely in the 16:9 aspect ratio. Here we plot with a title and endnote axis too. .. GENERATED FROM PYTHON SOURCE LINES 93-116 .. code-block:: Python FIGWIDTH = 16 FIGHEIGHT = 9 NROWS = 1 NCOLS = 3 SPACE = 0.09 MAX_GRID = 0.95 pitch = VerticalPitch(pad_top=1, pad_bottom=1, pad_left=1, pad_right=1, pitch_color='grass', stripe=True, line_color='white') GRID_WIDTH, GRID_HEIGHT = pitch.grid_dimensions(figwidth=FIGWIDTH, figheight=FIGHEIGHT, nrows=NROWS, ncols=NCOLS, max_grid=MAX_GRID, space=SPACE) TITLE_HEIGHT = 0.1 ENDNOTE_HEIGHT = MAX_GRID - (GRID_HEIGHT + TITLE_HEIGHT) fig, ax = pitch.grid(figheight=FIGHEIGHT, grid_width=GRID_WIDTH, grid_height=GRID_HEIGHT, space=SPACE, ncols=NCOLS, nrows=NROWS, title_height=TITLE_HEIGHT, endnote_height=ENDNOTE_HEIGHT, axis=True) .. image-sg:: /gallery/pitch_plots/images/sphx_glr_plot_twitter_powerpoint_003.png :alt: plot twitter powerpoint :srcset: /gallery/pitch_plots/images/sphx_glr_plot_twitter_powerpoint_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 117-121 16 by 9: 2 cropped half-pitches ------------------------------- Here we plot two half pitches side-by-side that are cropped so 15 units are taken off each side. This is how I would do game xG comparisons. .. GENERATED FROM PYTHON SOURCE LINES 121-143 .. code-block:: Python FIGWIDTH = 16 FIGHEIGHT = 9 NROWS = 1 NCOLS = 2 SPACE = 0.05 MAX_GRID = 0.95 pitch = VerticalPitch(pad_top=3, pad_bottom=-15, pad_left=-15, pad_right=-15, linewidth=1, half=True, pitch_color='grass', stripe=True, line_color='white') GRID_WIDTH, GRID_HEIGHT = pitch.grid_dimensions(figwidth=FIGWIDTH, figheight=FIGHEIGHT, nrows=NROWS, ncols=NCOLS, max_grid=MAX_GRID, space=SPACE) TITLE_HEIGHT = 0.08 ENDNOTE_HEIGHT = 0.04 fig, ax = pitch.grid(figheight=FIGHEIGHT, grid_width=GRID_WIDTH, grid_height=GRID_HEIGHT, space=SPACE, ncols=NCOLS, nrows=NROWS, title_height=TITLE_HEIGHT, endnote_height=ENDNOTE_HEIGHT, axis=True) .. image-sg:: /gallery/pitch_plots/images/sphx_glr_plot_twitter_powerpoint_004.png :alt: plot twitter powerpoint :srcset: /gallery/pitch_plots/images/sphx_glr_plot_twitter_powerpoint_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 144-147 16 by 9: team of pitches ------------------------ Here we plot 15 pitches (11 players + 3 subs + 1 pitch for the whole team). .. GENERATED FROM PYTHON SOURCE LINES 147-171 .. code-block:: Python FIGWIDTH = 16 FIGHEIGHT = 9 NROWS = 3 NCOLS = 5 SPACE = 0.1 MAX_GRID = 0.98 pitch = Pitch(pad_top=1, pad_bottom=1, pad_left=1, pad_right=1, linewidth=1, pitch_color='grass', stripe=True, line_color='white') GRID_WIDTH, GRID_HEIGHT = pitch.grid_dimensions(figwidth=FIGWIDTH, figheight=FIGHEIGHT, nrows=NROWS, ncols=NCOLS, max_grid=MAX_GRID, space=SPACE) TITLE_HEIGHT = 0.15 ENDNOTE_HEIGHT = 0.05 fig, ax = pitch.grid(figheight=FIGHEIGHT, grid_width=GRID_WIDTH, grid_height=GRID_HEIGHT, space=SPACE, ncols=NCOLS, nrows=NROWS, title_height=TITLE_HEIGHT, endnote_height=ENDNOTE_HEIGHT, axis=True) .. image-sg:: /gallery/pitch_plots/images/sphx_glr_plot_twitter_powerpoint_005.png :alt: plot twitter powerpoint :srcset: /gallery/pitch_plots/images/sphx_glr_plot_twitter_powerpoint_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 172-177 7 by 8 ------ Most of the Twitter aspect ratios are around 1.5 - 1.8 with the exception of the 7 by 8 aspect ratio. This isn't a great aspect ratio for pitches, but seems to work okay for one vertical pitch (with a bit of extra space either side). .. GENERATED FROM PYTHON SOURCE LINES 177-207 .. code-block:: Python FIGWIDTH = 7 FIGHEIGHT = 8 NROWS = 1 NCOLS = 1 SPACE = 0 MAX_GRID = 1 # here we setup the padding to get a 16:9 aspect ratio for the axis # note 80 is the StatsBomb width and 120 is the StatsBomb length # this will extend the (axis) grassy effect to the figure edges PAD_TOP = 2 PAD_BOTTOM = 2 PAD_SIDES = ((120 + PAD_TOP + PAD_BOTTOM) * FIGWIDTH / FIGHEIGHT - 80) / 2 pitch = VerticalPitch(pad_top=PAD_TOP, pad_bottom=PAD_BOTTOM, pad_left=PAD_SIDES, pad_right=PAD_SIDES, pitch_color='grass', stripe=True, line_color='white') GRID_WIDTH, GRID_HEIGHT = pitch.grid_dimensions(figwidth=FIGWIDTH, figheight=FIGHEIGHT, nrows=NROWS, ncols=NCOLS, max_grid=MAX_GRID, space=SPACE) TITLE_HEIGHT = 0 ENDNOTE_HEIGHT = 0 fig, ax = pitch.grid(figheight=FIGHEIGHT, grid_width=GRID_WIDTH, grid_height=GRID_HEIGHT, space=SPACE, ncols=NCOLS, nrows=NROWS, title_height=TITLE_HEIGHT, endnote_height=ENDNOTE_HEIGHT, axis=True) plt.show() # If you are using a Jupyter notebook you do not need this line .. image-sg:: /gallery/pitch_plots/images/sphx_glr_plot_twitter_powerpoint_006.png :alt: plot twitter powerpoint :srcset: /gallery/pitch_plots/images/sphx_glr_plot_twitter_powerpoint_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.301 seconds) .. _sphx_glr_download_gallery_pitch_plots_plot_twitter_powerpoint.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_twitter_powerpoint.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_twitter_powerpoint.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_twitter_powerpoint.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_