|
58 | 58 | "import xarray as xr\n", |
59 | 59 | "import numpy as np\n", |
60 | 60 | "import pandas as pd\n", |
| 61 | + "\n", |
61 | 62 | "# DataArray\n", |
62 | 63 | "state = np.random.RandomState(51423)\n", |
63 | 64 | "data = np.sin(np.linspace(0, 2*np.pi, 20))[:, None] \\\n", |
|
66 | 67 | " 'x': xr.DataArray(np.linspace(0, 1, 20), dims=('x',), attrs={'long_name': 'distance', 'units': 'km'}),\n", |
67 | 68 | " 'cat': xr.DataArray(np.arange(0, 80, 10), dims=('cat',), attrs={'long_name': 'parameter', 'units': 'K'})\n", |
68 | 69 | "}, name='position series')\n", |
| 70 | + "\n", |
69 | 71 | "# DataFrame\n", |
70 | 72 | "ts = pd.date_range('1/1/2000', periods=20)\n", |
71 | 73 | "data = (np.cos(np.linspace(0, 2*np.pi, 20))**4)[:, None] + state.rand(20, 5)**2\n", |
|
84 | 86 | "import proplot as plot\n", |
85 | 87 | "f, axs = plot.subplots(ncols=2, axwidth=2.2, share=0)\n", |
86 | 88 | "axs.format(suptitle='Automatic subplot formatting')\n", |
| 89 | + "\n", |
87 | 90 | "# Plot DataArray\n", |
88 | 91 | "cycle = plot.Cycle(plot.shade('light blue', 0.4), fade=90, space='hpl')\n", |
89 | 92 | "axs[0].plot(da, cycle=cycle, lw=3, colorbar='ul', colorbar_kw={'locator': 20})\n", |
| 93 | + "\n", |
90 | 94 | "# Plot Dataframe\n", |
91 | 95 | "cycle = plot.Cycle(plot.shade('jade', 0.4), fade=90, space='hpl')\n", |
92 | 96 | "axs[1].plot(df, cycle=cycle, lw=3, legend='uc')" |
|
122 | 126 | "state = np.random.RandomState(51423)\n", |
123 | 127 | "data = state.rand(20, 8).cumsum(axis=0).cumsum(axis=1)[:, ::-1] \\\n", |
124 | 128 | " + 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", |
127 | 133 | "axs.format(suptitle='Error bars with various plotting commands')\n", |
| 134 | + "axs[1:].format(xlabel='column number', xticks=1, xgrid=False)\n", |
| 135 | + "\n", |
128 | 136 | "# Asking add_errorbars to calculate bars\n", |
129 | 137 | "ax = axs[0]\n", |
130 | 138 | "obj = ax.barh(data, color='red orange', means=True)\n", |
131 | 139 | "ax.format(title='Column statistics')\n", |
| 140 | + "ax.format(ylabel='column number', title='Bar plot', ygrid=False)\n", |
| 141 | + "\n", |
132 | 142 | "# Showing a standard deviation range instead of percentile range\n", |
133 | 143 | "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", |
136 | 150 | "# Supplying error bar data manually\n", |
137 | 151 | "ax = axs[2]\n", |
138 | 152 | "boxdata = np.percentile(data, (25, 75), axis=0)\n", |
139 | 153 | "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", |
147 | 159 | "plot.rc.reset()" |
148 | 160 | ] |
149 | 161 | }, |
|
177 | 189 | "f, axs = plot.subplots(nrows=2, aspect=2, axwidth=3.5, share=0, hratios=(3, 2))\n", |
178 | 190 | "state = np.random.RandomState(51423)\n", |
179 | 191 | "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", |
183 | 198 | "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", |
188 | 209 | "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", |
191 | 214 | "ax.format(title='Stacked')\n", |
192 | 215 | "axs.format(grid=False)\n", |
193 | 216 | "plot.rc.reset()" |
|
225 | 248 | "state = np.random.RandomState(51423)\n", |
226 | 249 | "data = state.rand(5, 3).cumsum(axis=0)\n", |
227 | 250 | "cycle = ('gray3', 'gray5', 'gray7')\n", |
228 | | - "# 2D arrays\n", |
| 251 | + "\n", |
| 252 | + "# Overlaid and stacked area patches\n", |
229 | 253 | "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", |
233 | 258 | "ax.format(title='Fill between columns')\n", |
234 | 259 | "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", |
238 | 264 | "ax.format(title='Stack between columns')\n", |
239 | | - "# Positive and negative colors\n", |
| 265 | + "\n", |
| 266 | + "# Positive and negative color area patches\n", |
240 | 267 | "ax = axs[2]\n", |
241 | 268 | "data = 5*(state.rand(20)-0.5)\n", |
242 | 269 | "ax.area(data, negpos=True, negcolor='blue7', poscolor='red7')\n", |
|
274 | 301 | "state = np.random.RandomState(51423)\n", |
275 | 302 | "f, axs = plot.subplots(ncols=2)\n", |
276 | 303 | "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", |
279 | 314 | "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", |
283 | 319 | "ax.format(title='Box plots', titleloc='uc')\n", |
| 320 | + "\n", |
| 321 | + "# Violin plots\n", |
284 | 322 | "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')" |
290 | 328 | ] |
291 | 329 | }, |
292 | 330 | { |
|
316 | 354 | "N = 50\n", |
317 | 355 | "cmap = 'IceFire'\n", |
318 | 356 | "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", |
321 | 361 | "axs.format(suptitle='Parametric plots demo')\n", |
322 | | - "# Smooth gradations\n", |
| 362 | + "\n", |
| 363 | + "# Parametric line with smooth gradations\n", |
323 | 364 | "ax = axs[0]\n", |
324 | 365 | "state = np.random.RandomState(51423)\n", |
325 | 366 | "m = ax.plot((state.rand(N) - 0.5).cumsum(), state.rand(N),\n", |
326 | 367 | " 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", |
329 | 372 | "ax.format(xlim=(-1, 5), ylim=(-0.2, 1.2))\n", |
330 | 373 | "ax.colorbar(m, loc='b', label='parametric coordinate', locator=5)\n", |
331 | | - "# Step gradations\n", |
| 374 | + "\n", |
| 375 | + "# Parametric line with stepped gradations\n", |
332 | 376 | "N = 12\n", |
333 | 377 | "ax = axs[1]\n", |
334 | 378 | "values = np.linspace(-N/2, N/2, N + 1)\n", |
|
337 | 381 | "x = radii*np.cos(1.4*angles)\n", |
338 | 382 | "y = radii*np.sin(1.4*angles)\n", |
339 | 383 | "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", |
342 | 388 | "ax.colorbar(m, loc='b', maxn=10, label=f'parametric coordinate')" |
343 | 389 | ] |
344 | 390 | }, |
|
374 | 420 | "x = (state.rand(20)-0).cumsum()\n", |
375 | 421 | "data = (state.rand(20, 4)-0.5).cumsum(axis=0)\n", |
376 | 422 | "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", |
378 | 425 | "ax = axs[0]\n", |
379 | 426 | "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", |
382 | 433 | "ax = axs[1]\n", |
383 | 434 | "ax.format(title='Scatter plot with cmap')\n", |
384 | 435 | "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", |
388 | 441 | "axs.format(xlabel='xlabel', ylabel='ylabel')" |
389 | 442 | ] |
390 | 443 | } |
|
0 commit comments