diff --git a/examples/showcase/bachelors_degrees_by_gender.py b/examples/showcase/bachelors_degrees_by_gender.py index d3c124bc6d67..a67b6a029494 100644 --- a/examples/showcase/bachelors_degrees_by_gender.py +++ b/examples/showcase/bachelors_degrees_by_gender.py @@ -19,17 +19,18 @@ asfileobj=False) gender_degree_data = np.genfromtxt(fname, delimiter=',', names=True) -# These are the colors that will be used in the plot -color_sequence = ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c', - '#98df8a', '#d62728', '#ff9896', '#9467bd', '#c5b0d5', - '#8c564b', '#c49c94', '#e377c2', '#f7b6d2', '#7f7f7f', - '#c7c7c7', '#bcbd22', '#dbdb8d', '#17becf', '#9edae5'] - # You typically want your plot to be ~1.33x wider than tall. This plot # is a rare exception because of the number of lines being plotted on it. # Common sizes: (10, 7.5) and (12, 9) fig, ax = plt.subplots(1, 1, figsize=(12, 14)) +# These are the colors that will be used in the plot +ax.set_prop_cycle(color=[ + '#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c', '#98df8a', + '#d62728', '#ff9896', '#9467bd', '#c5b0d5', '#8c564b', '#c49c94', + '#e377c2', '#f7b6d2', '#7f7f7f', '#c7c7c7', '#bcbd22', '#dbdb8d', + '#17becf', '#9edae5']) + # Remove the plot frame lines. They are unnecessary here. ax.spines['top'].set_visible(False) ax.spines['bottom'].set_visible(False) @@ -47,22 +48,23 @@ ax.set_xlim(1969.5, 2011.1) ax.set_ylim(-0.25, 90) -# Make sure your axis ticks are large enough to be easily read. -# You don't want your viewers squinting to read your plot. -plt.xticks(range(1970, 2011, 10), fontsize=14) -plt.yticks(range(0, 91, 10), fontsize=14) +# Set a fixed location and format for ticks. +ax.set_xticks(range(1970, 2011, 10)) +ax.set_yticks(range(0, 91, 10)) ax.xaxis.set_major_formatter(plt.FuncFormatter('{:.0f}'.format)) ax.yaxis.set_major_formatter(plt.FuncFormatter('{:.0f}%'.format)) # Provide tick lines across the plot to help your viewers trace along # the axis ticks. Make sure that the lines are light and small so they # don't obscure the primary data lines. -plt.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3) +ax.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3) # Remove the tick marks; they are unnecessary with the tick lines we just -# plotted. -plt.tick_params(axis='both', which='both', bottom=False, top=False, - labelbottom=True, left=False, right=False, labelleft=True) +# plotted. Make sure your axis ticks are large enough to be easily read. +# You don't want your viewers squinting to read your plot. +ax.tick_params(axis='both', which='both', labelsize=14, + bottom=False, top=False, labelbottom=True, + left=False, right=False, labelleft=True) # Now that the plot is prepared, it's time to actually plot the data! # Note that I plotted the majors in order of the highest % in the final year. @@ -80,14 +82,12 @@ 'Math and Statistics': 0.75, 'Architecture': -0.75, 'Computer Science': 0.75, 'Engineering': -0.25} -for rank, column in enumerate(majors): +for column in majors: # Plot each line separately with its own color. column_rec_name = column.replace('\n', '_').replace(' ', '_') - line = plt.plot(gender_degree_data['Year'], - gender_degree_data[column_rec_name], - lw=2.5, - color=color_sequence[rank]) + line, = ax.plot('Year', column_rec_name, data=gender_degree_data, + lw=2.5) # Add a text label to the right end of every line. Most of the code below # is adding specific offsets y position because some labels overlapped. @@ -98,7 +98,7 @@ # Again, make sure that all labels are large enough to be easily read # by the viewer. - plt.text(2011.5, y_pos, column, fontsize=14, color=color_sequence[rank]) + ax.text(2011.5, y_pos, column, fontsize=14, color=line.get_color()) # Make the title big enough so it spans the entire plot, but don't make it # so big that it requires two lines to show. @@ -111,5 +111,5 @@ # Finally, save the figure as a PNG. # You can also save it as a PDF, JPEG, etc. # Just change the file extension in this call. -# plt.savefig('percent-bachelors-degrees-women-usa.png', bbox_inches='tight') +# fig.savefig('percent-bachelors-degrees-women-usa.png', bbox_inches='tight') plt.show() diff --git a/examples/showcase/integral.py b/examples/showcase/integral.py index f39174d03f64..f5ffbe05edff 100644 --- a/examples/showcase/integral.py +++ b/examples/showcase/integral.py @@ -26,8 +26,8 @@ def func(x): y = func(x) fig, ax = plt.subplots() -plt.plot(x, y, 'r', linewidth=2) -plt.ylim(ymin=0) +ax.plot(x, y, 'r', linewidth=2) +ax.set_ylim(bottom=0) # Make the shaded region ix = np.linspace(a, b) @@ -36,11 +36,11 @@ def func(x): poly = Polygon(verts, facecolor='0.9', edgecolor='0.5') ax.add_patch(poly) -plt.text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$", - horizontalalignment='center', fontsize=20) +ax.text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$", + horizontalalignment='center', fontsize=20) -plt.figtext(0.9, 0.05, '$x$') -plt.figtext(0.1, 0.9, '$y$') +fig.text(0.9, 0.05, '$x$') +fig.text(0.1, 0.9, '$y$') ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) diff --git a/examples/showcase/mandelbrot.py b/examples/showcase/mandelbrot.py index e8a4366afacd..3e19d9d644b9 100644 --- a/examples/showcase/mandelbrot.py +++ b/examples/showcase/mandelbrot.py @@ -61,7 +61,7 @@ def mandelbrot_set(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon=2.0): light = colors.LightSource(azdeg=315, altdeg=10) M = light.shade(M, cmap=plt.cm.hot, vert_exag=1.5, norm=colors.PowerNorm(0.3), blend_mode='hsv') - plt.imshow(M, extent=[xmin, xmax, ymin, ymax], interpolation="bicubic") + ax.imshow(M, extent=[xmin, xmax, ymin, ymax], interpolation="bicubic") ax.set_xticks([]) ax.set_yticks([]) diff --git a/examples/showcase/xkcd.py b/examples/showcase/xkcd.py index b7fdb56711b7..22fec6cfdc87 100644 --- a/examples/showcase/xkcd.py +++ b/examples/showcase/xkcd.py @@ -12,27 +12,27 @@ with plt.xkcd(): # Based on "Stove Ownership" from XKCD by Randall Munroe - # http://xkcd.com/418/ + # https://xkcd.com/418/ fig = plt.figure() ax = fig.add_axes((0.1, 0.2, 0.8, 0.7)) ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') - plt.xticks([]) - plt.yticks([]) + ax.set_xticks([]) + ax.set_yticks([]) ax.set_ylim([-30, 10]) data = np.ones(100) data[70:] -= np.arange(30) - plt.annotate( + ax.annotate( 'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED', xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10)) - plt.plot(data) + ax.plot(data) - plt.xlabel('time') - plt.ylabel('my overall health') + ax.set_xlabel('time') + ax.set_ylabel('my overall health') fig.text( 0.5, 0.05, '"Stove Ownership" from xkcd by Randall Munroe', @@ -42,7 +42,7 @@ with plt.xkcd(): # Based on "The Data So Far" from XKCD by Randall Munroe - # http://xkcd.com/373/ + # https://xkcd.com/373/ fig = plt.figure() ax = fig.add_axes((0.1, 0.2, 0.8, 0.7)) @@ -51,12 +51,12 @@ ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.set_xticks([0, 1]) + ax.set_xticklabels(['CONFIRMED BY\nEXPERIMENT', 'REFUTED BY\nEXPERIMENT']) ax.set_xlim([-0.5, 1.5]) + ax.set_yticks([]) ax.set_ylim([0, 110]) - ax.set_xticklabels(['CONFIRMED BY\nEXPERIMENT', 'REFUTED BY\nEXPERIMENT']) - plt.yticks([]) - plt.title("CLAIMS OF SUPERNATURAL POWERS") + ax.set_title("CLAIMS OF SUPERNATURAL POWERS") fig.text( 0.5, 0.05,