{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Matplotlib logo\n\nThis example generates the current matplotlib logo.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.cm as cm\nimport matplotlib.font_manager\nfrom matplotlib.patches import PathPatch, Rectangle\nfrom matplotlib.text import TextPath\nimport matplotlib.transforms as mtrans\n\nMPL_BLUE = '#11557c'\n\n\ndef get_font_properties():\n # The original font is Calibri, if that is not installed, we fall back\n # to Carlito, which is metrically equivalent.\n if 'Calibri' in matplotlib.font_manager.findfont('Calibri:bold'):\n return matplotlib.font_manager.FontProperties(family='Calibri',\n weight='bold')\n if 'Carlito' in matplotlib.font_manager.findfont('Carlito:bold'):\n print('Original font not found. Falling back to Carlito. '\n 'The logo text will not be in the correct font.')\n return matplotlib.font_manager.FontProperties(family='Carlito',\n weight='bold')\n print('Original font not found. '\n 'The logo text will not be in the correct font.')\n return None\n\n\ndef create_icon_axes(fig, ax_position, lw_bars, lw_grid, lw_border, rgrid):\n \"\"\"\n Create a polar Axes containing the matplotlib radar plot.\n\n Parameters\n ----------\n fig : matplotlib.figure.Figure\n The figure to draw into.\n ax_position : (float, float, float, float)\n The position of the created Axes in figure coordinates as\n (x, y, width, height).\n lw_bars : float\n The linewidth of the bars.\n lw_grid : float\n The linewidth of the grid.\n lw_border : float\n The linewidth of the Axes border.\n rgrid : array-like\n Positions of the radial grid.\n\n Returns\n -------\n ax : matplotlib.axes.Axes\n The created Axes.\n \"\"\"\n with plt.rc_context({'axes.edgecolor': MPL_BLUE,\n 'axes.linewidth': lw_border}):\n ax = fig.add_axes(ax_position, projection='polar')\n ax.set_axisbelow(True)\n\n N = 7\n arc = 2. * np.pi\n theta = np.arange(0.0, arc, arc / N)\n radii = np.array([2, 6, 8, 7, 4, 5, 8])\n width = np.pi / 4 * np.array([0.4, 0.4, 0.6, 0.8, 0.2, 0.5, 0.3])\n bars = ax.bar(theta, radii, width=width, bottom=0.0, align='edge',\n edgecolor='0.3', lw=lw_bars)\n for r, bar in zip(radii, bars):\n color = *cm.jet(r / 10.)[:3], 0.6 # color from jet with alpha=0.6\n bar.set_facecolor(color)\n\n ax.tick_params(labelbottom=False, labeltop=False,\n labelleft=False, labelright=False)\n\n ax.grid(lw=lw_grid, color='0.9')\n ax.set_rmax(9)\n ax.set_yticks(rgrid)\n\n # the actual visible background - extends a bit beyond the axis\n ax.add_patch(Rectangle((0, 0), arc, 9.58,\n facecolor='white', zorder=0,\n clip_on=False, in_layout=False))\n return ax\n\n\ndef create_text_axes(fig, height_px):\n \"\"\"Create an Axes in *fig* that contains 'matplotlib' as Text.\"\"\"\n ax = fig.add_axes((0, 0, 1, 1))\n ax.set_aspect(\"equal\")\n ax.set_axis_off()\n\n path = TextPath((0, 0), \"matplotlib\", size=height_px * 0.8,\n prop=get_font_properties())\n\n angle = 4.25 # degrees\n trans = mtrans.Affine2D().skew_deg(angle, 0)\n\n patch = PathPatch(path, transform=trans + ax.transData, color=MPL_BLUE,\n lw=0)\n ax.add_patch(patch)\n ax.autoscale()\n\n\ndef make_logo(height_px, lw_bars, lw_grid, lw_border, rgrid, with_text=False):\n \"\"\"\n Create a full figure with the Matplotlib logo.\n\n Parameters\n ----------\n height_px : int\n Height of the figure in pixel.\n lw_bars : float\n The linewidth of the bar border.\n lw_grid : float\n The linewidth of the grid.\n lw_border : float\n The linewidth of icon border.\n rgrid : sequence of float\n The radial grid positions.\n with_text : bool\n Whether to draw only the icon or to include 'matplotlib' as text.\n \"\"\"\n dpi = 100\n height = height_px / dpi\n figsize = (5 * height, height) if with_text else (height, height)\n fig = plt.figure(figsize=figsize, dpi=dpi)\n fig.patch.set_alpha(0)\n\n if with_text:\n create_text_axes(fig, height_px)\n ax_pos = (0.535, 0.12, .17, 0.75) if with_text else (0.03, 0.03, .94, .94)\n ax = create_icon_axes(fig, ax_pos, lw_bars, lw_grid, lw_border, rgrid)\n\n return fig, ax" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A large logo:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "make_logo(height_px=110, lw_bars=0.7, lw_grid=0.5, lw_border=1,\n rgrid=[1, 3, 5, 7])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A small 32px logo:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "make_logo(height_px=32, lw_bars=0.3, lw_grid=0.3, lw_border=0.3, rgrid=[5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A large logo including text, as used on the matplotlib website.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "make_logo(height_px=110, lw_bars=0.7, lw_grid=0.5, lw_border=1,\n rgrid=[1, 3, 5, 7], with_text=True)\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }