Skip to content

Commit a0d4adb

Browse files
committed
More sensible order for 1D plot examples and API docs
1 parent 00d5a86 commit a0d4adb

File tree

2 files changed

+181
-179
lines changed

2 files changed

+181
-179
lines changed

docs/1dplots.py

Lines changed: 178 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -195,81 +195,126 @@
195195

196196

197197
# %% [raw] raw_mimetype="text/restructuredtext"
198-
# .. _ug_errorbars:
198+
# .. _ug_lines:
199199
#
200-
# Shading and error bars
201-
# ----------------------
200+
# Line plots
201+
# ----------
202202
#
203-
# The `~proplot.axes.indicate_error` wrapper lets you draw error bars
204-
# and error shading on-the-fly by passing certain keyword arguments to
205-
# `~matplotlib.axes.Axes.plot`, `~matplotlib.axes.Axes.scatter`,
206-
# `~matplotlib.axes.Axes.bar`, or `~matplotlib.axes.Axes.barh`.
203+
# The `~matplotlib.axes.Axes.plot` command is wrapped by
204+
# `~proplot.axes.apply_cycle` and `~proplot.axes.standardize_1d`.
205+
# Its behavior is the same -- ProPlot simply tries to expand
206+
# the flexibility of this command to the rest of the 1D plotting commands.
207+
# The new `~proplot.axes.Axes.plotx` command can be used just like
208+
# `~matplotlib.axes.Axes.plotx`, except a single argument is interpreted
209+
# as *x* coordinates (with default *y* coordinates inferred from the data),
210+
# and multiple arguments are interpreted as *y* and *x* coordinates (in that order).
211+
# This is analogous to `~matplotlib.axes.Axes.barh` and
212+
# `~matplotlib.axes.Axes.fill_betweenx`.
207213
#
208-
# If you pass 2D arrays to these methods with ``mean=True`` or ``median=True``,
209-
# the means or medians of each column are drawn as points, lines, or bars, and
210-
# *error bars* or *shading* is drawn to represent the spread of the distribution
211-
# for each column. You can also specify the error bounds *manually* with the
212-
# `bardata`, `boxdata`, `shadedata`, and `fadedata` keywords.
213-
# `~proplot.axes.indicate_error` can draw thin error bars with optional whiskers,
214-
# thick "boxes" overlayed on top of these bars (think of these as miniature boxplots),
215-
# and up to 2 layers of shading. See `~proplot.axes.indicate_error` for details.
214+
# As with the other 1D plotting commands, `~matplotlib.axes.Axes.step`,
215+
# `~matplotlib.axes.Axes.hlines`, `~matplotlib.axes.Axes.vlines`, and
216+
# `~matplotlib.axes.Axes.stem` are wrapped by `~proplot.axes.standardize_1d`.
217+
# `~proplot.axes.Axes.step` now use the property cycle, just like
218+
# `~matplotlib.axes.Axes.plot`. `~matplotlib.axes.Axes.vlines` and
219+
# `~matplotlib.axes.Axes.hlines` are also wrapped by `~proplot.axes.vlines_extras`
220+
# and `~proplot.axes.hlines_extras`, which can use
221+
# different colors for "negative" and "positive" lines using ``negpos=True``
222+
# (the default colors are :rc:`negcolor` and :rc:`poscolor`).
223+
224+
# %%
225+
import proplot as plot
226+
import numpy as np
227+
state = np.random.RandomState(51423)
228+
fig, axs = plot.subplots(ncols=2, nrows=2, share=0)
229+
axs.format(suptitle='Line plots demo', xlabel='xlabel', ylabel='ylabel')
230+
231+
# Step
232+
ax = axs[0]
233+
data = state.rand(20, 4).cumsum(axis=1).cumsum(axis=0)
234+
cycle = ('blue7', 'gray5', 'red7', 'gray5')
235+
ax.step(data, cycle=cycle, labels=list('ABCD'), legend='ul', legend_kw={'ncol': 2})
236+
ax.format(title='Step plot')
237+
238+
# Stems
239+
ax = axs[1]
240+
data = state.rand(20)
241+
ax.stem(data, linefmt='k-')
242+
ax.format(title='Stem plot')
243+
244+
# Vertical lines
245+
gray = 'gray7'
246+
data = state.rand(20) - 0.5
247+
ax = axs[2]
248+
ax.area(data, color=gray, alpha=0.2)
249+
ax.vlines(data, negpos=True, linewidth=2)
250+
ax.format(title='Vertical lines')
251+
252+
# Horizontal lines
253+
ax = axs[3]
254+
ax.areax(data, color=gray, alpha=0.2)
255+
ax.hlines(data, negpos=True, linewidth=2)
256+
ax.format(title='Horizontal lines')
257+
216258

