Skip to content

Commit

Permalink
Fix overlapping legends in matplotlib plot
Browse files Browse the repository at this point in the history
  • Loading branch information
jngrad committed Sep 30, 2020
1 parent 4e7ee29 commit 6426cbf
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions doc/tutorials/01-lennard_jones/01-lennard_jones.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -699,23 +699,24 @@
"# Plot total energy and instantaneous temperature\n",
"import matplotlib.pyplot as plt\n",
"\n",
"fig1,ax = plt.subplots(num=None, figsize=(10, 6),\n",
"fig1,ax1 = plt.subplots(num=None, figsize=(10, 6),\n",
" dpi=80, facecolor='w', edgecolor='k')\n",
"fig1.set_tight_layout(False)\n",
"\n",
"t = np.arange(0,SAMPLING_ITERATIONS*SAMPLING_INTERVAL,SAMPLING_INTERVAL)\n",
"ax.plot(t,e_total, lw=2, label='e_total')\n",
"ax.set_xlabel('time step', fontsize=20)\n",
"ax.set_ylabel('total energy', fontsize=20)\n",
"ax.legend(fontsize=16)\n",
"ax.tick_params(axis='both', labelsize=16)\n",
"ax2 = ax.twinx()\n",
"ax2.plot(t,e_kin/ (1.5 * N_PART), color='r', ls='--', lw=2,\n",
"ln1 = ax1.plot(t, e_total, lw=2, label='total energy')\n",
"ax1.set_xlabel('time step', fontsize=20)\n",
"ax1.set_ylabel('Energy', fontsize=20)\n",
"ax1.tick_params(axis='both', labelsize=16)\n",
"ax2 = ax1.twinx()\n",
"ln2 = ax2.plot(t, e_kin/ (1.5 * N_PART), color='r', ls='--', lw=2,\n",
" label='instantaneous temperature')\n",
"ax2.axhline(TEMPERATURE, ls='-.', lw=3, color='k')\n",
"ax2.set_ylabel('instantaneous temperature', fontsize=20)\n",
"ax2.legend(fontsize=16)\n",
"ax2.set_ylabel('Temperature', fontsize=20)\n",
"ax2.tick_params(axis='both', labelsize=16)\n",
"lns = ln1 + ln2\n",
"labs = [l.get_label() for l in lns]\n",
"ax1.legend(lns, labs, fontsize=16)\n",
"print(\"average simulation temperature: {:1.3f}\".format(np.round(e_kin.mean()/ (1.5 * N_PART),3)),\n",
" \"\\t(which differs by {:1.3f}% from TEMPERATURE)\".format(np.round(((e_kin.mean()/ (1.5 * N_PART) - TEMPERATURE) / TEMPERATURE)*100,3)))\n",
"```"
Expand All @@ -740,7 +741,7 @@
"solution2": "hidden"
},
"source": [
"``` python\n",
"```python\n",
"# autocorrelation using numpy.correlate\n",
"def autocor(x):\n",
" mean=x.mean()\n",
Expand Down

0 comments on commit 6426cbf

Please sign in to comment.