From 414bcff83c6fec3fa1cfbe105be585e41f60b36f Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Sat, 22 Nov 2014 09:56:37 +0000 Subject: [PATCH 01/13] Use stringIO from six Python3 fix --- examples/api/skewt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/api/skewt.py b/examples/api/skewt.py index ce9e23fef157..ba1d23b6c9e6 100644 --- a/examples/api/skewt.py +++ b/examples/api/skewt.py @@ -146,7 +146,7 @@ def _set_lim_and_transforms(self): # Now make a simple example using the custom projection. from matplotlib.ticker import ScalarFormatter, MultipleLocator import matplotlib.pyplot as plt - from StringIO import StringIO + from six import StringIO import numpy as np # Some examples data From d0bb8c10bbd00efc50c5eb360d5606f6280f6dae Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Fri, 21 Nov 2014 21:39:04 +0000 Subject: [PATCH 02/13] Unicode issue on python3 --- doc/pyplots/text_commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/pyplots/text_commands.py b/doc/pyplots/text_commands.py index 93b2fcd80308..92f40b60b9ca 100644 --- a/doc/pyplots/text_commands.py +++ b/doc/pyplots/text_commands.py @@ -16,7 +16,7 @@ ax.text(2, 6, r'an equation: $E=mc^2$', fontsize=15) -ax.text(3, 2, unicode('unicode: Institut f\374r Festk\366rperphysik', 'latin-1')) +ax.text(3, 2, u'unicode: Institut f\374r Festk\366rperphysik') ax.text(0.95, 0.01, 'colored text in axes coords', verticalalignment='bottom', horizontalalignment='right', From 280001aa9e5b6488474bac575928fd4a260ba8e1 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Fri, 21 Nov 2014 20:43:14 +0000 Subject: [PATCH 03/13] Use open rather than file --- examples/pylab_examples/logo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pylab_examples/logo.py b/examples/pylab_examples/logo.py index 436135cf7fa9..03cb060d18d2 100755 --- a/examples/pylab_examples/logo.py +++ b/examples/pylab_examples/logo.py @@ -9,7 +9,7 @@ datafile = cbook.get_sample_data('membrane.dat', asfileobj=False) print('loading', datafile) -x = 1000*0.1*fromstring(file(datafile, 'rb').read(), float32) +x = 1000*0.1*fromstring(open(datafile, 'rb').read(), float32) # 0.0005 is the sample interval t = 0.0005*arange(len(x)) figure(1, figsize=(7, 1), dpi=100) From 247a0eca896de06feffbfd26bd1b51a1c09cd372 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Wed, 19 Nov 2014 18:09:11 +0000 Subject: [PATCH 04/13] python3 whats_new_99_spines --- doc/pyplots/whats_new_99_spines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/pyplots/whats_new_99_spines.py b/doc/pyplots/whats_new_99_spines.py index 1ee80a9e708e..83c4edf5d07f 100644 --- a/doc/pyplots/whats_new_99_spines.py +++ b/doc/pyplots/whats_new_99_spines.py @@ -4,7 +4,7 @@ def adjust_spines(ax,spines): - for loc, spine in ax.spines.iteritems(): + for loc, spine in ax.spines.items(): if loc in spines: spine.set_position(('outward',10)) # outward by 10 points else: From a67a8d6776dd0ecd42944847c18d12f49216a9d6 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Wed, 19 Nov 2014 18:04:39 +0000 Subject: [PATCH 05/13] Add Lfunction --- doc/users/plotting/colormaps/Lfunction.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/doc/users/plotting/colormaps/Lfunction.py b/doc/users/plotting/colormaps/Lfunction.py index dadf4de457de..c94a8be4b45a 100644 --- a/doc/users/plotting/colormaps/Lfunction.py +++ b/doc/users/plotting/colormaps/Lfunction.py @@ -3,7 +3,7 @@ with the binary matplotlib colormap, too. Trying to show the difference between adding blackness to a color at different rates. ''' - +from __future__ import print_function import numpy as np import matplotlib.pyplot as plt import colorconv as color @@ -39,10 +39,10 @@ # Alter successive rows with more black k = 1 -for i in xrange(red.shape[1]): +for i in range(red.shape[1]): # more blackness is closer to 0 than one, and in first column of LAB lab_add[0,i,0] = lab_add[0,i,0] - 10*i - print i,k + print(i,k) if i != 0: lab_geometric[0,i,0] = lab_geometric[0,i,0] - 10*k k *= 2 @@ -57,15 +57,15 @@ fig = plt.figure() k = 1 -for i in xrange(red.shape[1]): +for i in range(red.shape[1]): # LHS: additive ax1 = fig.add_subplot(nrows,2,i*2+1, axisbg=tuple(rgb_add[0,i,:])) - print tuple(lab_add[0,i,:])#, tuple(rgb_add[0,i,:]) + print(tuple(lab_add[0,i,:]))#, tuple(rgb_add[0,i,:]) # RHS: multiplicative ax2 = fig.add_subplot(nrows,2,i*2+2, axisbg=tuple(rgb_geometric[0,i,:])) - print tuple(lab_geometric[0,i,:])#, tuple(rgb_geometric[0,i,:]) + print(tuple(lab_geometric[0,i,:]))#, tuple(rgb_geometric[0,i,:]) # ylabels if i!=0: @@ -117,16 +117,16 @@ k = 1 di = 8 I0 = 5 -for i in xrange(nrows): +for i in range(nrows): # Do more blackness via increasing indices rgb_add[:,i,:] = rgb[:,i*di+I0,:] if i != 0: - print i*di+I0, di*k+I0, (I0**(1./3)+i*di**(1./3))**3 + print(i*di+I0, di*k+I0, (I0**(1./3)+i*di**(1./3))**3) rgb_geometric[:,i,:] = rgb[:,I0+di*k,:] k *= 2 elif i==0: - print i*di+I0, I0, (I0**(1./3)+i*di**(1./3))**3 + print(i*di+I0, I0, (I0**(1./3)+i*di**(1./3))**3) rgb_geometric[:,i,:] = rgb[:,I0,:] lab_add = color.rgb2lab(rgb_add) @@ -134,7 +134,7 @@ fig = plt.figure() k = 1 -for i in xrange(nrows): +for i in range(nrows): # LHS: additive ax1 = fig.add_subplot(nrows,ncols,i*2+1, axisbg=tuple(rgb_add[0,i,:])) From 6a86f6973141aae19434bbf91339ad3445243669 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Wed, 19 Nov 2014 18:02:03 +0000 Subject: [PATCH 06/13] Python3 lightness.py --- doc/users/plotting/colormaps/lightness.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/users/plotting/colormaps/lightness.py b/doc/users/plotting/colormaps/lightness.py index fb97500d0cbc..15f93d29779d 100644 --- a/doc/users/plotting/colormaps/lightness.py +++ b/doc/users/plotting/colormaps/lightness.py @@ -58,7 +58,7 @@ fig = plt.figure(figsize=(11.5,4*nsubplots)) - for i, subplot in enumerate(xrange(nsubplots)): + for i, subplot in enumerate(range(nsubplots)): locs = [] # locations for text labels From 45f403544d03a7419163f0107d4fdf8661a55271 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Wed, 19 Nov 2014 18:07:33 +0000 Subject: [PATCH 07/13] python3 demo_gridspec06 --- doc/users/plotting/examples/demo_gridspec06.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/users/plotting/examples/demo_gridspec06.py b/doc/users/plotting/examples/demo_gridspec06.py index ead2029af251..e1fbaf5c5f99 100644 --- a/doc/users/plotting/examples/demo_gridspec06.py +++ b/doc/users/plotting/examples/demo_gridspec06.py @@ -23,7 +23,7 @@ def squiggle_xy(a, b, c, d, i=np.arange(0.0, 2*np.pi, 0.05)): # gridspec inside gridspec outer_grid = gridspec.GridSpec(4, 4, wspace=0.0, hspace=0.0) -for i in xrange(16): +for i in range(16): inner_grid = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=outer_grid[i], wspace=0.0, hspace=0.0) a, b = int(i/4)+1,i%4+1 From 6dbfe7a6693a2e0f6b73c73e72e0d40f6e22c04e Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Wed, 19 Nov 2014 17:59:31 +0000 Subject: [PATCH 08/13] Multiple_yaxis python3 --- examples/pylab_examples/multiple_yaxis_with_spines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pylab_examples/multiple_yaxis_with_spines.py b/examples/pylab_examples/multiple_yaxis_with_spines.py index 98532d31b047..fde330ec5528 100644 --- a/examples/pylab_examples/multiple_yaxis_with_spines.py +++ b/examples/pylab_examples/multiple_yaxis_with_spines.py @@ -4,7 +4,7 @@ def make_patch_spines_invisible(ax): ax.set_frame_on(True) ax.patch.set_visible(False) - for sp in ax.spines.itervalues(): + for sp in ax.spines.values(): sp.set_visible(False) fig, host = plt.subplots() From fc226b65441f317b678165bbac161acd79d23580 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Wed, 19 Nov 2014 15:05:56 +0000 Subject: [PATCH 09/13] boxplot_demo2 python3 --- examples/pylab_examples/boxplot_demo2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/pylab_examples/boxplot_demo2.py b/examples/pylab_examples/boxplot_demo2.py index 8249c689110f..e90c00f8fdda 100644 --- a/examples/pylab_examples/boxplot_demo2.py +++ b/examples/pylab_examples/boxplot_demo2.py @@ -57,7 +57,7 @@ # Now fill the boxes with desired colors boxColors = ['darkkhaki', 'royalblue'] numBoxes = numDists*2 -medians = range(numBoxes) +medians = list(range(numBoxes)) for i in range(numBoxes): box = bp['boxes'][i] boxX = [] @@ -65,7 +65,7 @@ for j in range(5): boxX.append(box.get_xdata()[j]) boxY.append(box.get_ydata()[j]) - boxCoords = zip(boxX, boxY) + boxCoords = list(zip(boxX, boxY)) # Alternate between Dark Khaki and Royal Blue k = i % 2 boxPolygon = Polygon(boxCoords, facecolor=boxColors[k]) From 6fda3df9dbe439d69a4871258216d0bd505c6147 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Wed, 19 Nov 2014 14:51:42 +0000 Subject: [PATCH 10/13] marker_reference python 3 compatible --- examples/lines_bars_and_markers/marker_reference.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/lines_bars_and_markers/marker_reference.py b/examples/lines_bars_and_markers/marker_reference.py index 81bd3977ae4e..3878aa7e460e 100644 --- a/examples/lines_bars_and_markers/marker_reference.py +++ b/examples/lines_bars_and_markers/marker_reference.py @@ -32,9 +32,10 @@ def split_list(a_list): fig, axes = plt.subplots(ncols=2) # Filter out filled markers and marker settings that do nothing. -unfilled_markers = [m for m, func in Line2D.markers.iteritems() +unfilled_markers = [m for m, func in Line2D.markers.items() if func != 'nothing' and m not in Line2D.filled_markers] -unfilled_markers = sorted(unfilled_markers)[::-1] # Reverse-sort for pretty +# Reverse-sort for pretty +unfilled_markers = sorted(unfilled_markers, key=str)[::-1] for ax, markers in zip(axes, split_list(unfilled_markers)): for y, marker in enumerate(markers): ax.text(-0.5, y, nice_repr(marker), **text_style) From fbf0da901d1d3219d9faba3fb7c02252a4005415 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Tue, 18 Nov 2014 21:38:20 +0000 Subject: [PATCH 11/13] No need to use xrange for a short loop fixes python3 --- examples/pylab_examples/animation_demo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pylab_examples/animation_demo.py b/examples/pylab_examples/animation_demo.py index f294a189f75f..a8048d357b1c 100644 --- a/examples/pylab_examples/animation_demo.py +++ b/examples/pylab_examples/animation_demo.py @@ -13,7 +13,7 @@ y = np.arange(5) z = x * y[:, np.newaxis] -for i in xrange(5): +for i in range(5): if i == 0: p = plt.imshow(z) fig = plt.gcf() From 273462d54b5b864b70a4331bca024bf01c802b87 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Tue, 30 Dec 2014 12:40:37 +0100 Subject: [PATCH 12/13] Use iteritems to iterate over markers --- examples/lines_bars_and_markers/marker_reference.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/lines_bars_and_markers/marker_reference.py b/examples/lines_bars_and_markers/marker_reference.py index 3878aa7e460e..a4219986b61c 100644 --- a/examples/lines_bars_and_markers/marker_reference.py +++ b/examples/lines_bars_and_markers/marker_reference.py @@ -1,6 +1,7 @@ """ Reference for filled- and unfilled-marker types included with Matplotlib. """ +from six import iteritems import numpy as np import matplotlib.pyplot as plt from matplotlib.lines import Line2D @@ -32,7 +33,9 @@ def split_list(a_list): fig, axes = plt.subplots(ncols=2) # Filter out filled markers and marker settings that do nothing. -unfilled_markers = [m for m, func in Line2D.markers.items() +# We use iteritems from six to make sure that we get an iterator +# in both python 2 and 3 +unfilled_markers = [m for m, func in iteritems(Line2D.markers) if func != 'nothing' and m not in Line2D.filled_markers] # Reverse-sort for pretty unfilled_markers = sorted(unfilled_markers, key=str)[::-1] From 27c26f169fe7bb5133453b5d934cb3cc0f19af54 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Tue, 30 Dec 2014 18:27:28 +0100 Subject: [PATCH 13/13] Restore python2 sorting --- examples/lines_bars_and_markers/marker_reference.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/lines_bars_and_markers/marker_reference.py b/examples/lines_bars_and_markers/marker_reference.py index a4219986b61c..ad7db0e89023 100644 --- a/examples/lines_bars_and_markers/marker_reference.py +++ b/examples/lines_bars_and_markers/marker_reference.py @@ -37,8 +37,10 @@ def split_list(a_list): # in both python 2 and 3 unfilled_markers = [m for m, func in iteritems(Line2D.markers) if func != 'nothing' and m not in Line2D.filled_markers] -# Reverse-sort for pretty -unfilled_markers = sorted(unfilled_markers, key=str)[::-1] +# Reverse-sort for pretty. We use our own sort key which is essentially +# a python3 compatible reimplementation of python2 sort. +unfilled_markers = sorted(unfilled_markers, + key=lambda x: (str(type(x)), str(x)))[::-1] for ax, markers in zip(axes, split_list(unfilled_markers)): for y, marker in enumerate(markers): ax.text(-0.5, y, nice_repr(marker), **text_style)