259+
# %% [raw] raw_mimetype="text/restructuredtext"
260+
# .. _ug_scatter:
261+
#
262+
# Scatter plots
263+
# -------------
264+
#
265+
# The `~matplotlib.axes.Axes.scatter` command is wrapped by
266+
# `~proplot.axes.scatter_extras`, `~proplot.axes.apply_cycle`, and
267+
# `~proplot.axes.standardize_1d`. This means that
268+
# `~matplotlib.axes.Axes.scatter` now accepts 2D *y* coordinates and permits
269+
# omitting *x* coordinates, just like `~matplotlib.axes.Axes.plot`.
270+
# `~matplotlib.axes.Axes.scatter` now also accepts keywords that look like
271+
# `~matplotlib.axes.Axes.plot` keywords (e.g., `color` instead of `c` and
272+
# `markersize` instead of `s`). This way, `~matplotlib.axes.Axes.scatter` can
273+
# optionally be used simply to "plot markers, not lines" without changing the
274+
# input arguments relative to `~matplotlib.axes.Axes.plot`.
275+
#
276+
# Just like `~matplotlib.axes.Axes.plot`, the property cycle is used
277+
# with `~matplotlib.axes.Axes.scatter` plots by default. It can be changed
278+
# using the `cycle` keyword argument, and it can include properties like `marker`
279+
# and `markersize`. The colormap `cmap` and normalizer `norm` used with the
280+
# optional `c` color array are now passed through the `~proplot.constructor.Colormap`
281+
# and `~proplot.constructor.Norm` constructor functions, and the the `s` marker
282+
# size array can now be conveniently scaled using the arguments `smin` and `smax`
283+
# (analogous to `vmin` and `vmax` used for colors).
217284

218285
# %%
219286
import proplot as plot
220287
import numpy as np
221288
import pandas as pd
222-
plot.rc['title.loc'] = 'uc'
223289

224290
# Sample data
225291
state = np.random.RandomState(51423)
226-
data = state.rand(20, 8).cumsum(axis=0).cumsum(axis=1)[:, ::-1]
227-
data = data + 20 * state.normal(size=(20, 8)) + 30
228-
data = pd.DataFrame(data, columns=np.arange(0, 16, 2))
229-
data.name = 'variable'
292+
x = (state.rand(20) - 0).cumsum()
293+
data = (state.rand(20, 4) - 0.5).cumsum(axis=0)
294+
data = pd.DataFrame(data, columns=pd.Index(['a', 'b', 'c', 'd'], name='label'))
230295

231296
# Figure
232-
fig, axs = plot.subplots(
233-
nrows=3, refaspect=1.5, refwidth=4,
234-
share=0, hratios=(2, 1, 1)
235-
)
236-
axs.format(suptitle='Indicating error bounds')
237-
axs[1:].format(xlabel='column number', xticks=1, xgrid=False)
297+
fig, axs = plot.subplots(ncols=2, share=1)
298+
axs.format(suptitle='Scatter plot demo')
238299

