8000 Merge pull request #21794 from jklymak/doc-basic-usage-fixes · matplotlib/matplotlib@761733d · GitHub
[go: up one dir, main page]

Skip to content

Commit 761733d

Browse files
dstansbyjklymak
authored andcommitted
Merge pull request #21794 from jklymak/doc-basic-usage-fixes
DOC: some minor fixes to the usage rewrite
1 parent 98a383f commit 761733d

File tree

2 files changed

+40
-41
lines changed

2 files changed

+40
-41
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ per-file-ignores =
100100
tutorials/introductory/images.py: E402, E501
101101
tutorials/introductory/pyplot.py: E402, E501
102102
tutorials/introductory/sample_plots.py: E501
103-
tutorials/introductory/usage.py: E501
103+
tutorials/introductory/usage.py: E703
104104
tutorials/text/annotations.py: E402, E501
105105
tutorials/text/text_intro.py: E402
106106
tutorials/text/text_props.py: E501

tutorials/introductory/usage.py

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# `.Axes.plot` to draw some data on the axes:
2626

2727
fig, ax = plt.subplots() # Create a figure containing a single axes.
28-
ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) # Plot some data on the axes.
28+
ax.plot([1, 2, 3, 4], [1, 4, 2, 3]); # Plot some data on the axes.
2929

3030
###############################################################################
3131
# .. _figure_parts:
@@ -123,14 +123,14 @@
123123
fig, ax = plt.subplots(figsize=(5, 2.7), constrained_layout=True)
124124
ax.scatter('a', 'b', c='c', s='d', data=data)
125125
ax.set_xlabel('entry a')
126-
ax.set_ylabel('entry b')
126+
ax.set_ylabel('entry b');
127127

128128
##############################################################################
129129
# .. _coding_styles:
130130
#
131131
# Coding styles
132132
# =============
133-
133+
#
134134
# The object-oriented and the pyplot interfaces
135135
# ---------------------------------------------
136136
#
@@ -153,7 +153,7 @@
153153
ax.set_xlabel('x label') # Add an x-label to the axes.
154154
ax.set_ylabel('y label') # Add a y-label to the axes.
155155
ax.set_title("Simple Plot") # Add a title to the axes.
156-
ax.legend() # Add a legend.
156+
ax.legend(); # Add a legend.
157157

158158
###############################################################################
159159
# or the pyplot-style:
@@ -167,19 +167,19 @@
167167
plt.xlabel('x label')
168168
plt.ylabel('y label')
169169
plt.title("Simple Plot")
170-
plt.legend()
170+
plt.legend();
171171

172172
###############################################################################
173173
# (In addition, there is a third approach, for the case when embedding
174174
# Matplotlib in a GUI application, which completely drops pyplot, even for
175-
# figure creation. See the corresponding section in the gallery for more info
176-
# (:ref:`user_interfaces`).)
175+
# figure creation. See the corresponding section in the gallery for more info:
176+
# :ref:`user_interfaces`.)
177177
#
178178
# Matplotlib's documentation and examples use both the OO and the pyplot
179179
# styles. In general, we suggest using the OO style, particularly for
180180
# complicated plots, and functions and scripts that are intended to be reused
181-
# as part of a larger project. However, the pyplot style can be very conveneient
182-
# for quick interactive work.
181+
# as part of a larger project. However, the pyplot style can be very
182+
# conveneient for quick interactive work.
183183
#
184184
# .. note::
185185
#
@@ -189,9 +189,9 @@
189189
# Making a helper functions
190190
# -------------------------
191191
#
192-
# If you need to make the same plots over and over again with different data sets,
193-
# or want to easily wrap Matplotlib methods, use the recommended signature function
194-
# below.
192+
# If you need to make the same plots over and over again with different data
193+
# sets, or want to easily wrap Matplotlib methods, use the recommended
194+
# signature function below.
195195

196196

197197
def my_plotter(ax, data1, data2, param_dict):
@@ -208,11 +208,9 @@ def my_plotter(ax, data1, data2, param_dict):
208208
xdata = np.arange(len(data1)) # make an ordinal for this
209209
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(5, 2.7))
210210
my_plotter(ax1, data1, data2, {'marker': 'x'})
211-
my_plotter(ax2, data3, data4, {'marker': 'o'})
211+
my_plotter(ax2, data3, data4, {'marker': 'o'});
212212

