From 50fa443a25517762a41d7b2ab35429101974d64d Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Mon, 12 Jul 2021 23:09:24 +0200 Subject: [PATCH] Use set_xticks(ticks, labels) instead of a separate set_xticklabels() --- .../make_room_for_ylabel_using_axesgrid.py | 9 +++------ examples/axes_grid1/simple_axisline4.py | 6 +++--- .../axisartist/demo_ticklabel_alignment.py | 6 ++---- .../image_annotated_heatmap.py | 18 ++++++------------ .../lines_bars_and_markers/bar_label_demo.py | 9 +++------ examples/lines_bars_and_markers/barchart.py | 3 +-- examples/lines_bars_and_markers/barh.py | 3 +-- examples/lines_bars_and_markers/broken_barh.py | 3 +-- examples/lines_bars_and_markers/hat_graph.py | 3 +-- examples/pyplots/auto_subplots_adjust.py | 5 ++--- examples/scales/log_bar.py | 3 +-- examples/showcase/integral.py | 3 +-- examples/specialty_plots/mri_with_eeg.py | 3 +-- examples/statistics/barchart_demo.py | 8 +++----- examples/statistics/boxplot_vs_violin.py | 6 ++---- examples/statistics/customized_violin.py | 3 +-- .../multiple_histograms_side_by_side.py | 3 +-- examples/style_sheets/ggplot.py | 3 +-- .../style_sheets/style_sheets_reference.py | 3 +-- .../text_labels_and_annotations/multiline.py | 6 +++--- examples/ticks_and_spines/spines_bounds.py | 3 +-- examples/units/bar_unit_demo.py | 3 +-- tutorials/toolkits/axes_grid.py | 6 +++--- 23 files changed, 43 insertions(+), 75 deletions(-) diff --git a/examples/axes_grid1/make_room_for_ylabel_using_axesgrid.py b/examples/axes_grid1/make_room_for_ylabel_using_axesgrid.py index af5946535cfb..b6ce60531741 100644 --- a/examples/axes_grid1/make_room_for_ylabel_using_axesgrid.py +++ b/examples/axes_grid1/make_room_for_ylabel_using_axesgrid.py @@ -14,8 +14,7 @@ plt.figure() ax = plt.axes([0, 0, 1, 1]) -ax.set_yticks([0.5]) -ax.set_yticklabels(["very long label"]) +ax.set_yticks([0.5], labels=["very long label"]) make_axes_area_auto_adjustable(ax) @@ -26,8 +25,7 @@ ax1 = plt.axes([0, 0, 1, 0.5]) ax2 = plt.axes([0, 0.5, 1, 0.5]) -ax1.set_yticks([0.5]) -ax1.set_yticklabels(["very long label"]) +ax1.set_yticks([0.5], labels=["very long label"]) ax1.set_ylabel("Y label") ax2.set_title("Title") @@ -53,8 +51,7 @@ divider.add_auto_adjustable_area(use_axes=[ax1, ax2], pad=0.1, adjust_dirs=["top", "bottom"]) -ax1.set_yticks([0.5]) -ax1.set_yticklabels(["very long label"]) +ax1.set_yticks([0.5], labels=["very long label"]) ax2.set_title("Title") ax2.set_xlabel("X - Label") diff --git a/examples/axes_grid1/simple_axisline4.py b/examples/axes_grid1/simple_axisline4.py index 91b76cf3e956..4d93beb2b0d0 100644 --- a/examples/axes_grid1/simple_axisline4.py +++ b/examples/axes_grid1/simple_axisline4.py @@ -13,9 +13,9 @@ ax.plot(xx, np.sin(xx)) ax2 = ax.twin() # ax2 is responsible for "top" axis and "right" axis -ax2.set_xticks([0., .5*np.pi, np.pi, 1.5*np.pi, 2*np.pi]) -ax2.set_xticklabels(["$0$", r"$\frac{1}{2}\pi$", - r"$\pi$", r"$\frac{3}{2}\pi$", r"$2\pi$"]) +ax2.set_xticks([0., .5*np.pi, np.pi, 1.5*np.pi, 2*np.pi], + labels=["$0$", r"$\frac{1}{2}\pi$", + r"$\pi$", r"$\frac{3}{2}\pi$", r"$2\pi$"]) ax2.axis["right"].major_ticklabels.set_visible(False) ax2.axis["top"].major_ticklabels.set_visible(True) diff --git a/examples/axisartist/demo_ticklabel_alignment.py b/examples/axisartist/demo_ticklabel_alignment.py index 928b3c71a64d..e6d2d0c6ae59 100644 --- a/examples/axisartist/demo_ticklabel_alignment.py +++ b/examples/axisartist/demo_ticklabel_alignment.py @@ -12,10 +12,8 @@ def setup_axes(fig, pos): ax = fig.add_subplot(pos, axes_class=axisartist.Axes) - ax.set_yticks([0.2, 0.8]) - ax.set_yticklabels(["short", "loooong"]) - ax.set_xticks([0.2, 0.8]) - ax.set_xticklabels([r"$\frac{1}{2}\pi$", r"$\pi$"]) + ax.set_yticks([0.2, 0.8], labels=["short", "loooong"]) + ax.set_xticks([0.2, 0.8], labels=[r"$\frac{1}{2}\pi$", r"$\pi$"]) return ax diff --git a/examples/images_contours_and_fields/image_annotated_heatmap.py b/examples/images_contours_and_fields/image_annotated_heatmap.py index 062cd81ab39f..5bcbb57a5e8f 100644 --- a/examples/images_contours_and_fields/image_annotated_heatmap.py +++ b/examples/images_contours_and_fields/image_annotated_heatmap.py @@ -59,12 +59,9 @@ fig, ax = plt.subplots() im = ax.imshow(harvest) -# We want to show all ticks... -ax.set_xticks(np.arange(len(farmers))) -ax.set_yticks(np.arange(len(vegetables))) -# ... and label them with the respective list entries -ax.set_xticklabels(farmers) -ax.set_yticklabels(vegetables) +# Show all ticks and label them with the respective list entries +ax.set_xticks(np.arange(len(farmers)), labels=farmers) +ax.set_yticks(np.arange(len(vegetables)), labels=vegetables) # Rotate the tick labels and set their alignment. plt.setp(ax.get_xticklabels(), rotation=45, ha="right", @@ -133,12 +130,9 @@ def heatmap(data, row_labels, col_labels, ax=None, cbar = ax.figure.colorbar(im, ax=ax, **cbar_kw) cbar.ax.set_ylabel(cbarlabel, rotation=-90, va="bottom") - # We want to show all ticks... - ax.set_xticks(np.arange(data.shape[1])) - ax.set_yticks(np.arange(data.shape[0])) - # ... and label them with the respective list entries. - ax.set_xticklabels(col_labels) - ax.set_yticklabels(row_labels) + # Show all ticks and label them with the respective list entries. + ax.set_xticks(np.arange(data.shape[1]), labels=col_labels) + ax.set_yticks(np.arange(data.shape[0]), labels=row_labels) # Let the horizontal axes labeling appear on top. ax.tick_params(top=True, bottom=False, diff --git a/examples/lines_bars_and_markers/bar_label_demo.py b/examples/lines_bars_and_markers/bar_label_demo.py index 9b353376a53e..3ed3f79018cc 100644 --- a/examples/lines_bars_and_markers/bar_label_demo.py +++ b/examples/lines_bars_and_markers/bar_label_demo.py @@ -40,8 +40,7 @@ ax.axhline(0, color='grey', linewidth=0.8) ax.set_ylabel('Scores') ax.set_title('Scores by group and gender') -ax.set_xticks(ind) -ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5')) +ax.set_xticks(ind, labels=['G1', 'G2', 'G3', 'G4', 'G5']) ax.legend() # Label with label_type 'center' instead of the default 'edge' @@ -66,8 +65,7 @@ fig, ax = plt.subplots() hbars = ax.barh(y_pos, performance, xerr=error, align='center') -ax.set_yticks(y_pos) -ax.set_yticklabels(people) +ax.set_yticks(y_pos, labels=people) ax.invert_yaxis() # labels read top-to-bottom ax.set_xlabel('Performance') ax.set_title('How fast do you want to go today?') @@ -84,8 +82,7 @@ fig, ax = plt.subplots() hbars = ax.barh(y_pos, performance, xerr=error, align='center') -ax.set_yticks(y_pos) -ax.set_yticklabels(people) +ax.set_yticks(y_pos, labels=people) ax.invert_yaxis() # labels read top-to-bottom ax.set_xlabel('Performance') ax.set_title('How fast do you want to go today?') diff --git a/examples/lines_bars_and_markers/barchart.py b/examples/lines_bars_and_markers/barchart.py index 71ae2ca0f8f3..8e2dc9f1ed44 100644 --- a/examples/lines_bars_and_markers/barchart.py +++ b/examples/lines_bars_and_markers/barchart.py @@ -25,8 +25,7 @@ # Add some text for labels, title and custom x-axis tick labels, etc. ax.set_ylabel('Scores') ax.set_title('Scores by group and gender') -ax.set_xticks(x) -ax.set_xticklabels(labels) +ax.set_xticks(x, labels) ax.legend() ax.bar_label(rects1, padding=3) diff --git a/examples/lines_bars_and_markers/barh.py b/examples/lines_bars_and_markers/barh.py index c537c0d9b215..64d5a5137906 100644 --- a/examples/lines_bars_and_markers/barh.py +++ b/examples/lines_bars_and_markers/barh.py @@ -22,8 +22,7 @@ error = np.random.rand(len(people)) ax.barh(y_pos, performance, xerr=error, align='center') -ax.set_yticks(y_pos) -ax.set_yticklabels(people) +ax.set_yticks(y_pos, labels=people) ax.invert_yaxis() # labels read top-to-bottom ax.set_xlabel('Performance') ax.set_title('How fast do you want to go today?') diff --git a/examples/lines_bars_and_markers/broken_barh.py b/examples/lines_bars_and_markers/broken_barh.py index c0691beaf255..084e0aae9ecb 100644 --- a/examples/lines_bars_and_markers/broken_barh.py +++ b/examples/lines_bars_and_markers/broken_barh.py @@ -14,8 +14,7 @@ ax.set_ylim(5, 35) ax.set_xlim(0, 200) ax.set_xlabel('seconds since start') -ax.set_yticks([15, 25]) -ax.set_yticklabels(['Bill', 'Jim']) +ax.set_yticks([15, 25], labels=['Bill', 'Jim']) ax.grid(True) ax.annotate('race interrupted', (61, 25), xytext=(0.8, 0.9), textcoords='axes fraction', diff --git a/examples/lines_bars_and_markers/hat_graph.py b/examples/lines_bars_and_markers/hat_graph.py index f7658c97b036..6c939167b536 100644 --- a/examples/lines_bars_and_markers/hat_graph.py +++ b/examples/lines_bars_and_markers/hat_graph.py @@ -40,8 +40,7 @@ def label_bars(heights, rects): values = np.asarray(values) x = np.arange(values.shape[1]) - ax.set_xticks(x) - ax.set_xticklabels(xlabels) + ax.set_xticks(x, labels=xlabels) spacing = 0.3 # spacing between hat groups width = (1 - spacing) / values.shape[0] heights0 = values[0] diff --git a/examples/pyplots/auto_subplots_adjust.py b/examples/pyplots/auto_subplots_adjust.py index 88b8e11e2f3e..b4c731cb4cdc 100644 --- a/examples/pyplots/auto_subplots_adjust.py +++ b/examples/pyplots/auto_subplots_adjust.py @@ -43,13 +43,12 @@ fig, ax = plt.subplots() ax.plot(range(10)) -ax.set_yticks((2, 5, 7)) -labels = ax.set_yticklabels(('really, really, really', 'long', 'labels')) +ax.set_yticks([2, 5, 7], labels=['really, really, really', 'long', 'labels']) def on_draw(event): bboxes = [] - for label in labels: + for label in ax.get_yticklabels(): # Bounding box in pixels bbox_px = label.get_window_extent() # Transform to relative figure coordinates. This is the inverse of diff --git a/examples/scales/log_bar.py b/examples/scales/log_bar.py index bcbdee2a7f98..239069806c5c 100644 --- a/examples/scales/log_bar.py +++ b/examples/scales/log_bar.py @@ -20,8 +20,7 @@ y = [d[i] for d in data] b = ax.bar(x + i * dimw, y, dimw, bottom=0.001) -ax.set_xticks(x + dimw / 2) -ax.set_xticklabels(map(str, x)) +ax.set_xticks(x + dimw / 2, labels=map(str, x)) ax.set_yscale('log') ax.set_xlabel('x') diff --git a/examples/showcase/integral.py b/examples/showcase/integral.py index e0d07fec3de2..c75ef940b0db 100644 --- a/examples/showcase/integral.py +++ b/examples/showcase/integral.py @@ -46,8 +46,7 @@ def func(x): ax.spines.top.set_visible(False) ax.xaxis.set_ticks_position('bottom') -ax.set_xticks((a, b)) -ax.set_xticklabels(('$a$', '$b$')) +ax.set_xticks([a, b], labels=['$a$', '$b$']) ax.set_yticks([]) plt.show() diff --git a/examples/specialty_plots/mri_with_eeg.py b/examples/specialty_plots/mri_with_eeg.py index 0f622bf42a67..91bcd0e232f6 100644 --- a/examples/specialty_plots/mri_with_eeg.py +++ b/examples/specialty_plots/mri_with_eeg.py @@ -68,8 +68,7 @@ ax2.add_collection(lines) # Set the yticks to use axes coordinates on the y axis -ax2.set_yticks(ticklocs) -ax2.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9']) +ax2.set_yticks(ticklocs, labels=['PG3', 'PG5', 'PG7', 'PG9']) ax2.set_xlabel('Time (s)') diff --git a/examples/statistics/barchart_demo.py b/examples/statistics/barchart_demo.py index 8c549b0cdb70..f3922140b558 100644 --- a/examples/statistics/barchart_demo.py +++ b/examples/statistics/barchart_demo.py @@ -88,14 +88,12 @@ def plot_student_results(student, scores, cohort_size): # Set the right-hand Y-axis ticks and labels ax2 = ax1.twinx() - # Set the tick locations - ax2.set_yticks(pos) + # Set the tick locations and labels + ax2.set_yticks( + pos, labels=[format_score(scores[k].score, k) for k in test_names]) # Set equal limits on both yaxis so that the ticks line up ax2.set_ylim(ax1.get_ylim()) - # Set the tick labels - ax2.set_yticklabels([format_score(scores[k].score, k) for k in test_names]) - ax2.set_ylabel('Test Scores') xlabel = ('Percentile Ranking Across {grade} Grade {gender}s\n' diff --git a/examples/statistics/boxplot_vs_violin.py b/examples/statistics/boxplot_vs_violin.py index 6f5d8de06f3b..f1162a1ef7a7 100644 --- a/examples/statistics/boxplot_vs_violin.py +++ b/examples/statistics/boxplot_vs_violin.py @@ -45,13 +45,11 @@ # adding horizontal grid lines for ax in axs: ax.yaxis.grid(True) - ax.set_xticks([y + 1 for y in range(len(all_data))]) + ax.set_xticks([y + 1 for y in range(len(all_data))], + labels=['x1', 'x2', 'x3', 'x4']) ax.set_xlabel('Four separate samples') ax.set_ylabel('Observed values') -# add x-tick labels -plt.setp(axs, xticks=[y + 1 for y in range(len(all_data))], - xticklabels=['x1', 'x2', 'x3', 'x4']) plt.show() ############################################################################# diff --git a/examples/statistics/customized_violin.py b/examples/statistics/customized_violin.py index a93270770afc..4809e5f28d81 100644 --- a/examples/statistics/customized_violin.py +++ b/examples/statistics/customized_violin.py @@ -29,8 +29,7 @@ def adjacent_values(vals, q1, q3): def set_axis_style(ax, labels): ax.xaxis.set_tick_params(direction='out') ax.xaxis.set_ticks_position('bottom') - ax.set_xticks(np.arange(1, len(labels) + 1)) - ax.set_xticklabels(labels) + ax.set_xticks(np.arange(1, len(labels) + 1), labels=labels) ax.set_xlim(0.25, len(labels) + 0.75) ax.set_xlabel('Sample name') diff --git a/examples/statistics/multiple_histograms_side_by_side.py b/examples/statistics/multiple_histograms_side_by_side.py index b62dbf175355..544b3ec4304b 100644 --- a/examples/statistics/multiple_histograms_side_by_side.py +++ b/examples/statistics/multiple_histograms_side_by_side.py @@ -54,8 +54,7 @@ lefts = x_loc - 0.5 * binned_data ax.barh(centers, binned_data, height=heights, left=lefts) -ax.set_xticks(x_locations) -ax.set_xticklabels(labels) +ax.set_xticks(x_locations, labels) ax.set_ylabel("Data values") ax.set_xlabel("Data sets") diff --git a/examples/style_sheets/ggplot.py b/examples/style_sheets/ggplot.py index 49b71b556d90..edd07ff367fb 100644 --- a/examples/style_sheets/ggplot.py +++ b/examples/style_sheets/ggplot.py @@ -45,8 +45,7 @@ ax3.bar(x, y1, width) ax3.bar(x + width, y2, width, color=list(plt.rcParams['axes.prop_cycle'])[2]['color']) -ax3.set_xticks(x + width) -ax3.set_xticklabels(['a', 'b', 'c', 'd', 'e']) +ax3.set_xticks(x + width, labels=['a', 'b', 'c', 'd', 'e']) # circles with colors from default color cycle for i, color in enumerate(plt.rcParams['axes.prop_cycle']): diff --git a/examples/style_sheets/style_sheets_reference.py b/examples/style_sheets/style_sheets_reference.py index 8bdeb0c62bc7..a3d6e3dd9ac9 100644 --- a/examples/style_sheets/style_sheets_reference.py +++ b/examples/style_sheets/style_sheets_reference.py @@ -45,8 +45,7 @@ def plot_bar_graphs(ax, prng, min_value=5, max_value=25, nb_samples=5): width = 0.25 ax.bar(x, ya, width) ax.bar(x + width, yb, width, color='C2') - ax.set_xticks(x + width) - ax.set_xticklabels(['a', 'b', 'c', 'd', 'e']) + ax.set_xticks(x + width, labels=['a', 'b', 'c', 'd', 'e']) return ax diff --git a/examples/text_labels_and_annotations/multiline.py b/examples/text_labels_and_annotations/multiline.py index d0ca38b95232..2aa6fea8c1af 100644 --- a/examples/text_labels_and_annotations/multiline.py +++ b/examples/text_labels_and_annotations/multiline.py @@ -34,9 +34,9 @@ va="baseline", ha="right", multialignment="left", bbox=dict(fc="none")) -ax1.set_xticks([0.2, 0.4, 0.6, 0.8, 1.]) -ax1.set_xticklabels(["Jan\n2009", "Feb\n2009", "Mar\n2009", "Apr\n2009", - "May\n2009"]) +ax1.set_xticks([0.2, 0.4, 0.6, 0.8, 1.], + labels=["Jan\n2009", "Feb\n2009", "Mar\n2009", "Apr\n2009", + "May\n2009"]) ax1.axhline(0.4) ax1.set_title("test line spacing for multiline text") diff --git a/examples/ticks_and_spines/spines_bounds.py b/examples/ticks_and_spines/spines_bounds.py index 96205a43d38e..5ccf3051e1ea 100644 --- a/examples/ticks_and_spines/spines_bounds.py +++ b/examples/ticks_and_spines/spines_bounds.py @@ -21,8 +21,7 @@ # set ticks and tick labels ax.set_xlim((0, 2*np.pi)) -ax.set_xticks([0, np.pi, 2*np.pi]) -ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$']) +ax.set_xticks([0, np.pi, 2*np.pi], labels=['0', r'$\pi$', r'2$\pi$']) ax.set_ylim((-1.5, 1.5)) ax.set_yticks([-1, 0, 1]) diff --git a/examples/units/bar_unit_demo.py b/examples/units/bar_unit_demo.py index da963e19f734..7b24a453154d 100644 --- a/examples/units/bar_unit_demo.py +++ b/examples/units/bar_unit_demo.py @@ -33,8 +33,7 @@ label='Women') ax.set_title('Scores by group and gender') -ax.set_xticks(ind + width / 2) -ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5')) +ax.set_xticks(ind + width / 2, labels=['G1', 'G2', 'G3', 'G4', 'G5']) ax.legend() ax.yaxis.set_units(inch) diff --git a/tutorials/toolkits/axes_grid.py b/tutorials/toolkits/axes_grid.py index 433f578a312f..fc797b4255ed 100644 --- a/tutorials/toolkits/axes_grid.py +++ b/tutorials/toolkits/axes_grid.py @@ -161,9 +161,9 @@ tick-formatter for bottom(or left)-axis. :: ax2 = ax.twin() # now, ax2 is responsible for "top" axis and "right" axis - ax2.set_xticks([0., .5*np.pi, np.pi, 1.5*np.pi, 2*np.pi]) - ax2.set_xticklabels(["0", r"$\frac{1}{2}\pi$", - r"$\pi$", r"$\frac{3}{2}\pi$", r"$2\pi$"]) + ax2.set_xticks([0., .5*np.pi, np.pi, 1.5*np.pi, 2*np.pi], + labels=["0", r"$\frac{1}{2}\pi$", + r"$\pi$", r"$\frac{3}{2}\pi$", r"$2\pi$"]) .. figure:: ../../gallery/axes_grid1/images/sphx_glr_simple_axisline4_001.png :target: ../../gallery/axes_grid1/simple_axisline4.html