239-
# Medians and percentile ranges
300+
# Scatter plot with property cycler
240301
ax = axs[0]
241-
obj = ax.barh(
242-
data, color='light red', legend=True,
243-
median=True, barpctile=90, boxpctile=True,
244-
# median=True, barpctile=(5, 95), boxpctile=(25, 75) # equivalent
302+
ax.format(title='With property cycle')
303+
obj = ax.scatter(
304+
x, data, legend='ul', cycle='Set2', legend_kw={'ncols': 2},
305+
cycle_kw={'marker': ['x', 'o', 'x', 'o'], 'markersize': [5, 10, 20, 30]}
245306
)
246-
ax.format(title='Column statistics')
247-
ax.format(ylabel='column number', title='Bar plot', ygrid=False)
248307

249-
# Means and standard deviation range
308+
# Scatter plot with colormap
250309
ax = axs[1]
251-
ax.scatter(
252-
data, color='denim', marker='x', markersize=8**2, linewidth=0.8, legend='ll',
253-
label='mean', shadelabel=True,
254-
mean=True, shadestd=1,
255-
# mean=True, shadestd=(-1, 1) # equivalent
256-
)
257-
ax.format(title='Marker plot')
258-
259-
# User-defined error bars
260-
ax = axs[2]
261-
means = data.mean(axis=0)
262-
means.name = data.name
263-
shadedata = np.percentile(data, (25, 75), axis=0) # dark shading
264-
fadedata = np.percentile(data, (5, 95), axis=0) # light shading
265-
ax.plot(
266-
means,
267-
shadedata=shadedata, fadedata=fadedata,
268-
label='mean', shadelabel='50% CI', fadelabel='90% CI',
269-
color='ocean blue', barzorder=0, boxmarker=False, legend='ll',
310+
ax.format(title='With colormap')
311+
data = state.rand(2, 100)
312+
obj = ax.scatter(
313+
*data, color=data.sum(axis=0), size=state.rand(100), smin=3, smax=30,
314+
marker='o', cmap='dark red', cmap_kw={'fade': 90}, vmin=0, vmax=2,
315+
colorbar='lr', colorbar_kw={'label': 'label', 'locator': 0.5},
270316
)
271-
ax.format(title='Line plot')
272-
plot.rc.reset()
317+
axs.format(xlabel='xlabel', ylabel='ylabel')
273318

274319

275320
# %% [raw] raw_mimetype="text/restructuredtext"
@@ -392,6 +437,84 @@
392437
axs[1].format(title='Area plot')
393438

394439

440+
# %% [raw] raw_mimetype="text/restructuredtext"
441+
# .. _ug_errorbars:
442+
#
443+
# Shading and error bars
444+
# ----------------------
445+
#
446+
# The `~proplot.axes.indicate_error` wrapper lets you draw error bars
447+
# and error shading on-the-fly by passing certain keyword arguments to
448+
# `~matplotlib.axes.Axes.plot`, `~matplotlib.axes.Axes.scatter`,
449+
# `~matplotlib.axes.Axes.bar`, or `~matplotlib.axes.Axes.barh`.
450+
#
451+
# If you pass 2D arrays to these methods with ``mean=True`` or ``median=True``,
452+
# the means or medians of each column are drawn as points, lines, or bars, and
453+
# *error bars* or *shading* is drawn to represent the spread of the distribution
454+
# for each column. You can also specify the error bounds *manually* with the
455+
# `bardata`, `boxdata`, `shadedata`, and `fadedata` keywords.
456+
# `~proplot.axes.indicate_error` can draw thin error bars with optional whiskers,
457+
# thick "boxes" overlayed on top of these bars (think of these as miniature boxplots),
458+
# and up to 2 layers of shading. See `~proplot.axes.indicate_error` for details.
459+
460+
461+
# %%
462+
import proplot as plot
463+
import numpy as np
464+
import pandas as pd
465+
plot.rc['title.loc'] = 'uc'
466+
467+
# Sample data
468+
state = np.random.RandomState(51423)
469+
data = state.rand(20, 8).cumsum(axis=0).cumsum(axis=1)[:, ::-1]
470+
data = data + 20 * state.normal(size=(20, 8)) + 30
471+
data = pd.DataFrame(data, columns=np.arange(0, 16, 2))
472+
data.name = 'variable'
473+
474+
# Figure
475+
fig, axs = plot.subplots(
476+
nrows=3, refaspect=1.5, refwidth=4,
477+
share=0, hratios=(2, 1, 1)
478+
)
479+
axs.format(suptitle='Indicating error bounds')
480+
axs[1:].format(xlabel='column number', xticks=1, xgrid=False)
481+
482+
# Medians and percentile ranges
483+
ax = axs[0]
484+
obj = ax.barh(
485+
data, color='light red', legend=True,
486+
median=True, barpctile=90, boxpctile=True,
487+
# median=True, barpctile=(5, 95), boxpctile=(25, 75) # equivalent
488+
)
489+
ax.format(title='Column statistics')
490+
ax.format(ylabel='column number', title='Bar plot', ygrid=False)
491+
492+
# Means and standard deviation range
493+
ax = axs[1]
494+
ax.scatter(
495+
data, color='denim', marker='x', markersize=8**2, linewidth=0.8, legend='ll',
496+
label='mean', shadelabel=True,
497+
mean=True, shadestd=1,
498+
# mean=True, shadestd=(-1, 1) # equivalent
499+
)
500+
ax.format(title='Marker plot')
501+
502+
# User-defined error bars
503+
ax = axs[2]
504+
means = data.mean(axis=0)
505+
means.name = data.name
506+
shadedata = np.percentile(data, (25, 75), axis=0) # dark shading
507+
fadedata = np.percentile(data, (5, 95), axis=0) # light shading
508+
ax.plot(
509+
means,
510+
shadedata=shadedata, fadedata=fadedata,
511+
label='mean', shadelabel='50% CI', fadelabel='90% CI',
512+
color='ocean blue', barzorder=0, boxmarker=False, legend='ll',
513+
)
514+
ax.format(title='Line plot')
515+
plot.rc.reset()
516+
517+
395518
# %% [raw] raw_mimetype="text/restructuredtext"
396519
# .. _ug_boxplots:
397520
#
@@ -448,127 +571,6 @@
448571
ax.format(title='Multiple colors', titleloc='uc', ymargin=0.15)
449572

450573

451-
# %% [raw] raw_mimetype="text/restructuredtext"
452-
# .. _ug_lines:
453-
#
454-
# Line plots
455-
# ----------
456-
#
457-
# The `~matplotlib.axes.Axes.plot` command is wrapped by
458-
# `~proplot.axes.apply_cycle` and `~proplot.axes.standardize_1d`.
459-
# But in general, its behavior is the same -- ProPlot simply tries to expand
460-
# the flexibility of this command to the rest of the 1D plotting commands.
461-
# The new `~proplot.axes.Axes.plotx` command can be used just like
462-
# `~matplotlib.axes.Axes.plotx`, except a single argument is interpreted
463-
# as *x* coordinates (with default *y* coordinates inferred from the data),
464-
# and multiple arguments are interpreted as *y* and *x* coordinates (in that order).
465-
# This is analogous to `~matplotlib.axes.Axes.barh` and
466-
# `~matplotlib.axes.Axes.fill_betweenx`.
467-
#
468-
# As with the other 1D plotting commands, `~matplotlib.axes.Axes.step`,
469-
# `~matplotlib.axes.Axes.hlines`, `~matplotlib.axes.Axes.vlines`, and
470-
# `~matplotlib.axes.Axes.stem` are wrapped by `~proplot.axes.standardize_1d`.
471-
# `~proplot.axes.Axes.step` now use the property cycle, just like
472-
# `~matplotlib.axes.Axes.plot`. `~matplotlib.axes.Axes.vlines` and
473-
# `~matplotlib.axes.Axes.hlines` are also wrapped by `~proplot.axes.vlines_extras`
474-
# and `~proplot.axes.hlines_extras`, which can use
475-
# different colors for "negative" and "positive" lines using ``negpos=True``
476-
# (the default colors are :rc:`negcolor` and :rc:`poscolor`).
477-
478-
# %%
479-
import proplot as plot
480-
import numpy as np
481-
state = np.random.RandomState(51423)
482-
fig, axs = plot.subplots(ncols=2, nrows=2, share=0)
483-
axs.format(suptitle='Line plots demo', xlabel='xlabel', ylabel='ylabel')
484-
485-
# Step
486-
ax = axs[0]
487-
data = state.rand(20, 4).cumsum(axis=1).cumsum(axis=0)
488-
cycle = ('blue7', 'gray5', 'red7', 'gray5')
489-
ax.step(data, cycle=cycle, labels=list('ABCD'), legend='ul', legend_kw={'ncol': 2})
490-
ax.format(title='Step plot')
491-
492-
# Stems
493-
ax = axs[1]
494-
data = state.rand(20)
495-
ax.stem(data, linefmt='k-')
496-
ax.format(title='Stem plot')
497-
498-
# Vertical lines
499-
gray = 'gray7'
500-
data = state.rand(20) - 0.5
501-
ax = axs[2]
502-
ax.area(data, color=gray, alpha=0.2)
503-
ax.vlines(data, negpos=True, linewidth=2)
504-
ax.format(title='Vertical lines')
505-
506-
# Horizontal lines
507-
ax = axs[3]
508-
ax.areax(data, color=gray, alpha=0.2)
509-
ax.hlines(data, negpos=True, linewidth=2)
510-
ax.format(title='Horizontal lines')
511-
512-
# %% [raw] raw_mimetype="text/restructuredtext"
513-
# .. _ug_scatter:
514-
#
515-
# Scatter plots
516-
# -------------
517-
#
518-
# The `~matplotlib.axes.Axes.scatter` command is wrapped by
519-
# `~proplot.axes.scatter_extras`, `~proplot.axes.apply_cycle`, and
520-
# `~proplot.axes.standardize_1d`. This means that
521-
# `~matplotlib.axes.Axes.scatter` now accepts 2D *y* coordinates and permits
522-
# omitting *x* coordinates, just like `~matplotlib.axes.Axes.plot`.
523-
# `~matplotlib.axes.Axes.scatter` now also accepts keywords that look like
524-
# `~matplotlib.axes.Axes.plot` keywords (e.g., `color` instead of `c` and
525-
# `markersize` instead of `s`). This way, `~matplotlib.axes.Axes.scatter` can
526-
# optionally be used simply to "plot markers, not lines" without changing the
527-
# input arguments relative to `~matplotlib.axes.Axes.plot`.
528-
#
529-
# Just like `~matplotlib.axes.Axes.plot`, the property cycle is used
530-
# with `~matplotlib.axes.Axes.scatter` plots by default. It can be changed
531-
# using the `cycle` keyword argument, and it can include properties like `marker`
532-
# and `markersize`. The colormap `cmap` and normalizer `norm` used with the
533-
# optional `c` color array are now passed through the `~proplot.constructor.Colormap`
534-
# and `~proplot.constructor.Norm` constructor functions, and the the `s` marker
535-
# size array can now be conveniently scaled using the arguments `smin` and `smax`
536-
# (analogous to `vmin` and `vmax` used for colors).
537-
538-
# %%
539-
import proplot as plot
540-
import numpy as np
541-
import pandas as pd
542-
543-
# Sample data
544-
state = np.random.RandomState(51423)
545-
x = (state.rand(20) - 0).cumsum()
546-
data = (state.rand(20, 4) - 0.5).cumsum(axis=0)
547-
data = pd.DataFrame(data, columns=pd.Index(['a', 'b', 'c', 'd'], name='label'))
548-
549-
# Figure
550-
fig, axs = plot.subplots(ncols=2, share=1)
551-
axs.format(suptitle='Scatter plot demo')
552-
553-
# Scatter plot with property cycler
554-
ax = axs[0]
555-
ax.format(title='With property cycle')
556-
obj = ax.scatter(
557-
x, data, legend='ul', cycle='Set2', legend_kw={'ncols': 2},
558-
cycle_kw={'marker': ['x', 'o', 'x', 'o'], 'markersize': [5, 10, 20, 30]}
559-
)
560-
561-
# Scatter plot with colormap
562-
ax = axs[1]
563-
ax.format(title='With colormap')
564-
data = state.rand(2, 100)
565-
obj = ax.scatter(
566-
*data, color=data.sum(axis=0), size=state.rand(100), smin=3, smax=30,
567-
marker='o', cmap='dark red', cmap_kw={'fade': 90}, vmin=0, vmax=2,
568-
colorbar='lr', colorbar_kw={'label': 'label', 'locator': 0.5},
569-
)
570-
axs.format(xlabel='xlabel', ylabel='ylabel')
571-
572574
# %% [raw] raw_mimetype="text/restructuredtext"
573575
# .. _ug_parametric:
574576
#

0 commit comments

Comments
 (0)