|
94 | 94 | fig, axs = plot.subplots(ncols=2, share=False) |
95 | 95 | x = np.linspace(-5, 5, N) |
96 | 96 | y = state.rand(N, 5) |
97 | | - axs.format(xlabel='xlabel', ylabel='ylabel', ymargin=0.05) |
| 97 | + axs.format(xlabel='xlabel', ylabel='ylabel') |
98 | 98 | axs.format(suptitle='Standardized arguments demonstration') |
99 | 99 |
|
100 | 100 | # Plot by passing both x and y coordinates |
|
182 | 182 | # %% [raw] raw_mimetype="text/restructuredtext" |
183 | 183 | # .. _ug_errorbars: |
184 | 184 | # |
185 | | -# Adding error bars |
186 | | -# ----------------- |
| 185 | +# Error bars and shading |
| 186 | +# ---------------------- |
187 | 187 | # |
188 | | -# The `~proplot.axes.add_errorbars` wrapper lets you draw error bars |
189 | | -# on-the-fly by passing certain keyword arguments to |
| 188 | +# The `~proplot.axes.indicate_error` wrapper lets you draw error bars |
| 189 | +# and error shading on-the-fly by passing certain keyword arguments to |
190 | 190 | # `~matplotlib.axes.Axes.plot`, `~matplotlib.axes.Axes.scatter`, |
191 | 191 | # `~matplotlib.axes.Axes.bar`, or `~matplotlib.axes.Axes.barh`. |
192 | 192 | # |
193 | 193 | # If you pass 2D arrays to these methods with ``means=True`` or |
194 | 194 | # ``medians=True``, the means or medians of each column are drawn as points, |
195 | | -# lines, or bars, and error bars are drawn to represent the spread in each |
196 | | -# column. `~proplot.axes.add_errorbars` lets you draw both thin error |
197 | | -# "bars" with optional whiskers, and thick error "boxes" overlayed on top of |
198 | | -# these bars (this can be used to represent different percentil ranges). |
199 | | -# Instead of using 2D arrays, you can also pass error bar coordinates |
200 | | -# *manually* with the `bardata` and `boxdata` keyword arguments. See |
201 | | -# `~proplot.axes.add_errorbars` for details. |
| 195 | +# lines, or bars, and error bars or shading is drawn to represent the spread in |
| 196 | +# each column. `~proplot.axes.indicate_error` lets you draw thin error |
| 197 | +# bars with optional whiskers, thick "boxes" overlayed on top of these bars |
| 198 | +# (think of this as a miniature boxplot), or up to 2 intervals of shading. |
| 199 | +# Instead of using 2D arrays, you can also pass the error bounds |
| 200 | +# *manually* with the `bardata`, `boxdata`, `shadedata`, and `fadedata` keyword |
| 201 | +# arguments. See `~proplot.axes.indicate_error` for details. |
202 | 202 |
|
203 | | -# %% |
204 | 203 | import proplot as plot |
205 | 204 | import numpy as np |
206 | 205 | import pandas as pd |
207 | 206 | plot.rc['title.loc'] = 'uc' |
| 207 | + |
| 208 | +# Generate sample data |
208 | 209 | state = np.random.RandomState(51423) |
209 | | -data = ( |
210 | | - state.rand(20, 8).cumsum(axis=0).cumsum(axis=1)[:, ::-1] |
211 | | - + 20 * state.normal(size=(20, 8)) + 30 |
212 | | -) |
| 210 | +data = state.rand(20, 8).cumsum(axis=0).cumsum(axis=1)[:, ::-1] |
| 211 | +data = data + 20 * state.normal(size=(20, 8)) + 30 |
| 212 | +data = pd.DataFrame(data, columns=np.arange(0, 16, 2)) |
| 213 | +data.name = 'variable' |
| 214 | + |
| 215 | +# Generate figure |
213 | 216 | fig, axs = plot.subplots( |
214 | 217 | nrows=3, aspect=1.5, axwidth=4, |
215 | 218 | share=0, hratios=(2, 1, 1) |
216 | 219 | ) |
217 | | -axs.format(suptitle='Error bars with various plotting commands') |
| 220 | +axs.format(suptitle='Indicating error bounds with various plotting commands') |
218 | 221 | axs[1:].format(xlabel='column number', xticks=1, xgrid=False) |
219 | 222 |
|
220 | | -# Asking add_errorbars to calculate bars |
| 223 | +# Automatically calculate medians and display default percentile range |
221 | 224 | ax = axs[0] |
222 | | -obj = ax.barh(data, color='red orange', means=True) |
| 225 | +obj = ax.barh( |
| 226 | + data, color='light red', legend=True, |
| 227 | + medians=True, barpctiles=True, boxpctiles=True |
| 228 | +) |
223 | 229 | ax.format(title='Column statistics') |
224 | 230 | ax.format(ylabel='column number', title='Bar plot', ygrid=False) |
225 | 231 |
|
226 | | -# Showing a standard deviation range instead of percentile range |
| 232 | +# Automatically calculate means and display requested standard deviation range |
227 | 233 | ax = axs[1] |
228 | 234 | ax.scatter( |
229 | | - data, color='k', marker='_', markersize=50, |
230 | | - medians=True, barstd=True, boxes=False, capsize=2, |
231 | | - barcolor='gray6', barrange=(-1, 1), barzorder=0, barlw=1, |
| 235 | + data, color='denim', marker='x', markersize=8**2, linewidth=0.8, |
| 236 | + means=True, shadestds=(-1, 1), legend='ll', |
232 | 237 | ) |
233 | 238 | ax.format(title='Scatter plot') |
234 | 239 |
|
235 | | -# Supplying error bar data manually |
| 240 | +# Manually supply error bar data and legend labels |
236 | 241 | ax = axs[2] |
237 | | -boxdata = np.percentile(data, (25, 75), axis=0) |
238 | | -bardata = np.percentile(data, (5, 95), axis=0) |
| 242 | +means = data.mean(axis=0) |
| 243 | +means.name = data.name |
| 244 | +shadedata = np.percentile(data, (25, 75), axis=0) # dark shading |
| 245 | +fadedata = np.percentile(data, (5, 95), axis=0) # light shading |
239 | 246 | ax.plot( |
240 | | - data.mean(axis=0), boxes=True, |
241 | | - edgecolor='k', color='gray9', |
242 | | - boxdata=boxdata, bardata=bardata, barzorder=0, |
243 | | - boxcolor='gray7', barcolor='gray7', boxmarker=False, |
| 247 | + means, shadedata=shadedata, fadedata=fadedata, |
| 248 | + color='ocean blue', barzorder=0, boxmarker=False, legend='ll', |
244 | 249 | ) |
245 | 250 | ax.format(title='Line plot') |
246 | 251 | plot.rc.reset() |
|
280 | 285 | import numpy as np |
281 | 286 | import pandas as pd |
282 | 287 | plot.rc.titleloc = 'uc' |
283 | | -plot.rc.margin = 0.05 |
284 | 288 | fig, axs = plot.subplots(nrows=2, aspect=2, axwidth=5, share=0, hratios=(3, 2)) |
285 | 289 | state = np.random.RandomState(51423) |
286 | 290 | data = state.rand(5, 5).cumsum(axis=0).cumsum(axis=1)[:, ::-1] |
|
367 | 371 | import proplot as plot |
368 | 372 | import numpy as np |
369 | 373 | import pandas as pd |
| 374 | + |
| 375 | +# Generate sample data |
370 | 376 | N = 500 |
371 | 377 | state = np.random.RandomState(51423) |
372 | | -fig, axs = plot.subplots(ncols=2, axwidth=2.5) |
373 | 378 | data = state.normal(size=(N, 5)) + 2 * (state.rand(N, 5) - 0.5) * np.arange(5) |
374 | 379 | data = pd.DataFrame( |
375 | 380 | data, |
376 | 381 | columns=pd.Index(['a', 'b', 'c', 'd', 'e'], name='xlabel') |
377 | 382 | ) |
| 383 | + |
| 384 | +# Generate figure |
| 385 | +fig, axs = plot.subplots(ncols=2, axwidth=2.5) |
378 | 386 | axs.format(grid=False, suptitle='Boxes and violins demo') |
379 | 387 |
|
380 | 388 | # Box plots |
|
0 commit comments