213213
###############################################################################
214-
# These examples provide convenience for more complex graphs.
215-
#
216214
# Note that if you want to install these as a python package, or any other
217215
# customizations you could use use one of the many templates on the web;
218216
# Matplotlib has one at `mpl-cookiecutter
@@ -232,7 +230,7 @@ def my_plotter(ax, data1, data2, param_dict):
232230
x = np.arange(len(data1))
233231
ax.plot(x, np.cumsum(data1), color='blue', linewidth=3, linestyle='--')
234232
l, = ax.plot(x, np.cumsum(data2), color='orange', linewidth=2)
235-
l.set_linestyle(':')
233+
l.set_linestyle(':');
236234

237235
###############################################################################
238236
# Colors
@@ -246,7 +244,7 @@ def my_plotter(ax, data1, data2, param_dict):
246244

247245
fig, ax = plt.subplots(figsize=(5, 2.7))
248246
x = np.arange(len(data1))
249-
ax.scatter(data1, data2, s=50, facecolor='C0', edgecolor='k')
247+
ax.scatter(data1, data2, s=50, facecolor='C0', edgecolor='k');
250248

251249
###############################################################################
252250
# Linewidths, linestyles, and markersizes
@@ -260,8 +258,8 @@ def my_plotter(ax, data1, data2, param_dict):
260258
# Marker size depends on the method being used. `~.Axes.plot` specifies
261259
# markersize in points, and is generally the "diameter" or width of the
262260
# marker. `~.Axes.scatter` specifies markersize as approximately
263-
# proportional to the visual area of the marker. There are also an array of
264-
# markerstyles available as string codes (see :mod:`~.matplotlib.markers`) or
261+
# proportional to the visual area of the marker. There is an array of
262+
# markerstyles available as string codes (see :mod:`~.matplotlib.markers`), or
265263
# users can define their own `~.MarkerStyle` (see
266264
# :doc:`/gallery/lines_bars_and_markers/marker_reference`):
267265

@@ -270,19 +268,19 @@ def my_plotter(ax, data1, data2, param_dict):
270268
ax.plot(data2, 'd', label='data2')
271269
ax.plot(data3, 'v', label='data3')
272270
ax.plot(data4, 's', label='data4')
273-
ax.legend()
271+
ax.legend();
274272

275-
################################################################################
273+
###############################################################################
276274
#
277275
# Labelling plots
278276
# ===============
279277
#
280278
# Axes labels and text
281279
# --------------------
282280
#
283-
# `~.Axes.set_xlabel`, `~.Axes.set_ylabel` and `~.Axes.set_title` are used to
284-
# add text in the indicated locations (see :doc:`/tutorials/text/text_intro` for
285-
# more discussion). Text can also be directly added to plots using
281+
# `~.Axes.set_xlabel`, `~.Axes.set_ylabel`, and `~.Axes.set_title` are used to
282+
# add text in the indicated locations (see :doc:`/tutorials/text/text_intro`
283+
# for more discussion). Text can also be directly added to plots using
286284
# `~.Axes.text`:
287285

288286
mu, sigma = 115, 15
@@ -296,8 +294,7 @@ def my_plotter(ax, data1, data2, param_dict):
296294
ax.set_title('Aardvark lengths\n (not really)')
297295
ax.text(75, .025, r'$\mu=115,\ \sigma=15$')
298296
ax.axis([55, 175, 0, 0.03])
299-
ax.grid(True)
300-
plt.show()
297+
ax.grid(True);
301298

302299
###############################################################################
303300
# All of the `~.Axes.text` functions return a `matplotlib.text.Text`
@@ -341,7 +338,7 @@ def my_plotter(ax, data1, data2, param_dict):
341338
ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
342339
arrowprops=dict(facecolor='black', shrink=0.05))
343340

344-
ax.set_ylim(-2, 2)
341+
ax.set_ylim(-2, 2);
345342

346343
###############################################################################
347344
# In this basic example, both *xy* and *xytext* are in data coordinates.
@@ -359,7 +356,7 @@ def my_plotter(ax, data1, data2, param_dict):
359356
ax.plot(np.arange(len(data1)), data1, label='data1')
360357
ax.plot(np.arange(len(data2)), data2, label='data2')
361358
ax.plot(np.arange(len(data3)), data3, 'd', label='data3')
362-
ax.legend()
359+
ax.legend();
363360

