|
58 | 58 | "import xarray as xr\n", |
59 | 59 | "import numpy as np\n", |
60 | 60 | "import pandas as pd\n", |
61 | | - "import proplot as plot\n", |
62 | 61 | "# DataArray\n", |
63 | 62 | "state = np.random.RandomState(51423)\n", |
64 | | - "data = np.sin(np.linspace(0, 2*np.pi, 20))[:,None] + state.rand(20,8).cumsum(axis=1)\n", |
| 63 | + "data = np.sin(np.linspace(0, 2*np.pi, 20))[:, None] \\\n", |
| 64 | + " + state.rand(20, 8).cumsum(axis=1)\n", |
65 | 65 | "da = xr.DataArray(data, dims=('x', 'cat'), coords={\n", |
66 | | - " 'x':xr.DataArray(np.linspace(0,1,20), dims=('x',), attrs={'long_name':'distance', 'units':'km'}),\n", |
67 | | - " 'cat':xr.DataArray(np.arange(0,80,10), dims=('cat',), attrs={'long_name':'parameter', 'units':'K'})\n", |
68 | | - " }, name='position series')\n", |
| 66 | + " 'x': xr.DataArray(np.linspace(0, 1, 20), dims=('x',), attrs={'long_name': 'distance', 'units': 'km'}),\n", |
| 67 | + " 'cat': xr.DataArray(np.arange(0, 80, 10), dims=('cat',), attrs={'long_name': 'parameter', 'units': 'K'})\n", |
| 68 | + "}, name='position series')\n", |
69 | 69 | "# DataFrame\n", |
70 | | - "plot.rc.reset()\n", |
71 | 70 | "ts = pd.date_range('1/1/2000', periods=20)\n", |
72 | | - "data = (np.cos(np.linspace(0, 2*np.pi, 20))**4)[:,None] + state.rand(20,5)**2\n", |
73 | | - "df = pd.DataFrame(data, index=ts, columns=['foo','bar','baz','zap','baf'])\n", |
| 71 | + "data = (np.cos(np.linspace(0, 2*np.pi, 20))**4)[:, None] + state.rand(20, 5)**2\n", |
| 72 | + "df = pd.DataFrame(data, index=ts, columns=['foo', 'bar', 'baz', 'zap', 'baf'])\n", |
74 | 73 | "df.name = 'time series'\n", |
75 | 74 | "df.index.name = 'time (s)'\n", |
76 | 75 | "df.columns.name = 'columns'" |
|
82 | 81 | "metadata": {}, |
83 | 82 | "outputs": [], |
84 | 83 | "source": [ |
85 | | - "# Figure\n", |
| 84 | + "import proplot as plot\n", |
86 | 85 | "f, axs = plot.subplots(ncols=2, axwidth=2.2, share=0)\n", |
87 | 86 | "axs.format(suptitle='Automatic subplot formatting')\n", |
88 | 87 | "# Plot DataArray\n", |
89 | 88 | "cycle = plot.Cycle(plot.shade('light blue', 0.4), fade=90, space='hpl')\n", |
90 | | - "axs[0].plot(da, cycle=cycle, lw=3, colorbar='ul', colorbar_kw={'locator':20})\n", |
| 89 | + "axs[0].plot(da, cycle=cycle, lw=3, colorbar='ul', colorbar_kw={'locator': 20})\n", |
91 | 90 | "# Plot Dataframe\n", |
92 | 91 | "cycle = plot.Cycle(plot.shade('jade', 0.4), fade=90, space='hpl')\n", |
93 | 92 | "axs[1].plot(df, cycle=cycle, lw=3, legend='uc')" |
|
137 | 136 | "plot.rc['title.loc'] = 'uc'\n", |
138 | 137 | "plot.rc['axes.ymargin'] = plot.rc['axes.xmargin'] = 0.05\n", |
139 | 138 | "state = np.random.RandomState(51423)\n", |
140 | | - "data = state.rand(20,8).cumsum(axis=0).cumsum(axis=1)[:,::-1] + 20*state.normal(size=(20,8)) + 30\n", |
141 | | - "f, axs = plot.subplots(nrows=3, aspect=1.5, axwidth=3, share=0, hratios=(2,1,1))\n", |
| 139 | + "data = state.rand(20, 8).cumsum(axis=0).cumsum(axis=1)[:, ::-1] \\\n", |
| 140 | + " + 20*state.normal(size=(20, 8)) + 30\n", |
| 141 | + "f, axs = plot.subplots(nrows=3, aspect=1.5, axwidth=3,\n", |
| 142 | + " share=0, hratios=(2, 1, 1))\n", |
142 | 143 | "axs.format(suptitle='Error bars with various plotting commands')\n", |
143 | 144 | "# Asking add_errorbars to calculate bars\n", |
144 | 145 | "ax = axs[0]\n", |
|
147 | 148 | "# Showing a standard deviation range instead of percentile range\n", |
148 | 149 | "ax = axs[1]\n", |
149 | 150 | "ax.scatter(data, color='k', marker='x', markersize=50, barcolor='gray5',\n", |
150 | | - " medians=True, barstd=True, barrange=(-1,1), barzorder=0, boxes=False, capsize=2)\n", |
| 151 | + " medians=True, barstd=True, barrange=(-1, 1), barzorder=0, boxes=False, capsize=2)\n", |
151 | 152 | "# Supplying error bar data manually\n", |
152 | 153 | "ax = axs[2]\n", |
153 | | - "boxdata = np.percentile(data, (25,75), axis=0)\n", |
154 | | - "bardata = np.percentile(data, (5,95), axis=0)\n", |
| 154 | + "boxdata = np.percentile(data, (25, 75), axis=0)\n", |
| 155 | + "bardata = np.percentile(data, (5, 95), axis=0)\n", |
155 | 156 | "ax.plot(data.mean(axis=0), boxes=False, marker='o', markersize=5,\n", |
156 | 157 | " edgecolor='k', color='cerulean', boxdata=boxdata, bardata=bardata)\n", |
157 | 158 | "# Formatting\n", |
|
189 | 190 | "import pandas as pd\n", |
190 | 191 | "plot.rc.titleloc = 'uc'\n", |
191 | 192 | "plot.rc.margin = 0.05\n", |
192 | | - "f, axs = plot.subplots(nrows=2, aspect=2, axwidth=3.5, share=0, hratios=(3,2))\n", |
| 193 | + "f, axs = plot.subplots(nrows=2, aspect=2, axwidth=3.5, share=0, hratios=(3, 2))\n", |
193 | 194 | "state = np.random.RandomState(51423)\n", |
194 | | - "data = state.rand(5,5).cumsum(axis=0).cumsum(axis=1)[:,::-1]\n", |
195 | | - "data = pd.DataFrame(data, columns=pd.Index(np.arange(1,6), name='column'), index=pd.Index(['a','b','c','d','e'], name='row idx'))\n", |
| 195 | + "data = state.rand(5, 5).cumsum(axis=0).cumsum(axis=1)[:, ::-1]\n", |
| 196 | + "data = pd.DataFrame(data,\n", |
| 197 | + " columns=pd.Index(np.arange(1, 6), name='column'),\n", |
| 198 | + " index=pd.Index(['a', 'b', 'c', 'd', 'e'], name='row idx'))\n", |
196 | 199 | "ax = axs[0]\n", |
197 | | - "obj = ax.bar(data, cycle='Reds', colorbar='ul', edgecolor='red9', colorbar_kw={'frameon':False})\n", |
198 | | - "ax.format(xlocator=1, xminorlocator=0.5, ytickminor=False, title='Side-by-side', suptitle='Bar plot wrapper demo')\n", |
| 200 | + "obj = ax.bar(data, cycle='Reds', colorbar='ul',\n", |
| 201 | + " edgecolor='red9', colorbar_kw={'frameon': False})\n", |
| 202 | + "ax.format(xlocator=1, xminorlocator=0.5, ytickminor=False,\n", |
| 203 | + " title='Side-by-side', suptitle='Bar plot wrapper demo')\n", |
199 | 204 | "ax = axs[1]\n", |
200 | | - "obj = ax.barh(data.iloc[::-1,:], cycle='Blues', legend='ur', edgecolor='blue9', stacked=True)\n", |
| 205 | + "obj = ax.barh(data.iloc[::-1, :], cycle='Blues',\n", |
| 206 | + " legend='ur', edgecolor='blue9', stacked=True)\n", |
201 | 207 | "ax.format(title='Stacked')\n", |
202 | 208 | "axs.format(grid=False)\n", |
203 | 209 | "plot.rc.reset()" |
|
230 | 236 | "import proplot as plot\n", |
231 | 237 | "import numpy as np\n", |
232 | 238 | "plot.rc.margin = 0\n", |
233 | | - "f, axs = plot.subplots(array=[[1,2],[3,3]], hratios=(1,0.8), share=0)\n", |
| 239 | + "f, axs = plot.subplots(array=[[1, 2], [3, 3]], hratios=(1, 0.8), share=0)\n", |
234 | 240 | "axs.format(xlabel='xlabel', ylabel='ylabel', suptitle='Area plot demo')\n", |
235 | 241 | "state = np.random.RandomState(51423)\n", |
236 | | - "data = state.rand(5,3).cumsum(axis=0)\n", |
| 242 | + "data = state.rand(5, 3).cumsum(axis=0)\n", |
237 | 243 | "cycle = ('gray3', 'gray5', 'gray7')\n", |
| 244 | + "# 2D arrays\n", |
238 | 245 | "ax = axs[0]\n", |
239 | | - "ax.areax(np.arange(5), data, data + state.rand(5)[:,None], cycle=cycle, alpha=0.5,\n", |
240 | | - " legend='uc', legend_kw={'center':True, 'ncols':2, 'labels':['z','y','qqqq']},\n", |
241 | | - " )\n", |
| 246 | + "ax.areax(np.arange(5), data, data + state.rand(5)[:, None], cycle=cycle, alpha=0.5,\n", |
| 247 | + " legend='uc', legend_kw={'center': True, 'ncols': 2, 'labels': ['z', 'y', 'qqqq']},\n", |
| 248 | + " )\n", |
242 | 249 | "ax.format(title='Fill between columns')\n", |
243 | 250 | "ax = axs[1]\n", |
244 | 251 | "ax.area(np.arange(5), data, stacked=True, cycle=cycle, alpha=0.8,\n", |
245 | | - " legend='ul', legend_kw={'center':True, 'ncols':2, 'labels':['z','y','qqqq']},\n", |
| 252 | + " legend='ul', legend_kw={'center': True, 'ncols': 2, 'labels': ['z', 'y', 'qqqq']},\n", |
246 | 253 | " )\n", |
247 | 254 | "ax.format(title='Stack between columns')\n", |
| 255 | + "# Positive and negative colors\n", |
248 | 256 | "ax = axs[2]\n", |
249 | 257 | "data = 5*(state.rand(20)-0.5)\n", |
250 | 258 | "ax.area(data, negpos=True, negcolor='blue7', poscolor='red7')\n", |
|
281 | 289 | "N = 500\n", |
282 | 290 | "state = np.random.RandomState(51423)\n", |
283 | 291 | "f, axs = plot.subplots(ncols=2)\n", |
284 | | - "data = state.normal(size=(N,5)) + 2*(state.rand(N,5)-0.5)*np.arange(5)\n", |
285 | | - "data = pd.DataFrame(data, columns=pd.Index(['a','b','c','d','e'], name='xlabel'))\n", |
| 292 | + "data = state.normal(size=(N, 5)) + 2*(state.rand(N, 5)-0.5)*np.arange(5)\n", |
| 293 | + "data = pd.DataFrame(data, columns=pd.Index(\n", |
| 294 | + " ['a', 'b', 'c', 'd', 'e'], name='xlabel'))\n", |
286 | 295 | "ax = axs[0]\n", |
287 | | - "obj1 = ax.boxplot(data, lw=0.7, marker='x', fillcolor='gray5', medianlw=1, mediancolor='k')#, boxprops={'color':'C0'})#, labels=data.columns)\n", |
| 296 | + "# , boxprops={'color':'C0'})#, labels=data.columns)\n", |
| 297 | + "obj1 = ax.boxplot(data, lw=0.7, marker='x', fillcolor='gray5',\n", |
| 298 | + " medianlw=1, mediancolor='k')\n", |
288 | 299 | "ax.format(title='Box plots', titleloc='uc')\n", |
289 | 300 | "ax = axs[1]\n", |
290 | | - "obj2 = ax.violinplot(data, lw=0.7, fillcolor='gray7', points=500, bw_method=0.3, means=True)\n", |
| 301 | + "obj2 = ax.violinplot(data, lw=0.7, fillcolor='gray7',\n", |
| 302 | + " points=500, bw_method=0.3, means=True)\n", |
291 | 303 | "ax.format(title='Violin plots', titleloc='uc')\n", |
292 | | - "axs.format(ymargin=0.1, xmargin=0.1, grid=False, suptitle='Boxes and violins demo')" |
| 304 | + "axs.format(ymargin=0.1, xmargin=0.1, grid=False,\n", |
| 305 | + " suptitle='Boxes and violins demo')" |
293 | 306 | ] |
294 | 307 | }, |
295 | 308 | { |
|
320 | 333 | "N = 50\n", |
321 | 334 | "cmap = 'IceFire'\n", |
322 | 335 | "values = np.linspace(-N/2, N/2, N)\n", |
323 | | - "f, axs = plot.subplots(share=0, ncols=2, wratios=(2,1), axwidth='6cm', aspect=(2,1))\n", |
| 336 | + "f, axs = plot.subplots(share=0, ncols=2, wratios=(2, 1),\n", |
| 337 | + " axwidth='6cm', aspect=(2, 1))\n", |
324 | 338 | "ax = axs[0]\n", |
325 | 339 | "state = np.random.RandomState(51423)\n", |
326 | | - "m = ax.plot((state.rand(N)-0.5).cumsum(), state.rand(N), cmap=cmap, values=values, lw=7, extend='both')\n", |
327 | | - "ax.format(xlabel='xlabel', ylabel='ylabel', title='Line with smooth color gradations', titleweight='bold')\n", |
328 | | - "ax.format(xlim=(-1,5), ylim=(-0.2,1.2))\n", |
| 340 | + "m = ax.plot((state.rand(N) - 0.5).cumsum(), state.rand(N),\n", |
| 341 | + " cmap=cmap, values=values, lw=7, extend='both')\n", |
| 342 | + "ax.format(xlabel='xlabel', ylabel='ylabel',\n", |
| 343 | + " title='Line with smooth color gradations', titleweight='bold')\n", |
| 344 | + "ax.format(xlim=(-1, 5), ylim=(-0.2, 1.2))\n", |
329 | 345 | "ax.colorbar(m, loc='b', label='parametric coordinate', locator=5)\n", |
330 | 346 | "N = 12\n", |
331 | 347 | "ax = axs[1]\n", |
332 | 348 | "values = np.linspace(-N/2, N/2 - 1, N)\n", |
333 | | - "radii = np.linspace(1,0.2,N)\n", |
334 | | - "angles = np.linspace(0,4*np.pi,N)\n", |
| 349 | + "radii = np.linspace(1, 0.2, N)\n", |
| 350 | + "angles = np.linspace(0, 4*np.pi, N)\n", |
335 | 351 | "x = radii*np.cos(1.4*angles)\n", |
336 | 352 | "y = radii*np.sin(1.4*angles)\n", |
337 | 353 | "m = ax.plot(x, y, values=values, linewidth=15, interp=False, cmap=cmap)\n", |
338 | | - "ax.format(xlim=(-1,1), ylim=(-1,1), title='Step gradations', titleweight='bold', xlabel='cosine angle', ylabel='sine angle')\n", |
| 354 | + "ax.format(xlim=(-1, 1), ylim=(-1, 1), title='Step gradations',\n", |
| 355 | + " titleweight='bold', xlabel='cosine angle', ylabel='sine angle')\n", |
339 | 356 | "ax.colorbar(m, loc='b', maxn=10, label=f'parametric coordinate')" |
340 | 357 | ] |
341 | 358 | }, |
|
369 | 386 | "f, axs = plot.subplots(ncols=2, share=1)\n", |
370 | 387 | "state = np.random.RandomState(51423)\n", |
371 | 388 | "x = (state.rand(20)-0).cumsum()\n", |
372 | | - "data = (state.rand(20,4)-0.5).cumsum(axis=0)\n", |
373 | | - "data = pd.DataFrame(data, columns=pd.Index(['a','b','c','d'], name='label'))\n", |
| 389 | + "data = (state.rand(20, 4)-0.5).cumsum(axis=0)\n", |
| 390 | + "data = pd.DataFrame(data, columns=pd.Index(['a', 'b', 'c', 'd'], name='label'))\n", |
374 | 391 | "# Scatter demo\n", |
375 | 392 | "ax = axs[0]\n", |
376 | 393 | "ax.format(title='Extra prop cycle properties', suptitle='Scatter plot demo')\n", |
377 | | - "obj = ax.scatter(x, data, legend='ul', cycle='warm', legend_kw={'ncols':2},\n", |
378 | | - " cycle_kw={'marker':['x','o','x','o'], 'markersize':[5,10,20,30]})\n", |
| 394 | + "obj = ax.scatter(x, data, legend='ul', cycle='warm', legend_kw={'ncols': 2},\n", |
| 395 | + " cycle_kw={'marker': ['x', 'o', 'x', 'o'], 'markersize': [5, 10, 20, 30]})\n", |
379 | 396 | "ax = axs[1]\n", |
380 | 397 | "ax.format(title='Scatter plot with cmap')\n", |
381 | | - "data = (state.rand(2,100) - 0.5)\n", |
| 398 | + "data = (state.rand(2, 100) - 0.5)\n", |
382 | 399 | "obj = ax.scatter(*data, color=data.sum(axis=0), size=state.rand(100), smin=3, smax=30,\n", |
383 | | - " marker='o', cmap='dusk', colorbar='lr', colorbar_kw={'locator':0.5, 'label':'label'})\n", |
| 400 | + " marker='o', cmap='dusk', colorbar='lr',\n", |
| 401 | + " colorbar_kw={'locator': 0.5, 'label': 'label'})\n", |
384 | 402 | "axs.format(xlabel='xlabel', ylabel='ylabel')" |
385 | 403 | ] |
386 | 404 | } |
|
0 commit comments