-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix python3 issues in some examples #3831
Changes from all commits
414bcff
d0bb8c1
280001a
247a0ec
a67a8d6
6a86f69
45f4035
6dbfe7a
fc226b6
6fda3df
fbf0da9
273462d
27c26f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also include There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need (only 5 items) |
||
# 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,24 +117,24 @@ | |
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) | ||
lab_geometric = color.rgb2lab(rgb_geometric) | ||
|
||
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,:])) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Less that 10 items |
||
|
||
locs = [] # locations for text labels | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 16 elements. Iterator makes no difference |
||
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably do this with a context manager, but that is out of scope for this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed |
||
# 0.0005 is the sample interval | ||
t = 0.0005*arange(len(x)) | ||
figure(1, figsize=(7, 1), dpi=100) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need, there is a maximum of 4 spines |
||
sp.set_visible(False) | ||
|
||
fig, host = plt.subplots() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be done as
six.iteritems(spines)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I deliberately chose to avoid six as much as possible to simplify the examples. As I see it examples has a different balance between code readability and performance. But you are probably right that in the cases of axes spines etc. which are somewhat longer loops it makes sense to use iterators.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. At this point I think of six as being part of the 'standard'
library and don't write anything without it so did not give it a tremendous
amount of thought.
The case could be made that the examples are should also be examples of
general best practices, and python 2/3 compatibility is one of them, but I
will defer to your judgement on this.
On Tue Nov 25 2014 at 3:40:52 PM Jens Hedegaard Nielsen <
notifications@github.com> wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will go through them one by one and change to six in cases like this where it is a definite advantage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use your judgment; in my view, six should be used as little as possible, especially in examples. It's an ugly but sometimes necessary workaround for 2/3 compatibility. One would have to have a huge dictionary before there would be any practical difference between items and iteritems, for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @efiring here: Let's not use six in examples unless there is a strong reason on a case-by-case basis.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only 4 spines in a figure so six is unlikely to make a difference.