%matplotlib inline import numpy as np import matplotlib.pyplot as plt plt.rc('font', size=16) fig, ax = plt.subplots() ax.set_aspect('equal') ax.grid() ax.set_xlim([-1,7]) ax.set_ylim([-1,4]) ax.set_xticks(range(7)) ax.set_yticks(range(4)) ax.fill([0, 1, 1, 0], [0, 0, 1, 1], linewidth=3,facecolor='violet',edgecolor='k') ax.fill(np.array([0, 1, 1, 0]) + 1, np.array([0, 0, 1, 1]) + 1, linewidth=3,facecolor='darkviolet',edgecolor='k')
fill
, the lists with
- and -coordinates are converted to NumPy arrays so that we can
do computations on them - in this case add a constant.
for
-loop doing the job, but there is another practical
way, namely a function. Here is an example:
def plot_square(x,y,col): xs = np.array([0, 1, 1, 0]) ys = np.array([0, 0, 1, 1]) ax.fill(x + xs,y + ys,linewidth=3,facecolor=col,edgecolor='k')
def
declares that a function is specified,
where plot_square
is the name of the function in this
example, and where (x,y,col)
specify that three arguments must be
given when calling the function. The colon, :
, and the
indentation are part of the syntax. Everything indented will be executed
when the function is called. The indentation must be uniform
- typically four spacings.
plot_square(0,2,'lime') fig
plot_square(2,0,'yellow') plot_square(4,0,'orangered') plot_square(5,2,'chocolate') fig
fill
method on the Axel object
("method" is the accurate name for a function that is addressed
via an object) a new Polygon object is created and appears in the
figure. However, the calling of fill
actually also
returns some information. By storing the returned info in a variable
we can inspect it. So lets start over again, but now capturing the
output from ax.fill
in a variable fill_output
:
fig, ax = plt.subplots() ax.set_aspect('equal') ax.grid() ax.set_xlim([-1,7]) ax.set_ylim([-1,4]) ax.set_xticks(range(7)) ax.set_yticks(range(4)) xs = np.array([0, 1, 1, 0]) ys = np.array([0, 0, 1, 1]) fill_output = ax.fill(xs,ys,linewidth=3, facecolor='red',edgecolor='k') fill_output
[<matplotlib.patches.Polygon at 0x7fa82ef5a650>]
fill
is a list whose first (and only) element is the just-created Polygon object::
print('type(fill_output):',type(fill_output)) my_square = fill_output[0] print('type(my_square):',type(my_square)) print('my_square',my_square)
type(fill_output): <class 'list'> type(my_square): <class 'matplotlib.patches.Polygon'> my_square Polygon5((0, 0) ...)
my_square
above, we
may now modify the drawn square after the initial call of
fill
. Calling the set
method on my_square
:
my_square.set(facecolor='cadetblue') fig
fill
returns:
def plot_square(x,y,col): fill_output = ax.fill(x + xs, y + ys, linewidth=3, facecolor=col, edgecolor='k') # fill_output is a list # its first (and only) element is a Polygon object the_square = fill_output[0] return the_square
square1 = plot_square(0,2,'lightskyblue') square2 = plot_square(4,1,'dodgerblue') fig
print(square1) print(square2) square1.set(facecolor='darkorange') square2.set(facecolor='yellow') fig
Polygon5((0, 2) ...) Polygon5((4, 1) ...)
xy_vals = square1.get_xy() xy_vals
array([[0., 2.], [1., 2.], [1., 3.], [0., 3.], [0., 2.]])
new_xy_vals = xy_vals + [2,-1] new_xy_vals
array([[2., 1.], [3., 1.], [3., 2.], [2., 2.], [2., 1.]])
square1.update({'xy': new_xy_vals}) fig
update
method is a dictionary. The square moves accordingly:
def plot_square_more_compact(x,y,col): return ax.fill(x + xs, y + ys, linewidth=3, facecolor=col, edgecolor='k')[0] # note the [0] square3 = plot_square_more_compact(3,0,'pink') fig
Choose which booklet to go to: