Skip to content

Commit 22af8f9

Browse files
committed
Notebook examples formatting improvements
1 parent 3ca9a02 commit 22af8f9

File tree

11 files changed

+809
-396
lines changed

11 files changed

+809
-396
lines changed

docs/1dplots.ipynb

Lines changed: 105 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"import xarray as xr\n",
5959
"import numpy as np\n",
6060
"import pandas as pd\n",
61+
"\n",
6162
"# DataArray\n",
6263
"state = np.random.RandomState(51423)\n",
6364
"data = np.sin(np.linspace(0, 2*np.pi, 20))[:, None] \\\n",
@@ -66,6 +67,7 @@
6667
" 'x': xr.DataArray(np.linspace(0, 1, 20), dims=('x',), attrs={'long_name': 'distance', 'units': 'km'}),\n",
6768
" 'cat': xr.DataArray(np.arange(0, 80, 10), dims=('cat',), attrs={'long_name': 'parameter', 'units': 'K'})\n",
6869
"}, name='position series')\n",
70+
"\n",
6971
"# DataFrame\n",
7072
"ts = pd.date_range('1/1/2000', periods=20)\n",
7173
"data = (np.cos(np.linspace(0, 2*np.pi, 20))**4)[:, None] + state.rand(20, 5)**2\n",
@@ -84,9 +86,11 @@
8486
"import proplot as plot\n",
8587
"f, axs = plot.subplots(ncols=2, axwidth=2.2, share=0)\n",
8688
"axs.format(suptitle='Automatic subplot formatting')\n",
89+
"\n",
8790
"# Plot DataArray\n",
8891
"cycle = plot.Cycle(plot.shade('light blue', 0.4), fade=90, space='hpl')\n",
8992
"axs[0].plot(da, cycle=cycle, lw=3, colorbar='ul', colorbar_kw={'locator': 20})\n",
93+
"\n",
9094
"# Plot Dataframe\n",
9195
"cycle = plot.Cycle(plot.shade('jade', 0.4), fade=90, space='hpl')\n",
9296
"axs[1].plot(df, cycle=cycle, lw=3, legend='uc')"
@@ -122,28 +126,36 @@
122126
"state = np.random.RandomState(51423)\n",
123127
"data = state.rand(20, 8).cumsum(axis=0).cumsum(axis=1)[:, ::-1] \\\n",
124128
" + 20*state.normal(size=(20, 8)) + 30\n",
125-
"f, axs = plot.subplots(nrows=3, aspect=1.5, axwidth=3,\n",
126-
" share=0, hratios=(2, 1, 1))\n",
129+
"f, axs = plot.subplots(\n",
130+
" nrows=3, aspect=1.5, axwidth=3,\n",
131+
" share=0, hratios=(2, 1, 1)\n",
132+
")\n",
127133
"axs.format(suptitle='Error bars with various plotting commands')\n",
134+
"axs[1:].format(xlabel='column number', xticks=1, xgrid=False)\n",
135+
"\n",
128136
"# Asking add_errorbars to calculate bars\n",
129137
"ax = axs[0]\n",
130138
"obj = ax.barh(data, color='red orange', means=True)\n",
131139
"ax.format(title='Column statistics')\n",
140+
"ax.format(ylabel='column number', title='Bar plot', ygrid=False)\n",
141+
"\n",
132142
"# Showing a standard deviation range instead of percentile range\n",
133143
"ax = axs[1]\n",
134-
"ax.scatter(data, color='k', marker='x', markersize=50, barcolor='gray5',\n",
135-
" medians=True, barstd=True, barrange=(-1, 1), barzorder=0, boxes=False, capsize=2)\n",
144+
"ax.scatter(\n",
145+
" data, color='k', marker='x', markersize=50, barcolor='gray5',\n",
146+
" medians=True, barstd=True, barrange=(-1, 1), barzorder=0, boxes=False, capsize=2\n",
147+
")\n",
148+
"ax.format(title='Scatter plot')\n",
149+
"\n",
136150
"# Supplying error bar data manually\n",
137151
"ax = axs[2]\n",
138152
"boxdata = np.percentile(data, (25, 75), axis=0)\n",
139153
"bardata = np.percentile(data, (5, 95), axis=0)\n",
140-
"ax.plot(data.mean(axis=0), boxes=False, marker='o', markersize=5,\n",
141-
" edgecolor='k', color='cerulean', boxdata=boxdata, bardata=bardata)\n",
142-
"# Formatting\n",
143-
"axs[0].format(ylabel='column number', title='Bar plot', ygrid=False)\n",
144-
"axs[1].format(title='Scatter plot')\n",
145-
"axs[2].format(title='Line plot')\n",
146-
"axs[1:].format(xlabel='column number', xticks=1, xgrid=False)\n",
154+
"ax.plot(\n",
155+
" data.mean(axis=0), boxes=False, marker='o', markersize=5,\n",
156+
" edgecolor='k', color='cerulean', boxdata=boxdata, bardata=bardata\n",
157+
")\n",
158+
"ax.format(title='Line plot')\n",
147159
"plot.rc.reset()"
148160
]
149161
},
@@ -177,17 +189,28 @@
177189
"f, axs = plot.subplots(nrows=2, aspect=2, axwidth=3.5, share=0, hratios=(3, 2))\n",
178190
"state = np.random.RandomState(51423)\n",
179191
"data = state.rand(5, 5).cumsum(axis=0).cumsum(axis=1)[:, ::-1]\n",
180-
"data = pd.DataFrame(data,\n",
181-
" columns=pd.Index(np.arange(1, 6), name='column'),\n",
182-
" index=pd.Index(['a', 'b', 'c', 'd', 'e'], name='row idx'))\n",
192+
"data = pd.DataFrame(\n",
193+
" data, columns=pd.Index(np.arange(1, 6), name='column'),\n",
194+
" index=pd.Index(['a', 'b', 'c', 'd', 'e'], name='row idx')\n",
195+
")\n",
196+
"\n",
197+
"# Side-by-side bars\n",
183198
"ax = axs[0]\n",
184-
"obj = ax.bar(data, cycle='Reds', colorbar='ul',\n",
185-
" edgecolor='red9', colorbar_kw={'frameon': False})\n",
186-
"ax.format(xlocator=1, xminorlocator=0.5, ytickminor=False,\n",
187-
" title='Side-by-side', suptitle='Bar plot wrapper demo')\n",
199+
"obj = ax.bar(\n",
200+
" data, cycle='Reds', colorbar='ul',\n",
201+
" edgecolor='red9', colorbar_kw={'frameon': False}\n",
202+
")\n",
203+
"ax.format(\n",
204+
" xlocator=1, xminorlocator=0.5, ytickminor=False,\n",
205+
" title='Side-by-side', suptitle='Bar plot wrapper demo'\n",
206+
")\n",
207+
"\n",
208+
"# Stacked bars\n",
188209
"ax = axs[1]\n",
189-
"obj = ax.barh(data.iloc[::-1, :], cycle='Blues',\n",
190-
" legend='ur', edgecolor='blue9', stacked=True)\n",
210+
"obj = ax.barh(\n",
211+
" data.iloc[::-1, :], cycle='Blues',\n",
212+
" legend='ur', edgecolor='blue9', stacked=True\n",
213+
")\n",
191214
"ax.format(title='Stacked')\n",
192215
"axs.format(grid=False)\n",
193216
"plot.rc.reset()"
@@ -225,18 +248,22 @@
225248
"state = np.random.RandomState(51423)\n",
226249
"data = state.rand(5, 3).cumsum(axis=0)\n",
227250
"cycle = ('gray3', 'gray5', 'gray7')\n",
228-
"# 2D arrays\n",
251+
"\n",
252+
"# Overlaid and stacked area patches\n",
229253
"ax = axs[0]\n",
230-
"ax.areax(np.arange(5), data, data + state.rand(5)[:, None], cycle=cycle, alpha=0.5,\n",
231-
" legend='uc', legend_kw={'center': True, 'ncols': 2, 'labels': ['z', 'y', 'qqqq']},\n",
232-
" )\n",
254+
"ax.area(\n",
255+
" np.arange(5), data, data + state.rand(5)[:, None], cycle=cycle, alpha=0.5,\n",
256+
" legend='uc', legend_kw={'center': True, 'ncols': 2, 'labels': ['z', 'y', 'qqqq']},\n",
257+
")\n",
233258
"ax.format(title='Fill between columns')\n",
234259
"ax = axs[1]\n",
235-
"ax.area(np.arange(5), data, stacked=True, cycle=cycle, alpha=0.8,\n",
236-
" legend='ul', legend_kw={'center': True, 'ncols': 2, 'labels': ['z', 'y', 'qqqq']},\n",
237-
" )\n",
260+
"ax.area(\n",
261+
" np.arange(5), data, stacked=True, cycle=cycle, alpha=0.8,\n",
262+
" legend='ul', legend_kw={'center': True, 'ncols': 2, 'labels': ['z', 'y', 'qqqq']},\n",
263+
")\n",
238264
"ax.format(title='Stack between columns')\n",
239-
"# Positive and negative colors\n",
265+
"\n",
266+
"# Positive and negative color area patches\n",
240267
"ax = axs[2]\n",
241268
"data = 5*(state.rand(20)-0.5)\n",
242269
"ax.area(data, negpos=True, negcolor='blue7', poscolor='red7')\n",
@@ -274,19 +301,30 @@
274301
"state = np.random.RandomState(51423)\n",
275302
"f, axs = plot.subplots(ncols=2)\n",
276303
"data = state.normal(size=(N, 5)) + 2*(state.rand(N, 5)-0.5)*np.arange(5)\n",
277-
"data = pd.DataFrame(data, columns=pd.Index(\n",
278-
" ['a', 'b', 'c', 'd', 'e'], name='xlabel'))\n",
304+
"data = pd.DataFrame(\n",
305+
" data,\n",
306+
" columns=pd.Index(['a', 'b', 'c', 'd', 'e'], name='xlabel')\n",
307+
")\n",
308+
"axs.format(\n",
309+
" ymargin=0.1, xmargin=0.1, grid=False,\n",
310+
" suptitle='Boxes and violins demo'\n",
311+
")\n",
312+
"\n",
313+
"# Box plots\n",
279314
"ax = axs[0]\n",
280-
"# , boxprops={'color':'C0'})#, labels=data.columns)\n",
281-
"obj1 = ax.boxplot(data, lw=0.7, marker='x', fillcolor='gray5',\n",
282-
" medianlw=1, mediancolor='k')\n",
315+
"obj1 = ax.boxplot(\n",
316+
" data, lw=0.7, marker='x', fillcolor='gray5',\n",
317+
" medianlw=1, mediancolor='k'\n",
318+
")\n",
283319
"ax.format(title='Box plots', titleloc='uc')\n",
320+
"\n",
321+
"# Violin plots\n",
284322
"ax = axs[1]\n",
285-
"obj2 = ax.violinplot(data, lw=0.7, fillcolor='gray7',\n",
286-
" points=500, bw_method=0.3, means=True)\n",
287-
"ax.format(title='Violin plots', titleloc='uc')\n",
288-
"axs.format(ymargin=0.1, xmargin=0.1, grid=False,\n",
289-
" suptitle='Boxes and violins demo')"
323+
"obj2 = ax.violinplot(\n",
324+
" data, lw=0.7, fillcolor='gray7',\n",
325+
" points=500, bw_method=0.3, means=True\n",
326+
")\n",
327+
"ax.format(title='Violin plots', titleloc='uc')"
290328
]
291329
},
292330
{
@@ -316,19 +354,25 @@
316354
"N = 50\n",
317355
"cmap = 'IceFire'\n",
318356
"values = np.linspace(-N/2, N/2, N)\n",
319-
"f, axs = plot.subplots(share=0, ncols=2, wratios=(2, 1),\n",
320-
" axwidth='6cm', aspect=(2, 1))\n",
357+
"f, axs = plot.subplots(\n",
358+
" share=0, ncols=2, wratios=(2, 1),\n",
359+
" axwidth='7cm', aspect=(2, 1)\n",
360+
")\n",
321361
"axs.format(suptitle='Parametric plots demo')\n",
322-
"# Smooth gradations\n",
362+
"\n",
363+
"# Parametric line with smooth gradations\n",
323364
"ax = axs[0]\n",
324365
"state = np.random.RandomState(51423)\n",
325366
"m = ax.plot((state.rand(N) - 0.5).cumsum(), state.rand(N),\n",
326367
" cmap=cmap, values=values, lw=7, extend='both')\n",
327-
"ax.format(xlabel='xlabel', ylabel='ylabel',\n",
328-
" title='Line with smooth gradations')\n",
368+
"ax.format(\n",
369+
" xlabel='xlabel', ylabel='ylabel',\n",
370+
" title='Line with smooth gradations'\n",
371+
")\n",
329372
"ax.format(xlim=(-1, 5), ylim=(-0.2, 1.2))\n",
330373
"ax.colorbar(m, loc='b', label='parametric coordinate', locator=5)\n",
331-
"# Step gradations\n",
374+
"\n",
375+
"# Parametric line with stepped gradations\n",
332376
"N = 12\n",
333377
"ax = axs[1]\n",
334378
"values = np.linspace(-N/2, N/2, N + 1)\n",
@@ -337,8 +381,10 @@
337381
"x = radii*np.cos(1.4*angles)\n",
338382
"y = radii*np.sin(1.4*angles)\n",
339383
"m = ax.plot(x, y, values=values, linewidth=15, interp=False, cmap=cmap)\n",
340-
"ax.format(xlim=(-1, 1), ylim=(-1, 1), title='Step gradations',\n",
341-
" xlabel='cosine angle', ylabel='sine angle')\n",
384+
"ax.format(\n",
385+
" xlim=(-1, 1), ylim=(-1, 1), title='Step gradations',\n",
386+
" xlabel='cosine angle', ylabel='sine angle'\n",
387+
")\n",
342388
"ax.colorbar(m, loc='b', maxn=10, label=f'parametric coordinate')"
343389
]
344390
},
@@ -374,17 +420,24 @@
374420
"x = (state.rand(20)-0).cumsum()\n",
375421
"data = (state.rand(20, 4)-0.5).cumsum(axis=0)\n",
376422
"data = pd.DataFrame(data, columns=pd.Index(['a', 'b', 'c', 'd'], name='label'))\n",
377-
"# Scatter demo\n",
423+
"\n",
424+
"# Scatter plot with property cycler\n",
378425
"ax = axs[0]\n",
379426
"ax.format(title='Extra prop cycle properties', suptitle='Scatter plot demo')\n",
380-
"obj = ax.scatter(x, data, legend='ul', cycle='warm', legend_kw={'ncols': 2},\n",
381-
" cycle_kw={'marker': ['x', 'o', 'x', 'o'], 'markersize': [5, 10, 20, 30]})\n",
427+
"obj = ax.scatter(\n",
428+
" x, data, legend='ul', cycle='warm', legend_kw={'ncols': 2},\n",
429+
" cycle_kw={'marker': ['x', 'o', 'x', 'o'], 'markersize': [5, 10, 20, 30]}\n",
430+
")\n",
431+
"\n",
432+
"# Scatter plot with colormap\n",
382433
"ax = axs[1]\n",
383434
"ax.format(title='Scatter plot with cmap')\n",
384435
"data = state.rand(2, 100)\n",
385-
"obj = ax.scatter(*data, color=data.sum(axis=0), size=state.rand(100), smin=3, smax=30,\n",
386-
" marker='o', cmap='plum', colorbar='lr', vmin=0, vmax=2,\n",
387-
" colorbar_kw={'label': 'label', 'locator':0.5})\n",
436+
"obj = ax.scatter(\n",
437+
" *data, color=data.sum(axis=0), size=state.rand(100), smin=3, smax=30,\n",
438+
" marker='o', cmap='plum', colorbar='lr', vmin=0, vmax=2,\n",
439+
" colorbar_kw={'label': 'label', 'locator':0.5}\n",
440+
")\n",
388441
"axs.format(xlabel='xlabel', ylabel='ylabel')"
389442
]
390443
}

0 commit comments

Comments
 (0)