Skip to content

Commit ffc049e

Browse files
committed
Update error bars example
1 parent 55448dd commit ffc049e

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

docs/1dplots.py

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
fig, axs = plot.subplots(ncols=2, share=False)
9595
x = np.linspace(-5, 5, N)
9696
y = state.rand(N, 5)
97-
axs.format(xlabel='xlabel', ylabel='ylabel', ymargin=0.05)
97+
axs.format(xlabel='xlabel', ylabel='ylabel')
9898
axs.format(suptitle='Standardized arguments demonstration')
9999

100100
# Plot by passing both x and y coordinates
@@ -182,65 +182,70 @@
182182
# %% [raw] raw_mimetype="text/restructuredtext"
183183
# .. _ug_errorbars:
184184
#
185-
# Adding error bars
186-
# -----------------
185+
# Error bars and shading
186+
# ----------------------
187187
#
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
190190
# `~matplotlib.axes.Axes.plot`, `~matplotlib.axes.Axes.scatter`,
191191
# `~matplotlib.axes.Axes.bar`, or `~matplotlib.axes.Axes.barh`.
192192
#
193193
# If you pass 2D arrays to these methods with ``means=True`` or
194194
# ``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.
202202

203-
# %%
204203
import proplot as plot
205204
import numpy as np
206205
import pandas as pd
207206
plot.rc['title.loc'] = 'uc'
207+
208+
# Generate sample data
208209
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
213216
fig, axs = plot.subplots(
214217
nrows=3, aspect=1.5, axwidth=4,
215218
share=0, hratios=(2, 1, 1)
216219
)
217-
axs.format(suptitle='Error bars with various plotting commands')
220+
axs.format(suptitle='Indicating error bounds with various plotting commands')
218221
axs[1:].format(xlabel='column number', xticks=1, xgrid=False)
219222

220-
# Asking add_errorbars to calculate bars
223+
# Automatically calculate medians and display default percentile range
221224
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+
)
223229
ax.format(title='Column statistics')
224230
ax.format(ylabel='column number', title='Bar plot', ygrid=False)
225231

226-
# Showing a standard deviation range instead of percentile range
232+
# Automatically calculate means and display requested standard deviation range
227233
ax = axs[1]
228234
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',
232237
)
233238
ax.format(title='Scatter plot')
234239

235-
# Supplying error bar data manually
240+
# Manually supply error bar data and legend labels
236241
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
239246
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',
244249
)
245250
ax.format(title='Line plot')
246251
plot.rc.reset()
@@ -280,7 +285,6 @@
280285
import numpy as np
281286
import pandas as pd
282287
plot.rc.titleloc = 'uc'
283-
plot.rc.margin = 0.05
284288
fig, axs = plot.subplots(nrows=2, aspect=2, axwidth=5, share=0, hratios=(3, 2))
285289
state = np.random.RandomState(51423)
286290
data = state.rand(5, 5).cumsum(axis=0).cumsum(axis=1)[:, ::-1]
@@ -367,14 +371,18 @@
367371
import proplot as plot
368372
import numpy as np
369373
import pandas as pd
374+
375+
# Generate sample data
370376
N = 500
371377
state = np.random.RandomState(51423)
372-
fig, axs = plot.subplots(ncols=2, axwidth=2.5)
373378
data = state.normal(size=(N, 5)) + 2 * (state.rand(N, 5) - 0.5) * np.arange(5)
374379
data = pd.DataFrame(
375380
data,
376381
columns=pd.Index(['a', 'b', 'c', 'd', 'e'], name='xlabel')
377382
)
383+
384+
# Generate figure
385+
fig, axs = plot.subplots(ncols=2, axwidth=2.5)
378386
axs.format(grid=False, suptitle='Boxes and violins demo')
379387

380388
# Box plots

0 commit comments

Comments
 (0)