Skip to content

Commit 90df903

Browse files
committed
Add SymLogNorm example
1 parent 01f2b18 commit 90df903

File tree

1 file changed

+68
-5
lines changed

1 file changed

+68
-5
lines changed

matplotlib.ipynb

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
},
123123
"outputs": [],
124124
"source": [
125-
"x = np.linspace(0, 1, 100) # 100 numbers from 0 to 1\n",
125+
"x = np.linspace(0, 1, 250) # 250 numbers from 0 to 1\n",
126126
"\n",
127127
"plt.plot(x, x**2)\n",
128128
"# If not interactive, e.g. in a script: \n",
@@ -144,7 +144,7 @@
144144
},
145145
"outputs": [],
146146
"source": [
147-
"t = np.linspace(0, 2 * np.pi) # 50 points between 0 and 2π\n",
147+
"t = np.linspace(0, 2 * np.pi, 50) # 50 points between 0 and 2π\n",
148148
"plt.plot(t, np.sin(t));"
149149
]
150150
},
@@ -373,6 +373,33 @@
373373
"**Remember**: Legend entries are only generated for plot objects that have a label (note x⁴ is missing)!"
374374
]
375375
},
376+
{
377+
"cell_type": "markdown",
378+
"metadata": {},
379+
"source": [
380+
"### Legend outside the plot"
381+
]
382+
},
383+
{
384+
"cell_type": "code",
385+
"execution_count": null,
386+
"metadata": {},
387+
"outputs": [],
388+
"source": [
389+
"plt.plot(t, np.sin(t), label=r'$\\sin(t)$')\n",
390+
"plt.plot(t, np.cos(t), label=r'$\\cos(t)$')\n",
391+
"plt.legend()\n",
392+
"\n",
393+
"# put the legend in the center above the plot\n",
394+
"# coordinates are relative to the axes size.\n",
395+
"plt.legend(\n",
396+
" bbox_to_anchor=(0.5, 1.01),\n",
397+
" loc='lower center', # this no sets the anchor of the legend\n",
398+
" ncol=2,\n",
399+
")\n",
400+
"plt.tight_layout()"
401+
]
402+
},
376403
{
377404
"cell_type": "markdown",
378405
"metadata": {},
@@ -494,7 +521,7 @@
494521
"errX = np.random.normal(0, 0.4, 10)\n",
495522
"errY = np.random.normal(0, 0.4, 10)\n",
496523
"\n",
497-
"plt.errorbar(x + errX, x + errY, xerr=0.4, yerr=errY, fmt='o');"
524+
"plt.errorbar(x + errX, x + errY, xerr=0.4, yerr=errY, linestyle='');"
498525
]
499526
},
500527
{
@@ -699,6 +726,29 @@
699726
"### Using Norms – e.g. Logarithmic colorscale"
700727
]
701728
},
729+
{
730+
"cell_type": "code",
731+
"execution_count": null,
732+
"metadata": {},
733+
"outputs": [],
734+
"source": [
735+
"from matplotlib.colors import LogNorm, DivergingNorm, SymLogNorm\n",
736+
"\n",
737+
"plt.hist2d(x, y, bins=50, norm=LogNorm())\n",
738+
"plt.colorbar(label='Counts');"
739+
]
740+
},
741+
{
742+
"cell_type": "markdown",
743+
"metadata": {},
744+
"source": [
745+
"### SymLogNorm\n",
746+
"\n",
747+
"The SymLogNorm uses two logscales, one for the negative, one for the positive numbers.\n",
748+
"Around 0, a linear scale is used. The threshold value for the linear scale\n",
749+
"has to be given."
750+
]
751+
},
702752
{
703753
"cell_type": "code",
704754
"execution_count": null,
@@ -707,9 +757,22 @@
707757
},
708758
"outputs": [],
709759
"source": [
710-
"from matplotlib.colors import LogNorm, DivergingNorm, SymLogNorm\n",
760+
"x2, y2 = np.random.multivariate_normal(mean, cov, size=10000).T\n",
711761
"\n",
712-
"plt.hist2d(x, y, bins=50, norm=LogNorm())\n",
762+
"\n",
763+
"hist1, xedges, yedges = np.histogram2d(x, y, bins=100)\n",
764+
"hist2, _, _ = np.histogram2d(x2, y2, bins=[xedges, yedges])\n",
765+
"\n",
766+
"\n",
767+
"plt.pcolormesh(\n",
768+
" xedges,\n",
769+
" yedges,\n",
770+
" hist1 - hist2,\n",
771+
" cmap='RdBu',\n",
772+
" norm=SymLogNorm(0.1),\n",
773+
" vmin=-15,\n",
774+
" vmax=15,\n",
775+
")\n",
713776
"\n",
714777
"plt.colorbar();"
715778
]

0 commit comments

Comments
 (0)