From 9cc92f0d29ef779d6718d97ee6c1d5f77cf0a854 Mon Sep 17 00:00:00 2001 From: Henry Webel Date: Thu, 20 Nov 2025 09:25:42 +0100 Subject: [PATCH] :art: one solution for figure2 --- exercises/CRISPRi/figure_2.ipynb | 151 ++++++++++++++++++------------- exercises/CRISPRi/figure_2.py | 151 ++++++++++++++++++------------- 2 files changed, 180 insertions(+), 122 deletions(-) diff --git a/exercises/CRISPRi/figure_2.ipynb b/exercises/CRISPRi/figure_2.ipynb index 151be67..b8ba16f 100644 --- a/exercises/CRISPRi/figure_2.ipynb +++ b/exercises/CRISPRi/figure_2.ipynb @@ -33,6 +33,11 @@ "mpl.rcParams['pdf.fonttype'] = 42\n", "mpl.rcParams['ps.fonttype'] = 42\n", "\n", + "# global font size (can be set via environment variable FONT_SIZE)\n", + "FONT_SIZE = 9\n", + "mpl.rcParams['font.size'] = FONT_SIZE\n", + "mpl.rcParams[\"figure.figsize\"] = (3.5, 3)\n", + "\n", "IN_COLAB = \"COLAB_GPU\" in os.environ" ] }, @@ -111,7 +116,7 @@ }, "outputs": [], "source": [ - "fig, ax = plt.subplots(figsize=(8, 5))\n", + "fig, ax = plt.subplots()\n", "\n", "# main plot\n", "p = sns.lineplot(\n", @@ -133,9 +138,9 @@ "ax.set_ylim(0, 5)\n", "\n", "# define frequency and size of ticks and their labels\n", - "ax.set_xticks(range(5, 31, 5), range(5, 31, 5), fontsize=15)\n", + "ax.set_xticks(range(5, 31, 5), range(5, 31, 5), fontsize=FONT_SIZE)\n", "ax.xaxis.set_tick_params(width=2, length=10)\n", - "ax.set_yticks(range(1, 6, 1), [1, 2, 3, 4, \"\"], fontsize=15)\n", + "ax.set_yticks(range(1, 6, 1), [1, 2, 3, 4, \"\"], fontsize=FONT_SIZE)\n", "ax.yaxis.set_tick_params(width=2, length=10)\n", "\n", "# adjust the \"frame\" of the plot\n", @@ -151,7 +156,7 @@ " loc=\"upper left\",\n", " bbox_to_anchor=[0.01, 1.2],\n", " frameon=False,\n", - " fontsize=15,\n", + " fontsize=FONT_SIZE,\n", ")\n", "\n", "# make an annotation with arrow to point to timepoint at 1 hour\n", @@ -159,7 +164,7 @@ " \"1\",\n", " (1, 0.5),\n", " xytext=(1, 1.5),\n", - " fontsize=12,\n", + " fontsize=FONT_SIZE,\n", " xycoords=\"data\",\n", " horizontalalignment=\"center\",\n", " verticalalignment=\"center\",\n", @@ -172,8 +177,8 @@ ")\n", "\n", "# set the axis labels\n", - "ax.set_xlabel(\"Time (h)\", labelpad=10, fontdict={\"size\": 15})\n", - "ax.set_ylabel(\"$\\\\text{OD}_{600}$\", labelpad=20, fontdict={\"size\": 15})" + "ax.set_xlabel(\"Time (h)\", labelpad=10, fontdict={\"size\": FONT_SIZE})\n", + "ax.set_ylabel(\"$\\\\text{OD}_{600}$\", labelpad=20, fontdict={\"size\": FONT_SIZE})" ] }, { @@ -230,7 +235,7 @@ }, "outputs": [], "source": [ - "fig, ax = plt.subplots(figsize=(8, 5))\n", + "fig, ax = plt.subplots()\n", "\n", "TOTAL_WIDTH = 0.8\n", "bp = sns.barplot(\n", @@ -261,8 +266,8 @@ "ax.set_ylim(0, 3.2)\n", "\n", "# define frequency and size of ticks and their labels\n", - "ax.xaxis.set_tick_params(width=2, length=10, labelsize=15)\n", - "ax.set_yticks(range(1, 4, 1), [1, 2, \"\"], fontsize=15)\n", + "ax.xaxis.set_tick_params(width=2, length=10, labelsize=FONT_SIZE)\n", + "ax.set_yticks(range(1, 4, 1), [1, 2, \"\"], fontsize=FONT_SIZE)\n", "ax.yaxis.set_tick_params(width=2, length=10)\n", "\n", "# adjust the \"frame\" of the plot\n", @@ -281,15 +286,15 @@ " loc=\"upper left\",\n", " bbox_to_anchor=[0.01, 1.2],\n", " frameon=False,\n", - " fontsize=15,\n", + " fontsize=FONT_SIZE,\n", ")\n", "\n", "# set the axis labels\n", - "ax.set_xlabel(\"Time Interval (h)\", labelpad=10, fontdict={\"size\": 15})\n", + "ax.set_xlabel(\"Time Interval (h)\", labelpad=10, fontdict={\"size\": FONT_SIZE})\n", "ax.set_ylabel(\n", " \"Yield $\\\\left( \\\\text{mol } \\\\text{mol}^{-1} \\\\right)$\",\n", " labelpad=20,\n", - " fontdict={\"size\": 15},\n", + " fontdict={\"size\": FONT_SIZE},\n", ")\n", "\n", "# indicate statistical significances\n", @@ -310,7 +315,7 @@ " \"*\",\n", " horizontalalignment=\"center\",\n", " verticalalignment=\"bottom\",\n", - " fontsize=15,\n", + " fontsize=FONT_SIZE,\n", " )" ] }, @@ -368,7 +373,7 @@ }, "outputs": [], "source": [ - "fig, ax = plt.subplots(figsize=(8, 5))\n", + "fig, ax = plt.subplots()\n", "\n", "# main plot\n", "p = sns.lineplot(\n", @@ -422,10 +427,10 @@ "ax.set_ylim(0, 0.8)\n", "\n", "# define frequency and size of ticks and their labels\n", - "ax.set_xticks(range(5, 26, 5), list(range(5, 26, 5))[:-1] + [\"\"], fontsize=15)\n", + "ax.set_xticks(range(5, 26, 5), list(range(5, 26, 5))[:-1] + [\"\"], fontsize=FONT_SIZE)\n", "ax.xaxis.set_tick_params(width=2, length=10)\n", "y_tick_labels = np.round(np.linspace(0.1, 0.8, 8), 1)\n", - "ax.set_yticks(y_tick_labels, y_tick_labels.tolist()[:-1] + [\"\"], fontsize=15)\n", + "ax.set_yticks(y_tick_labels, y_tick_labels.tolist()[:-1] + [\"\"], fontsize=FONT_SIZE)\n", "ax.yaxis.set_tick_params(width=2, length=10)\n", "\n", "# adjust the \"frame\" of the plot\n", @@ -441,12 +446,12 @@ " loc=\"upper left\",\n", " bbox_to_anchor=[0.01, 1.2],\n", " frameon=False,\n", - " fontsize=15,\n", + " fontsize=FONT_SIZE,\n", ")\n", "\n", "# set the axis labels\n", - "ax.set_xlabel(\"Time (h)\", labelpad=10, fontdict={\"size\": 15})\n", - "ax.set_ylabel(\"$\\\\text{OD}_{600}$\", labelpad=20, fontdict={\"size\": 15})" + "ax.set_xlabel(\"Time (h)\", labelpad=10, fontdict={\"size\": FONT_SIZE})\n", + "ax.set_ylabel(\"$\\\\text{OD}_{600}$\", labelpad=20, fontdict={\"size\": FONT_SIZE})" ] }, { @@ -498,7 +503,7 @@ }, "outputs": [], "source": [ - "fig, ax1 = plt.subplots(figsize=(8, 5))\n", + "fig, ax1 = plt.subplots()\n", "\n", "# first of variables with millimolar unit\n", "p1 = sns.lineplot(\n", @@ -540,11 +545,11 @@ "ax2.set_ylim(0, 130)\n", "\n", "# define frequency and size of ticks and their labels\n", - "ax1.set_xticks(range(5, 25, 5), range(5, 25, 5), fontsize=15)\n", + "ax1.set_xticks(range(5, 25, 5), range(5, 25, 5), fontsize=FONT_SIZE)\n", "ax1.xaxis.set_tick_params(width=2, length=10)\n", - "ax1.set_yticks(range(1, 10, 1), list(range(1, 10, 1))[:-1] + [\"\"], fontsize=15)\n", + "ax1.set_yticks(range(1, 10, 1), list(range(1, 10, 1))[:-1] + [\"\"], fontsize=FONT_SIZE)\n", "ax1.yaxis.set_tick_params(width=2, length=10)\n", - "ax2.set_yticks(range(20, 141, 20), list(range(20, 141, 20))[:-1] + [\"\"], fontsize=15)\n", + "ax2.set_yticks(range(20, 141, 20), list(range(20, 141, 20))[:-1] + [\"\"], fontsize=FONT_SIZE)\n", "ax2.yaxis.set_tick_params(width=2, length=10)\n", "\n", "# adjust the \"frame\" of the plot\n", @@ -564,7 +569,7 @@ " loc=\"upper left\",\n", " bbox_to_anchor=[0.01, 1.2],\n", " frameon=False,\n", - " fontsize=15,\n", + " fontsize=FONT_SIZE,\n", ")\n", "\n", "# make an annotation with arrow to point to timepoint at 1 hour\n", @@ -572,7 +577,7 @@ " \"2\",\n", " (2, 10),\n", " xytext=(2, 30),\n", - " fontsize=12,\n", + " fontsize=FONT_SIZE,\n", " xycoords=\"data\",\n", " horizontalalignment=\"center\",\n", " verticalalignment=\"center\",\n", @@ -585,9 +590,9 @@ ")\n", "\n", "# set the axis labels\n", - "ax1.set_xlabel(\"Time (h)\", labelpad=10, fontdict={\"size\": 15})\n", - "ax2.set_ylabel(\"Concentration (mM)\", labelpad=20, fontdict={\"size\": 15})\n", - "ax1.set_ylabel(\"$\\\\text{OD}_{600}$\", labelpad=20, fontdict={\"size\": 15})" + "ax1.set_xlabel(\"Time (h)\", labelpad=10, fontdict={\"size\": FONT_SIZE})\n", + "ax2.set_ylabel(\"Concentration (mM)\", labelpad=20, fontdict={\"size\": FONT_SIZE})\n", + "ax1.set_ylabel(\"$\\\\text{OD}_{600}$\", labelpad=20, fontdict={\"size\": FONT_SIZE})" ] }, { @@ -636,9 +641,9 @@ " ax.set_ylim(0, 5)\n", "\n", " # define frequency and size of ticks and their labels\n", - " ax.set_xticks(range(5, 31, 5), range(5, 31, 5), fontsize=15)\n", + " ax.set_xticks(range(5, 31, 5), range(5, 31, 5), fontsize=FONT_SIZE)\n", " ax.xaxis.set_tick_params(width=2, length=10)\n", - " ax.set_yticks(range(1, 6, 1), [1, 2, 3, 4, \"\"], fontsize=15)\n", + " ax.set_yticks(range(1, 6, 1), [1, 2, 3, 4, \"\"], fontsize=FONT_SIZE)\n", " ax.yaxis.set_tick_params(width=2, length=10)\n", "\n", " # adjust the \"frame\" of the plot\n", @@ -654,7 +659,7 @@ " loc=\"upper left\",\n", " bbox_to_anchor=[0.01, 1.2],\n", " frameon=False,\n", - " fontsize=15,\n", + " fontsize=FONT_SIZE,\n", " )\n", "\n", " # make an annotation with arrow to point to timepoint at 1 hour\n", @@ -662,7 +667,7 @@ " \"1\",\n", " (1, 0.5),\n", " xytext=(1, 1.5),\n", - " fontsize=12,\n", + " fontsize=FONT_SIZE,\n", " xycoords=\"data\",\n", " horizontalalignment=\"center\",\n", " verticalalignment=\"center\",\n", @@ -675,8 +680,8 @@ " )\n", "\n", " # set the axis labels\n", - " ax.set_xlabel(\"Time (h)\", labelpad=10, fontdict={\"size\": 15})\n", - " ax.set_ylabel(\"$\\\\text{OD}_{600}$\", labelpad=20, fontdict={\"size\": 15})" + " ax.set_xlabel(\"Time (h)\", labelpad=10, fontdict={\"size\": FONT_SIZE})\n", + " ax.set_ylabel(\"$\\\\text{OD}_{600}$\", labelpad=20, fontdict={\"size\": FONT_SIZE})" ] }, { @@ -715,8 +720,8 @@ " ax.set_ylim(0, 3.2)\n", "\n", " # define frequency and size of ticks and their labels\n", - " ax.xaxis.set_tick_params(width=2, length=10, labelsize=15)\n", - " ax.set_yticks(range(1, 4, 1), [1, 2, \"\"], fontsize=15)\n", + " ax.xaxis.set_tick_params(width=2, length=10, labelsize=FONT_SIZE)\n", + " ax.set_yticks(range(1, 4, 1), [1, 2, \"\"], fontsize=FONT_SIZE)\n", " ax.yaxis.set_tick_params(width=2, length=10)\n", "\n", " # adjust the \"frame\" of the plot\n", @@ -735,15 +740,15 @@ " loc=\"upper left\",\n", " bbox_to_anchor=[0.01, 1.2],\n", " frameon=False,\n", - " fontsize=15,\n", + " fontsize=FONT_SIZE,\n", " )\n", "\n", " # set the axis labels\n", - " ax.set_xlabel(\"Time Interval (h)\", labelpad=10, fontdict={\"size\": 15})\n", + " ax.set_xlabel(\"Time Interval (h)\", labelpad=10, fontdict={\"size\": FONT_SIZE})\n", " ax.set_ylabel(\n", " \"Yield $\\\\left( \\\\text{mol } \\\\text{mol}^{-1} \\\\right)$\",\n", " labelpad=20,\n", - " fontdict={\"size\": 15},\n", + " fontdict={\"size\": FONT_SIZE},\n", " )\n", "\n", " # indicate statistical significances\n", @@ -764,7 +769,7 @@ " \"*\",\n", " horizontalalignment=\"center\",\n", " verticalalignment=\"bottom\",\n", - " fontsize=15,\n", + " fontsize=FONT_SIZE,\n", " )" ] }, @@ -827,10 +832,10 @@ " ax.set_ylim(0, 0.8)\n", "\n", " # define frequency and size of ticks and their labels\n", - " ax.set_xticks(range(5, 26, 5), list(range(5, 26, 5))[:-1] + [\"\"], fontsize=15)\n", + " ax.set_xticks(range(5, 26, 5), list(range(5, 26, 5))[:-1] + [\"\"], fontsize=FONT_SIZE)\n", " ax.xaxis.set_tick_params(width=2, length=10)\n", " y_tick_labels = np.round(np.linspace(0.1, 0.8, 8), 1)\n", - " ax.set_yticks(y_tick_labels, y_tick_labels.tolist()[:-1] + [\"\"], fontsize=15)\n", + " ax.set_yticks(y_tick_labels, y_tick_labels.tolist()[:-1] + [\"\"], fontsize=FONT_SIZE)\n", " ax.yaxis.set_tick_params(width=2, length=10)\n", "\n", " # adjust the \"frame\" of the plot\n", @@ -846,12 +851,12 @@ " loc=\"upper left\",\n", " bbox_to_anchor=[0.01, 1.2],\n", " frameon=False,\n", - " fontsize=15,\n", + " fontsize=FONT_SIZE,\n", " )\n", "\n", " # set the axis labels\n", - " ax.set_xlabel(\"Time (h)\", labelpad=10, fontdict={\"size\": 15})\n", - " ax.set_ylabel(\"$\\\\text{OD}_{600}$\", labelpad=20, fontdict={\"size\": 15})" + " ax.set_xlabel(\"Time (h)\", labelpad=10, fontdict={\"size\": FONT_SIZE})\n", + " ax.set_ylabel(\"$\\\\text{OD}_{600}$\", labelpad=20, fontdict={\"size\": FONT_SIZE})" ] }, { @@ -901,12 +906,12 @@ " ax2.set_ylim(0, 130)\n", "\n", " # define frequency and size of ticks and their labels\n", - " ax.set_xticks(range(5, 25, 5), range(5, 25, 5), fontsize=15)\n", + " ax.set_xticks(range(5, 25, 5), range(5, 25, 5), fontsize=FONT_SIZE)\n", " ax.xaxis.set_tick_params(width=2, length=10)\n", - " ax.set_yticks(range(1, 10, 1), list(range(1, 10, 1))[:-1] + [\"\"], fontsize=15)\n", + " ax.set_yticks(range(1, 10, 1), list(range(1, 10, 1))[:-1] + [\"\"], fontsize=FONT_SIZE)\n", " ax.yaxis.set_tick_params(width=2, length=10)\n", " ax2.set_yticks(\n", - " range(20, 141, 20), list(range(20, 141, 20))[:-1] + [\"\"], fontsize=15\n", + " range(20, 141, 20), list(range(20, 141, 20))[:-1] + [\"\"], fontsize=FONT_SIZE\n", " )\n", " ax2.yaxis.set_tick_params(width=2, length=10)\n", "\n", @@ -916,18 +921,18 @@ " ax2.spines[pos].set_linewidth(lw)\n", "\n", " # set the legend\n", - " _handles1, _ = p1.get_legend_handles_labels()\n", - " _handles2, _ = p2.get_legend_handles_labels()\n", + " handles1, labels1 = p1.get_legend_handles_labels()\n", + " handles2, labels2 = p2.get_legend_handles_labels()\n", " ax2.legend_.remove()\n", " ax.legend(\n", - " _handles2 + [_handles1[-1]],\n", + " handles2 + [handles1[-1]],\n", " [\"Acetate\", \"IBA\", \"Glucose\", \"$\\\\text{OD}_{600}$\"],\n", - " ncol=4,\n", + " ncol=2,\n", " title=None,\n", " loc=\"upper left\",\n", " bbox_to_anchor=[0.01, 1.2],\n", " frameon=False,\n", - " fontsize=8,\n", + " fontsize=FONT_SIZE,\n", " )\n", "\n", " # make an annotation with arrow to point to timepoint at 1 hour\n", @@ -935,7 +940,7 @@ " \"2\",\n", " (2, 10),\n", " xytext=(2, 30),\n", - " fontsize=12,\n", + " fontsize=FONT_SIZE,\n", " xycoords=\"data\",\n", " horizontalalignment=\"center\",\n", " verticalalignment=\"center\",\n", @@ -948,9 +953,9 @@ " )\n", "\n", " # set the axis labels\n", - " ax.set_xlabel(\"Time (h)\", labelpad=10, fontdict={\"size\": 15})\n", - " ax2.set_ylabel(\"Concentration (mM)\", labelpad=20, fontdict={\"size\": 15})\n", - " ax.set_ylabel(\"$\\\\text{OD}_{600}$\", labelpad=20, fontdict={\"size\": 15})" + " ax.set_xlabel(\"Time (h)\", labelpad=10, fontdict={\"size\": FONT_SIZE})\n", + " ax2.set_ylabel(\"Concentration (mM)\", labelpad=20, fontdict={\"size\": FONT_SIZE})\n", + " ax.set_ylabel(\"$\\\\text{OD}_{600}$\", labelpad=20, fontdict={\"size\": FONT_SIZE})" ] }, { @@ -971,8 +976,7 @@ "source": [ "fig = plt.figure(\n", " figsize=(7.2, 9.7),\n", - " # layout=\"constrained\",\n", - " # dpi=300,\n", + " constrained_layout=False,\n", ")\n", "\n", "gs = fig.add_gridspec(2, 2)\n", @@ -986,7 +990,7 @@ "):\n", " ax = fig.add_subplot(gs[i])\n", " plot_fun(_df, ax)\n", - " ax.set_title(label, fontsize=20, x=-0.05, y=1.1)\n", + " ax.set_title(label, fontsize=FONT_SIZE, x=-0.05, y=1.1)\n", "\n", "plt.subplots_adjust(hspace=0.5)\n", "plt.show()" @@ -1004,6 +1008,31 @@ "Click to see some hints\n", "1. Adjust the individual plots to roughly the correct size\n", "2. Adapt the default font sizes for axes labels, ticks, legends, and titles\n", + "\n", + "You might want to look into using relative size w.r.t to the default font size, e.g.,\n", + "```python\n", + "FONT_SIZE = 9\n", + "mpl.rcParams['font.size'] = FONT_SIZE\n", + "mpl.rcParams['fig.labelsize'] = FONT_SIZE\n", + "mpl.font_manager.font_scalings\n", + "```\n", + "which gives you the following scaling factors:\n", + "```python\n", + "{\n", + " 'xx-small': 0.579,\n", + " 'x-small': 0.694,\n", + " 'small': 0.833,\n", + " 'medium': 1.0,\n", + " 'large': 1.2,\n", + " 'x-large': 1.44,\n", + " 'xx-large': 1.728,\n", + " None: 1.0\n", + "}\n", + "```\n", + "meaning that setting to `fontsize=None` or `fontsize='medium'` result in using `FONT_SIZE`, i.e. 9 points.\n", + "If you prefer exact point sizes in integer, you can also do this manually, e.g. using\n", + "`fontsize=FONT_SIZE - 1` for tick labels, `fontsize=FONT_SIZE + 1` for axis labels, \n", + "and `fontsize=FONT_SIZE + 5` for titles (you need to play with it).\n", "" ] }, diff --git a/exercises/CRISPRi/figure_2.py b/exercises/CRISPRi/figure_2.py index 65f6829..bca9a66 100644 --- a/exercises/CRISPRi/figure_2.py +++ b/exercises/CRISPRi/figure_2.py @@ -34,6 +34,11 @@ mpl.rcParams['pdf.fonttype'] = 42 mpl.rcParams['ps.fonttype'] = 42 +# global font size (can be set via environment variable FONT_SIZE) +FONT_SIZE = 9 +mpl.rcParams['font.size'] = FONT_SIZE +mpl.rcParams["figure.figsize"] = (3.5, 3) + IN_COLAB = "COLAB_GPU" in os.environ @@ -81,7 +86,7 @@ # ### 1.2 Plot the Dat # %% -fig, ax = plt.subplots(figsize=(8, 5)) +fig, ax = plt.subplots() # main plot p = sns.lineplot( @@ -103,9 +108,9 @@ ax.set_ylim(0, 5) # define frequency and size of ticks and their labels -ax.set_xticks(range(5, 31, 5), range(5, 31, 5), fontsize=15) +ax.set_xticks(range(5, 31, 5), range(5, 31, 5), fontsize=FONT_SIZE) ax.xaxis.set_tick_params(width=2, length=10) -ax.set_yticks(range(1, 6, 1), [1, 2, 3, 4, ""], fontsize=15) +ax.set_yticks(range(1, 6, 1), [1, 2, 3, 4, ""], fontsize=FONT_SIZE) ax.yaxis.set_tick_params(width=2, length=10) # adjust the "frame" of the plot @@ -121,7 +126,7 @@ loc="upper left", bbox_to_anchor=[0.01, 1.2], frameon=False, - fontsize=15, + fontsize=FONT_SIZE, ) # make an annotation with arrow to point to timepoint at 1 hour @@ -129,7 +134,7 @@ "1", (1, 0.5), xytext=(1, 1.5), - fontsize=12, + fontsize=FONT_SIZE, xycoords="data", horizontalalignment="center", verticalalignment="center", @@ -142,8 +147,8 @@ ) # set the axis labels -ax.set_xlabel("Time (h)", labelpad=10, fontdict={"size": 15}) -ax.set_ylabel("$\\text{OD}_{600}$", labelpad=20, fontdict={"size": 15}) +ax.set_xlabel("Time (h)", labelpad=10, fontdict={"size": FONT_SIZE}) +ax.set_ylabel("$\\text{OD}_{600}$", labelpad=20, fontdict={"size": FONT_SIZE}) # %% [markdown] @@ -175,7 +180,7 @@ # ### 2.2 Plot The Data # %% -fig, ax = plt.subplots(figsize=(8, 5)) +fig, ax = plt.subplots() TOTAL_WIDTH = 0.8 bp = sns.barplot( @@ -206,8 +211,8 @@ ax.set_ylim(0, 3.2) # define frequency and size of ticks and their labels -ax.xaxis.set_tick_params(width=2, length=10, labelsize=15) -ax.set_yticks(range(1, 4, 1), [1, 2, ""], fontsize=15) +ax.xaxis.set_tick_params(width=2, length=10, labelsize=FONT_SIZE) +ax.set_yticks(range(1, 4, 1), [1, 2, ""], fontsize=FONT_SIZE) ax.yaxis.set_tick_params(width=2, length=10) # adjust the "frame" of the plot @@ -226,15 +231,15 @@ loc="upper left", bbox_to_anchor=[0.01, 1.2], frameon=False, - fontsize=15, + fontsize=FONT_SIZE, ) # set the axis labels -ax.set_xlabel("Time Interval (h)", labelpad=10, fontdict={"size": 15}) +ax.set_xlabel("Time Interval (h)", labelpad=10, fontdict={"size": FONT_SIZE}) ax.set_ylabel( "Yield $\\left( \\text{mol } \\text{mol}^{-1} \\right)$", labelpad=20, - fontdict={"size": 15}, + fontdict={"size": FONT_SIZE}, ) # indicate statistical significances @@ -255,7 +260,7 @@ "*", horizontalalignment="center", verticalalignment="bottom", - fontsize=15, + fontsize=FONT_SIZE, ) @@ -284,7 +289,7 @@ # ### 3.2 Plot The Data # %% -fig, ax = plt.subplots(figsize=(8, 5)) +fig, ax = plt.subplots() # main plot p = sns.lineplot( @@ -338,10 +343,10 @@ ax.set_ylim(0, 0.8) # define frequency and size of ticks and their labels -ax.set_xticks(range(5, 26, 5), list(range(5, 26, 5))[:-1] + [""], fontsize=15) +ax.set_xticks(range(5, 26, 5), list(range(5, 26, 5))[:-1] + [""], fontsize=FONT_SIZE) ax.xaxis.set_tick_params(width=2, length=10) y_tick_labels = np.round(np.linspace(0.1, 0.8, 8), 1) -ax.set_yticks(y_tick_labels, y_tick_labels.tolist()[:-1] + [""], fontsize=15) +ax.set_yticks(y_tick_labels, y_tick_labels.tolist()[:-1] + [""], fontsize=FONT_SIZE) ax.yaxis.set_tick_params(width=2, length=10) # adjust the "frame" of the plot @@ -357,12 +362,12 @@ loc="upper left", bbox_to_anchor=[0.01, 1.2], frameon=False, - fontsize=15, + fontsize=FONT_SIZE, ) # set the axis labels -ax.set_xlabel("Time (h)", labelpad=10, fontdict={"size": 15}) -ax.set_ylabel("$\\text{OD}_{600}$", labelpad=20, fontdict={"size": 15}) +ax.set_xlabel("Time (h)", labelpad=10, fontdict={"size": FONT_SIZE}) +ax.set_ylabel("$\\text{OD}_{600}$", labelpad=20, fontdict={"size": FONT_SIZE}) # %% [markdown] @@ -389,7 +394,7 @@ # ### 4.2 Plot The Data # %% -fig, ax1 = plt.subplots(figsize=(8, 5)) +fig, ax1 = plt.subplots() # first of variables with millimolar unit p1 = sns.lineplot( @@ -431,11 +436,11 @@ ax2.set_ylim(0, 130) # define frequency and size of ticks and their labels -ax1.set_xticks(range(5, 25, 5), range(5, 25, 5), fontsize=15) +ax1.set_xticks(range(5, 25, 5), range(5, 25, 5), fontsize=FONT_SIZE) ax1.xaxis.set_tick_params(width=2, length=10) -ax1.set_yticks(range(1, 10, 1), list(range(1, 10, 1))[:-1] + [""], fontsize=15) +ax1.set_yticks(range(1, 10, 1), list(range(1, 10, 1))[:-1] + [""], fontsize=FONT_SIZE) ax1.yaxis.set_tick_params(width=2, length=10) -ax2.set_yticks(range(20, 141, 20), list(range(20, 141, 20))[:-1] + [""], fontsize=15) +ax2.set_yticks(range(20, 141, 20), list(range(20, 141, 20))[:-1] + [""], fontsize=FONT_SIZE) ax2.yaxis.set_tick_params(width=2, length=10) # adjust the "frame" of the plot @@ -455,7 +460,7 @@ loc="upper left", bbox_to_anchor=[0.01, 1.2], frameon=False, - fontsize=15, + fontsize=FONT_SIZE, ) # make an annotation with arrow to point to timepoint at 1 hour @@ -463,7 +468,7 @@ "2", (2, 10), xytext=(2, 30), - fontsize=12, + fontsize=FONT_SIZE, xycoords="data", horizontalalignment="center", verticalalignment="center", @@ -476,9 +481,9 @@ ) # set the axis labels -ax1.set_xlabel("Time (h)", labelpad=10, fontdict={"size": 15}) -ax2.set_ylabel("Concentration (mM)", labelpad=20, fontdict={"size": 15}) -ax1.set_ylabel("$\\text{OD}_{600}$", labelpad=20, fontdict={"size": 15}) +ax1.set_xlabel("Time (h)", labelpad=10, fontdict={"size": FONT_SIZE}) +ax2.set_ylabel("Concentration (mM)", labelpad=20, fontdict={"size": FONT_SIZE}) +ax1.set_ylabel("$\\text{OD}_{600}$", labelpad=20, fontdict={"size": FONT_SIZE}) # %% [markdown] @@ -513,9 +518,9 @@ def figure_a(a_data: pd.DataFrame, ax: plt.Axes): ax.set_ylim(0, 5) # define frequency and size of ticks and their labels - ax.set_xticks(range(5, 31, 5), range(5, 31, 5), fontsize=15) + ax.set_xticks(range(5, 31, 5), range(5, 31, 5), fontsize=FONT_SIZE) ax.xaxis.set_tick_params(width=2, length=10) - ax.set_yticks(range(1, 6, 1), [1, 2, 3, 4, ""], fontsize=15) + ax.set_yticks(range(1, 6, 1), [1, 2, 3, 4, ""], fontsize=FONT_SIZE) ax.yaxis.set_tick_params(width=2, length=10) # adjust the "frame" of the plot @@ -531,7 +536,7 @@ def figure_a(a_data: pd.DataFrame, ax: plt.Axes): loc="upper left", bbox_to_anchor=[0.01, 1.2], frameon=False, - fontsize=15, + fontsize=FONT_SIZE, ) # make an annotation with arrow to point to timepoint at 1 hour @@ -539,7 +544,7 @@ def figure_a(a_data: pd.DataFrame, ax: plt.Axes): "1", (1, 0.5), xytext=(1, 1.5), - fontsize=12, + fontsize=FONT_SIZE, xycoords="data", horizontalalignment="center", verticalalignment="center", @@ -552,8 +557,8 @@ def figure_a(a_data: pd.DataFrame, ax: plt.Axes): ) # set the axis labels - ax.set_xlabel("Time (h)", labelpad=10, fontdict={"size": 15}) - ax.set_ylabel("$\\text{OD}_{600}$", labelpad=20, fontdict={"size": 15}) + ax.set_xlabel("Time (h)", labelpad=10, fontdict={"size": FONT_SIZE}) + ax.set_ylabel("$\\text{OD}_{600}$", labelpad=20, fontdict={"size": FONT_SIZE}) # %% @@ -587,8 +592,8 @@ def figure_b(b_data: pd.DataFrame, ax: plt.Axes): ax.set_ylim(0, 3.2) # define frequency and size of ticks and their labels - ax.xaxis.set_tick_params(width=2, length=10, labelsize=15) - ax.set_yticks(range(1, 4, 1), [1, 2, ""], fontsize=15) + ax.xaxis.set_tick_params(width=2, length=10, labelsize=FONT_SIZE) + ax.set_yticks(range(1, 4, 1), [1, 2, ""], fontsize=FONT_SIZE) ax.yaxis.set_tick_params(width=2, length=10) # adjust the "frame" of the plot @@ -607,15 +612,15 @@ def figure_b(b_data: pd.DataFrame, ax: plt.Axes): loc="upper left", bbox_to_anchor=[0.01, 1.2], frameon=False, - fontsize=15, + fontsize=FONT_SIZE, ) # set the axis labels - ax.set_xlabel("Time Interval (h)", labelpad=10, fontdict={"size": 15}) + ax.set_xlabel("Time Interval (h)", labelpad=10, fontdict={"size": FONT_SIZE}) ax.set_ylabel( "Yield $\\left( \\text{mol } \\text{mol}^{-1} \\right)$", labelpad=20, - fontdict={"size": 15}, + fontdict={"size": FONT_SIZE}, ) # indicate statistical significances @@ -636,7 +641,7 @@ def figure_b(b_data: pd.DataFrame, ax: plt.Axes): "*", horizontalalignment="center", verticalalignment="bottom", - fontsize=15, + fontsize=FONT_SIZE, ) @@ -694,10 +699,10 @@ def figure_c(c_data: pd.DataFrame, ax: plt.Axes): ax.set_ylim(0, 0.8) # define frequency and size of ticks and their labels - ax.set_xticks(range(5, 26, 5), list(range(5, 26, 5))[:-1] + [""], fontsize=15) + ax.set_xticks(range(5, 26, 5), list(range(5, 26, 5))[:-1] + [""], fontsize=FONT_SIZE) ax.xaxis.set_tick_params(width=2, length=10) y_tick_labels = np.round(np.linspace(0.1, 0.8, 8), 1) - ax.set_yticks(y_tick_labels, y_tick_labels.tolist()[:-1] + [""], fontsize=15) + ax.set_yticks(y_tick_labels, y_tick_labels.tolist()[:-1] + [""], fontsize=FONT_SIZE) ax.yaxis.set_tick_params(width=2, length=10) # adjust the "frame" of the plot @@ -713,12 +718,12 @@ def figure_c(c_data: pd.DataFrame, ax: plt.Axes): loc="upper left", bbox_to_anchor=[0.01, 1.2], frameon=False, - fontsize=15, + fontsize=FONT_SIZE, ) # set the axis labels - ax.set_xlabel("Time (h)", labelpad=10, fontdict={"size": 15}) - ax.set_ylabel("$\\text{OD}_{600}$", labelpad=20, fontdict={"size": 15}) + ax.set_xlabel("Time (h)", labelpad=10, fontdict={"size": FONT_SIZE}) + ax.set_ylabel("$\\text{OD}_{600}$", labelpad=20, fontdict={"size": FONT_SIZE}) # %% @@ -763,12 +768,12 @@ def figure_d(d_data: pd.DataFrame, ax: plt.Axes): ax2.set_ylim(0, 130) # define frequency and size of ticks and their labels - ax.set_xticks(range(5, 25, 5), range(5, 25, 5), fontsize=15) + ax.set_xticks(range(5, 25, 5), range(5, 25, 5), fontsize=FONT_SIZE) ax.xaxis.set_tick_params(width=2, length=10) - ax.set_yticks(range(1, 10, 1), list(range(1, 10, 1))[:-1] + [""], fontsize=15) + ax.set_yticks(range(1, 10, 1), list(range(1, 10, 1))[:-1] + [""], fontsize=FONT_SIZE) ax.yaxis.set_tick_params(width=2, length=10) ax2.set_yticks( - range(20, 141, 20), list(range(20, 141, 20))[:-1] + [""], fontsize=15 + range(20, 141, 20), list(range(20, 141, 20))[:-1] + [""], fontsize=FONT_SIZE ) ax2.yaxis.set_tick_params(width=2, length=10) @@ -778,18 +783,18 @@ def figure_d(d_data: pd.DataFrame, ax: plt.Axes): ax2.spines[pos].set_linewidth(lw) # set the legend - _handles1, _ = p1.get_legend_handles_labels() - _handles2, _ = p2.get_legend_handles_labels() + handles1, labels1 = p1.get_legend_handles_labels() + handles2, labels2 = p2.get_legend_handles_labels() ax2.legend_.remove() ax.legend( - _handles2 + [_handles1[-1]], + handles2 + [handles1[-1]], ["Acetate", "IBA", "Glucose", "$\\text{OD}_{600}$"], - ncol=4, + ncol=2, title=None, loc="upper left", bbox_to_anchor=[0.01, 1.2], frameon=False, - fontsize=8, + fontsize=FONT_SIZE, ) # make an annotation with arrow to point to timepoint at 1 hour @@ -797,7 +802,7 @@ def figure_d(d_data: pd.DataFrame, ax: plt.Axes): "2", (2, 10), xytext=(2, 30), - fontsize=12, + fontsize=FONT_SIZE, xycoords="data", horizontalalignment="center", verticalalignment="center", @@ -810,9 +815,9 @@ def figure_d(d_data: pd.DataFrame, ax: plt.Axes): ) # set the axis labels - ax.set_xlabel("Time (h)", labelpad=10, fontdict={"size": 15}) - ax2.set_ylabel("Concentration (mM)", labelpad=20, fontdict={"size": 15}) - ax.set_ylabel("$\\text{OD}_{600}$", labelpad=20, fontdict={"size": 15}) + ax.set_xlabel("Time (h)", labelpad=10, fontdict={"size": FONT_SIZE}) + ax2.set_ylabel("Concentration (mM)", labelpad=20, fontdict={"size": FONT_SIZE}) + ax.set_ylabel("$\\text{OD}_{600}$", labelpad=20, fontdict={"size": FONT_SIZE}) # %% [markdown] @@ -824,8 +829,7 @@ def figure_d(d_data: pd.DataFrame, ax: plt.Axes): # %% fig = plt.figure( figsize=(7.2, 9.7), - # layout="constrained", - # dpi=300, + constrained_layout=False, ) gs = fig.add_gridspec(2, 2) @@ -839,7 +843,7 @@ def figure_d(d_data: pd.DataFrame, ax: plt.Axes): ): ax = fig.add_subplot(gs[i]) plot_fun(_df, ax) - ax.set_title(label, fontsize=20, x=-0.05, y=1.1) + ax.set_title(label, fontsize=FONT_SIZE, x=-0.05, y=1.1) plt.subplots_adjust(hspace=0.5) plt.show() @@ -852,6 +856,31 @@ def figure_d(d_data: pd.DataFrame, ax: plt.Axes): # Click to see some hints # 1. Adjust the individual plots to roughly the correct size # 2. Adapt the default font sizes for axes labels, ticks, legends, and titles +# +# You might want to look into using relative size w.r.t to the default font size, e.g., +# ```python +# FONT_SIZE = 9 +# mpl.rcParams['font.size'] = FONT_SIZE +# mpl.rcParams['fig.labelsize'] = FONT_SIZE +# mpl.font_manager.font_scalings +# ``` +# which gives you the following scaling factors: +# ```python +# { +# 'xx-small': 0.579, +# 'x-small': 0.694, +# 'small': 0.833, +# 'medium': 1.0, +# 'large': 1.2, +# 'x-large': 1.44, +# 'xx-large': 1.728, +# None: 1.0 +# } +# ``` +# meaning that setting to `fontsize=None` or `fontsize='medium'` result in using `FONT_SIZE`, i.e. 9 points. +# If you prefer exact point sizes in integer, you can also do this manually, e.g. using +# `fontsize=FONT_SIZE - 1` for tick labels, `fontsize=FONT_SIZE + 1` for axis labels, +# and `fontsize=FONT_SIZE + 5` for titles (you need to play with it). # # %%