Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Remove unnecessary calls to np.array in examples. #16430

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

Merged
merged 1 commit into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/animation/animated_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
n, bins = np.histogram(data, 100)

# get the corners of the rectangles for the histogram
left = np.array(bins[:-1])
right = np.array(bins[1:])
left = bins[:-1]
right = bins[1:]
bottom = np.zeros(len(left))
top = bottom + n
nrects = len(left)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def annotate_heatmap(im, data=None, valfmt="{x:.2f}",
y = ["Prod. {}".format(i) for i in range(10, 70, 10)]
x = ["Cycle {}".format(i) for i in range(1, 7)]

qrates = np.array(list("ABCDEFG"))
qrates = list("ABCDEFG")
norm = matplotlib.colors.BoundaryNorm(np.linspace(-3.5, 3.5, 8), 7)
fmt = matplotlib.ticker.FuncFormatter(lambda x, pos: qrates[::-1][norm(x)])

Expand Down
2 changes: 1 addition & 1 deletion examples/lines_bars_and_markers/eventplot_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

# set different line properties for each set of positions
# note that some overlap
lineoffsets1 = np.array([-15, -3, 1, 1.5, 6, 10])
lineoffsets1 = [-15, -3, 1, 1.5, 6, 10]
linelengths1 = [5, 2, 1, 1, 3, 1.5]

fig, axs = plt.subplots(2, 2)
Expand Down
2 changes: 1 addition & 1 deletion examples/lines_bars_and_markers/scatter_star_poly.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
axs[0, 1].set_title(r"marker=r'\$\alpha\$'")

# marker from path
verts = np.array([[-1, -1], [1, -1], [1, 1], [-1, -1]])
verts = [[-1, -1], [1, -1], [1, 1], [-1, -1]]
axs[0, 2].scatter(x, y, s=80, c=z, marker=verts)
axs[0, 2].set_title("marker=verts")

Expand Down
9 changes: 5 additions & 4 deletions examples/lines_bars_and_markers/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@
markerline.set_ydata(np.zeros(len(dates)))

# annotate lines
vert = np.array(['top', 'bottom'])[(levels > 0).astype(int)]
for d, l, r, va in zip(dates, levels, names, vert):
ax.annotate(r, xy=(d, l), xytext=(-3, np.sign(l)*3),
textcoords="offset points", va=va, ha="right")
for d, l, r in zip(dates, levels, names):
ax.annotate(r, xy=(d, l),
xytext=(-3, np.sign(l)*3), textcoords="offset points",
horizontalalignment="right",
verticalalignment="bottom" if l > 0 else "top")

# format xaxis with 4 month intervals
ax.get_xaxis().set_major_locator(mdates.MonthLocator(interval=4))
Expand Down
4 changes: 2 additions & 2 deletions examples/misc/histogram_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
n, bins = np.histogram(data, 50)

# get the corners of the rectangles for the histogram
left = np.array(bins[:-1])
right = np.array(bins[1:])
left = bins[:-1]
right = bins[1:]
bottom = np.zeros(len(left))
top = bottom + n

Expand Down
4 changes: 2 additions & 2 deletions examples/pie_and_polar_charts/nested_pie.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

cmap = plt.get_cmap("tab20c")
outer_colors = cmap(np.arange(3)*4)
inner_colors = cmap(np.array([1, 2, 5, 6, 9, 10]))
inner_colors = cmap([1, 2, 5, 6, 9, 10])

ax.pie(vals.sum(axis=1), radius=1, colors=outer_colors,
wedgeprops=dict(width=size, edgecolor='w'))
Expand Down Expand Up @@ -63,7 +63,7 @@

cmap = plt.get_cmap("tab20c")
outer_colors = cmap(np.arange(3)*4)
inner_colors = cmap(np.array([1, 2, 5, 6, 9, 10]))
inner_colors = cmap([1, 2, 5, 6, 9, 10])

ax.bar(x=valsleft[:, 0],
width=valsnorm.sum(axis=1), bottom=1-size, height=size,
Expand Down
11 changes: 6 additions & 5 deletions examples/scales/logit_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
Examples of plots with logit axes.
"""

import math

import numpy as np
import matplotlib.pyplot as plt

xmax = 10
x = np.linspace(-xmax, xmax, 10000)
cdf_norm = np.array([np.math.erf(w / np.sqrt(2)) / 2 + 1 / 2 for w in x])
cdf_laplacian = np.array(
[1 / 2 * np.exp(w) if w < 0 else 1 - 1 / 2 * np.exp(-w) for w in x]
)
cdf_cauchy = 1 / np.pi * np.arctan(x) + 1 / 2
cdf_norm = [math.erf(w / np.sqrt(2)) / 2 + 1 / 2 for w in x]
cdf_laplacian = [1 / 2 * np.exp(w) if w < 0 else 1 - 1 / 2 * np.exp(-w)
for w in x]
cdf_cauchy = np.arctan(x) / np.pi + 1 / 2

fig, axs = plt.subplots(nrows=3, ncols=2, figsize=(6.4, 8.5))

Expand Down
4 changes: 2 additions & 2 deletions examples/shapes_and_collections/artist_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ def label(xy, text):
label(grid[7], "FancyBboxPatch")

# add a line
x, y = np.array([[-0.06, 0.0, 0.1], [0.05, -0.05, 0.05]])
x, y = ([-0.06, 0.0, 0.1], [0.05, -0.05, 0.05])
line = mlines.Line2D(x + grid[8, 0], y + grid[8, 1], lw=5., alpha=0.3)
label(grid[8], "Line2D")

colors = np.linspace(0, 1, len(patches))
collection = PatchCollection(patches, cmap=plt.cm.hsv, alpha=0.3)
collection.set_array(np.array(colors))
collection.set_array(colors)
ax.add_collection(collection)
ax.add_line(line)

Expand Down
4 changes: 1 addition & 3 deletions examples/shapes_and_collections/compound_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
and a triangle. Use ``CLOSEPOLY`` and ``MOVETO`` for the different parts of
the compound path
"""
import numpy as np

from matplotlib.path import Path
from matplotlib.patches import PathPatch
import matplotlib.pyplot as plt


vertices = []
codes = []

Expand All @@ -22,7 +21,6 @@
codes += [Path.MOVETO] + [Path.LINETO]*2 + [Path.CLOSEPOLY]
vertices += [(4, 4), (5, 5), (5, 4), (0, 0)]

vertices = np.array(vertices, float)
path = Path(vertices, codes)

pathpatch = PathPatch(path, facecolor='None', edgecolor='green')
Expand Down
4 changes: 2 additions & 2 deletions examples/shapes_and_collections/patch_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
polygon = Polygon(np.random.rand(N, 2), True)
patches.append(polygon)

colors = 100*np.random.rand(len(patches))
colors = 100 * np.random.rand(len(patches))
p = PatchCollection(patches, alpha=0.4)
p.set_array(np.array(colors))
p.set_array(colors)
ax.add_collection(p)
fig.colorbar(p, ax=ax)

Expand Down
4 changes: 2 additions & 2 deletions examples/specialty_plots/leftventricle_bulleye.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def bullseye_plot(ax, data, seg_bold=None, cmap=None, norm=None):
seg_bold = []

linewidth = 2
data = np.array(data).ravel()
data = np.ravel(data)

if cmap is None:
cmap = plt.cm.viridis
Expand Down Expand Up @@ -129,7 +129,7 @@ def bullseye_plot(ax, data, seg_bold=None, cmap=None, norm=None):


# Create the fake data
data = np.array(range(17)) + 1
data = np.arange(17) + 1


# Make a figure and axes with dimensions as desired.
Expand Down
2 changes: 1 addition & 1 deletion examples/statistics/boxplot_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def fake_bootstrapper(n):
conf_intervals = [None, None, ci1, ci2]

fig, ax = plt.subplots()
pos = np.array(range(len(treatments))) + 1
pos = np.arange(len(treatments)) + 1
bp = ax.boxplot(treatments, sym='k+', positions=pos,
notch=1, bootstrap=5000,
usermedians=medians,
Expand Down
24 changes: 10 additions & 14 deletions examples/statistics/confidence_ellipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ def get_correlated_dataset(n, dependency, mu, scale):
np.random.seed(0)

PARAMETERS = {
'Positive correlation': np.array([[0.85, 0.35],
[0.15, -0.65]]),
'Negative correlation': np.array([[0.9, -0.4],
[0.1, -0.6]]),
'Weak correlation': np.array([[1, 0],
[0, 1]]),
'Positive correlation': [[0.85, 0.35],
[0.15, -0.65]],
'Negative correlation': [[0.9, -0.4],
[0.1, -0.6]],
'Weak correlation': [[1, 0],
[0, 1]],
}

mu = 2, 4
Expand Down Expand Up @@ -164,10 +164,8 @@ def get_correlated_dataset(n, dependency, mu, scale):

fig, ax_nstd = plt.subplots(figsize=(6, 6))

dependency_nstd = np.array([
[0.8, 0.75],
[-0.2, 0.35]
])
dependency_nstd = [[0.8, 0.75],
[-0.2, 0.35]]
mu = 0, 0
scale = 8, 5

Expand Down Expand Up @@ -199,10 +197,8 @@ def get_correlated_dataset(n, dependency, mu, scale):
# to have the ellipse rendered in different ways.

fig, ax_kwargs = plt.subplots(figsize=(6, 6))
dependency_kwargs = np.array([
[-0.8, 0.5],
[-0.2, 0.5]
])
dependency_kwargs = [[-0.8, 0.5],
[-0.2, 0.5]]
mu = 2, -3
scale = 6, 5

Expand Down
4 changes: 2 additions & 2 deletions examples/statistics/errorbar_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
# mock up some limits by modifying previous data
xlolims = lolims
xuplims = uplims
lolims = np.zeros(x.shape)
uplims = np.zeros(x.shape)
lolims = np.zeros_like(x)
uplims = np.zeros_like(x)
lolims[[6]] = True # only limited at this index
uplims[[3]] = True # only limited at this index

Expand Down
2 changes: 1 addition & 1 deletion examples/subplots_axes_and_figures/axes_box_aspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
axs[1, 0].set_box_aspect(1)
axs[1, 1].set_box_aspect(3/1)

x, y = np.random.randn(2, 400) * np.array([[.5], [180]])
x, y = np.random.randn(2, 400) * [[.5], [180]]
axs[1, 0].scatter(x, y)
axs[0, 0].hist(x)
axs[1, 1].hist(y, orientation="horizontal")
Expand Down
4 changes: 2 additions & 2 deletions examples/text_labels_and_annotations/custom_legends.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
np.random.seed(19680801)

N = 10
data = [np.logspace(0, 1, 100) + np.random.randn(100) + ii for ii in range(N)]
data = np.array(data).T
data = np.transpose([np.logspace(0, 1, 100) + np.random.randn(100) + ii
for ii in range(N)])
cmap = plt.cm.coolwarm
rcParams['axes.prop_cycle'] = cycler(color=cmap(np.linspace(0, 1, N)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,26 @@
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

# Plot diagonal line (45 degrees)
h = plt.plot(np.arange(0, 10), np.arange(0, 10))
h = ax.plot(range(0, 10), range(0, 10))

# set limits so that it no longer looks on screen to be 45 degrees
plt.xlim([-10, 20])
ax.set_xlim([-10, 20])

# Locations to plot text
l1 = np.array((1, 1))
l2 = np.array((5, 5))

# Rotate angle
angle = 45
trans_angle = plt.gca().transData.transform_angles(np.array((45,)),
l2.reshape((1, 2)))[0]
trans_angle = ax.transData.transform_angles([45], l2.reshape((1, 2)))[0]

# Plot text
th1 = plt.text(l1[0], l1[1], 'text not rotated correctly', fontsize=16,
rotation=angle, rotation_mode='anchor')
th2 = plt.text(l2[0], l2[1], 'text rotated correctly', fontsize=16,
rotation=trans_angle, rotation_mode='anchor')
th1 = ax.text(*l1, 'text not rotated correctly', fontsize=16,
rotation=angle, rotation_mode='anchor')
th2 = ax.text(*l2, 'text rotated correctly', fontsize=16,
rotation=trans_angle, rotation_mode='anchor')

plt.show()
3 changes: 1 addition & 2 deletions examples/ticks_and_spines/date_concise_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
# First, the default formatter.

base = datetime.datetime(2005, 2, 1)
dates = np.array([base + datetime.timedelta(hours=(2 * i))
for i in range(732)])
dates = [base + datetime.timedelta(hours=(2 * i)) for i in range(732)]
N = len(dates)
np.random.seed(19680801)
y = np.cumsum(np.random.randn(N))
Expand Down
2 changes: 1 addition & 1 deletion examples/units/ellipse_with_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
])


x, y = np.dot(R, np.array([x, y]))
x, y = np.dot(R, [x, y])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
x, y = np.dot(R, [x, y])
x, y = R @ [x, y]

Simpler?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather leave the dot -> @ for another time.

x += xcenter
y += ycenter

Expand Down