364361
##############################################################################
365362
# Legends in Matplotlib are quite flexible in layout, placement, and what
@@ -369,9 +366,9 @@ def my_plotter(ax, data1, data2, param_dict):
369366
# Axis scales and ticks
370367
# =====================
371368
#
372-
# Each Axes has two (or three) `~.axis.Axis` objects represnting the x- and y-axis.
373-
# These control the *scale* of the axis, the tick *Locators* and the tick
374-
# *Formatters*.
369+
# Each Axes has two (or three) `~.axis.Axis` objects represnting the x- and
370+
# y-axis. These control the *scale* of the axis, the tick *Locators* and the
371+
# tick *Formatters*.
375372
#
376373
# Scales
377374
# ------
@@ -388,7 +385,7 @@ def my_plotter(ax, data1, data2, param_dict):
388385
axs[0].plot(xdata, data)
389386

390387
axs[1].set_yscale('log')
391-
axs[1].plot(xdata, data)
388+
axs[1].plot(xdata, data);
392389

393390
##############################################################################
394391
# The scale sets the mapping from data values to spacing along the Axis. This
@@ -409,7 +406,7 @@ def my_plotter(ax, data1, data2, param_dict):
409406
axs[1].plot(xdata, data1)
410407
axs[1].set_xticks(np.arange(0, 100, 30), ['zero', '30', 'sixty', '90'])
411408
axs[1].set_yticks([-1.5, 0, 1.5]) # note that we don't need to specify labels
412-
axs[1].set_title('Manual ticks')
409+
axs[1].set_title('Manual ticks');
413410

414411
##############################################################################
415412
# Different scales can have different locators and formatters; for instance
@@ -421,15 +418,15 @@ def my_plotter(ax, data1, data2, param_dict):
421418
# Plotting dates and strings
422419
# --------------------------
423420
#
424-
# Matplotlib can handle plotting arrays of dates and arrays of strings as
421+
# Matplotlib can handle plotting arrays of dates and arrays of strings, as
425422
# well as floating point numbers. These get special locators and formatters
426423
# as appropriate. For dates:
427424

428425
fig, ax = plt.subplots(figsize=(5, 3.7), constrained_layout=True)
429426
dates = np.arange(np.datetime64('2021-11-15'), np.datetime64('2021-12-25'),
430427
np.timedelta64(1, 'h'))
431428
data = np.cumsum(np.random.randn(len(dates)))
432-
ax.plot(dates, data)
429+
ax.plot(dates, data);
433430

434431
##############################################################################
435432
# For more information see the date examples
@@ -441,13 +438,13 @@ def my_plotter(ax, data1, data2, param_dict):
441438
fig, ax = plt.subplots(figsize=(5, 2.7), constrained_layout=True)
442439
categories = ['turnips', 'rutabega', 'cucumber', 'pumpkins']
443440

444-
ax.bar(categories, np.random.rand(len(categories)))
441+
ax.bar(categories, np.random.rand(len(categories)));
445442

446443
##############################################################################
447444
# One caveat about categorical plotting is that some methods of parsing
448445
# text files return a list of strings, even if the strings all represent
449-
# numbers or dates. If you pass 1000 strings Matplotlib will think you
450-
# meant 1000 categories and will add 1000 ticks to your plot.
446+
# numbers or dates. If you pass 1000 strings, Matplotlib will think you
447+
# meant 1000 categories and will add 1000 ticks to your plot!
451448
#
452449
# Color mapped data
453450
# =================
@@ -474,7 +471,7 @@ def my_plotter(ax, data1, data2, param_dict):
474471

475472
pc = axs[1, 1].scatter(data1, data2, c=data3, cmap='RdBu_r')
476473
fig.colorbar(pc, ax=axs[1, 1], extend='both')
477-
axs[1, 1].set_title('scatter()')
474+
axs[1, 1].set_title('scatter()');
478475

479476
##############################################################################
480477
# Colormaps
@@ -507,7 +504,9 @@ def my_plotter(ax, data1, data2, param_dict):
507504
# :doc:`/gallery/subplots_axes_and_figures/colorbar_placement` for
508505
# details. You can also change the 63D3 appearance of colorbars with the
509506
# *extend* keyword to add arrows to the ends, and *shrink* and *aspect* to
510-
# control the size.
507+
# control the size. Finally, the colorbar will have default Locators
508+
# and Formatters appropriate to the Norm. These can be changed as for
509+
# other axis objects.
511510
#
512511
#
513512
# Working with multiple figures and axes

0 commit comments

Comments
 (0)
0