Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Cleanup: broadcasting#7562

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
QuLogic merged 1 commit intomatplotlib:masterfromanntzer:cleanup-broadcasting
Aug 27, 2017
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsdoc/api/axis_api.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -109,8 +109,8 @@ Ticks, tick labels and Offset text
Axis.axis_date


Data and viewinternvals
------------------------
Data and viewintervals
-----------------------

.. autosummary::
:toctree: _as_gen
Expand Down
2 changes: 1 addition & 1 deletionexamples/animation/unchained.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,7 +25,7 @@
# Generate random data
data=np.random.uniform(0,1, (64,75))
X=np.linspace(-1,1,data.shape[-1])
G=1.5*np.exp(-4*X*X)
G=1.5*np.exp(-4*X**2)
Copy link
Member

Choose a reason for hiding this comment

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

In most cases I think this construct is marginally faster.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

# float is probably the most common caseIn [8]: x = np.arange(100000).astype(float)In [9]: %timeit x * x10000 loops, best of 3: 65.5 µs per loopIn [10]: %timeit x ** 210000 loops, best of 3: 66.4 µs per loop

(and you can easily get them reversed on another run).

Copy link
Member

Choose a reason for hiding this comment

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

interesting. That is an apparently wrong rule of thumb I have been using for a while.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Why would you think this would be the case? (I am honestly puzzled.)

Copy link
Member

Choose a reason for hiding this comment

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

It dates back to the early days of computers and compilers, when computers were slow and compilers were not so bright. Multiplication is faster than taking a power, in general. But now compilers recognize things like this and find the best algorithm. In the case of numpy, I'm pretty sure there is explicit internal optimization of small integer powers so that we wouldn't have to use that ancient manual optimization.

Copy link
Member

Choose a reason for hiding this comment

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

I respectfully request the children remain off of my lawn. 😈

ivanov reacted with laugh emoji
Copy link
Contributor

@eric-wiesereric-wieserDec 15, 2017
edited
Loading

Choose a reason for hiding this comment

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

As@efiring says, numpy hard-codespower(x, 2) to fall back onsquare(x), along with a handful of other constants (I think(-1, 0, 0.5, 1, 2))


# Generate line plots
lines= []
Expand Down
12 changes: 4 additions & 8 deletionsexamples/api/custom_projection_example.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -434,15 +434,11 @@ def __init__(self, resolution):
self._resolution = resolution

def transform_non_affine(self, xy):
x = xy[:, 0:1]
y = xy[:, 1:2]

quarter_x = 0.25 * x
half_y = 0.5 * y
z = np.sqrt(1.0 - quarter_x*quarter_x - half_y*half_y)
longitude = 2 * np.arctan((z*x) / (2.0 * (2.0*z*z - 1.0)))
x, y = xy.T
z = np.sqrt(1 - (x / 4) ** 2 - (y / 2) ** 2)
longitude = 2 * np.arctan((z * x) / (2 * (2 * z ** 2 - 1)))
latitude = np.arcsin(y*z)
return np.concatenate((longitude, latitude), 1)
return np.column_stack([longitude, latitude])
transform_non_affine.__doc__ = Transform.transform_non_affine.__doc__

def inverted(self):
Expand Down
36 changes: 17 additions & 19 deletionsexamples/api/scatter_piecharts.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,7 +7,7 @@

Thanks to Manuel Metz for the example
"""
import math

import numpy as np
import matplotlib.pyplot as plt

Expand All@@ -16,35 +16,33 @@
r2 = r1 + 0.4 # 40%

# define some sizes of the scatter marker
sizes = [60, 80, 120]
sizes =np.array([60, 80, 120])

# calculate the points of the first pie marker
#
# these are just the origin (0,0) +
# some points on a circle cos,sin
x = [0] + np.cos(np.linspace(0, 2*math.pi*r1, 10)).tolist()
y = [0] + np.sin(np.linspace(0, 2*math.pi*r1, 10)).tolist()

x = [0] + np.cos(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
y = [0] + np.sin(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
xy1 = list(zip(x, y))
s1 = max(max(x), max(y))
s1 =np.max(xy1)

# ...
x = [0] + np.cos(np.linspace(2*math.pi*r1, 2*math.pi*r2, 10)).tolist()
y = [0] + np.sin(np.linspace(2*math.pi*r1, 2*math.pi*r2, 10)).tolist()
x = [0] + np.cos(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
y = [0] + np.sin(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
xy2 = list(zip(x, y))
s2 = max(max(x), max(y))
s2 =np.max(xy2)

x = [0] + np.cos(np.linspace(2*math.pi*r2, 2*math.pi, 10)).tolist()
y = [0] + np.sin(np.linspace(2*math.pi*r2, 2*math.pi, 10)).tolist()
x = [0] + np.cos(np.linspace(2 * np.pi *r2, 2 * np.pi, 10)).tolist()
y = [0] + np.sin(np.linspace(2 * np.pi *r2, 2 * np.pi, 10)).tolist()
xy3 = list(zip(x, y))
s3 = max(max(x), max(y))
s3 =np.max(xy3)

fig, ax = plt.subplots()
ax.scatter(np.arange(3),np.arange(3), marker=(xy1, 0),
s=[s1*s1*_ for _ insizes], facecolor='blue')
ax.scatter(np.arange(3),np.arange(3), marker=(xy2, 0),
s=[s2*s2*_ for _ insizes], facecolor='green')
ax.scatter(np.arange(3),np.arange(3), marker=(xy3, 0),
s=[s3*s3*_ for _ insizes], facecolor='red')
ax.scatter(range(3),range(3), marker=(xy1, 0),
s=s1 ** 2 *sizes, facecolor='blue')
ax.scatter(range(3),range(3), marker=(xy2, 0),
s=s2 ** 2 *sizes, facecolor='green')
ax.scatter(range(3),range(3), marker=(xy3, 0),
s=s3 ** 2 *sizes, facecolor='red')

plt.show()
2 changes: 1 addition & 1 deletionexamples/event_handling/timers.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,7 +18,7 @@ def update_title(axes):
fig, ax = plt.subplots()

x = np.linspace(-3, 3)
ax.plot(x, x*x)
ax.plot(x, x ** 2)

# Create a new timer object. Set the interval to 100 milliseconds
# (1000 is default) and tell the timer what function should be called.
Expand Down
24 changes: 11 additions & 13 deletionsexamples/event_handling/trifinder_event_demo.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,16 +11,15 @@
from matplotlib.tri import Triangulation
from matplotlib.patches import Polygon
import numpy as np
import math


def update_polygon(tri):
if tri == -1:
points = [0, 0, 0]
else:
points =triangulation.triangles[tri]
xs =triangulation.x[points]
ys =triangulation.y[points]
points =triang.triangles[tri]
xs =triang.x[points]
ys =triang.y[points]
polygon.set_xy(list(zip(xs, ys)))


Expand All@@ -39,23 +38,22 @@ def motion_notify(event):
n_radii = 5
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, n_radii)
angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] +=math.pi / n_angles
angles[:, 1::2] +=np.pi / n_angles
x = (radii*np.cos(angles)).flatten()
y = (radii*np.sin(angles)).flatten()
triangulation = Triangulation(x, y)
xmid = x[triangulation.triangles].mean(axis=1)
ymid = y[triangulation.triangles].mean(axis=1)
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
triangulation.set_mask(mask)
triang = Triangulation(x, y)
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
y[triang.triangles].mean(axis=1))
< min_radius)

# Use the triangulation's default TriFinder object.
trifinder =triangulation.get_trifinder()
trifinder =triang.get_trifinder()

# Setup plot and callbacks.
plt.subplot(111, aspect='equal')
plt.triplot(triangulation, 'bo-')
plt.triplot(triang, 'bo-')
polygon = Polygon([[0, 0], [0, 0]], facecolor='y') # dummy data for xs,ys
update_polygon(-1)
plt.gca().add_patch(polygon)
Expand Down
14 changes: 6 additions & 8 deletionsexamples/images_contours_and_fields/tricontour_demo.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,6 @@
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
import math

###############################################################################
# Creating a Triangulation without specifying the triangles results in the
Expand All@@ -20,22 +19,21 @@
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2 *math.pi, n_angles, endpoint=False)
angles = np.linspace(0, 2 *np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] +=math.pi / n_angles
angles[:, 1::2] +=np.pi / n_angles

x = (radii * np.cos(angles)).flatten()
y = (radii * np.sin(angles)).flatten()
z = (np.cos(radii) * np.cos(angles *3.0)).flatten()
z = (np.cos(radii) * np.cos(3 *angles)).flatten()

# Create the Triangulation; no triangles so Delaunay triangulation created.
triang = tri.Triangulation(x, y)

# Mask off unwanted triangles.
xmid = x[triang.triangles].mean(axis=1)
ymid = y[triang.triangles].mean(axis=1)
mask = np.where(xmid * xmid + ymid * ymid < min_radius * min_radius, 1, 0)
triang.set_mask(mask)
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
y[triang.triangles].mean(axis=1))
< min_radius)

###############################################################################
# pcolor plot.
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,7 +10,6 @@
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
import math


#-----------------------------------------------------------------------------
Expand All@@ -36,9 +35,9 @@ def function_z(x, y):
min_radius = 0.15
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2 *math.pi, n_angles, endpoint=False)
angles = np.linspace(0, 2 *np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] +=math.pi / n_angles
angles[:, 1::2] +=np.pi / n_angles

x = (radii * np.cos(angles)).flatten()
y = (radii * np.sin(angles)).flatten()
Expand All@@ -50,10 +49,9 @@ def function_z(x, y):
triang = tri.Triangulation(x, y)

# Mask off unwanted triangles.
xmid = x[triang.triangles].mean(axis=1)
ymid = y[triang.triangles].mean(axis=1)
mask = np.where(xmid * xmid + ymid * ymid < min_radius * min_radius, 1, 0)
triang.set_mask(mask)
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
y[triang.triangles].mean(axis=1))
< min_radius)

#-----------------------------------------------------------------------------
# Refine data
Expand Down
16 changes: 7 additions & 9 deletionsexamples/images_contours_and_fields/trigradient_demo.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,12 +5,11 @@

Demonstrates computation of gradient with matplotlib.tri.CubicTriInterpolator.
"""
from matplotlib.tri importTriangulation, UniformTriRefiner,\
CubicTriInterpolator
from matplotlib.tri import(
Triangulation, UniformTriRefiner,CubicTriInterpolator)
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
import math


#-----------------------------------------------------------------------------
Expand All@@ -33,9 +32,9 @@ def dipole_potential(x, y):
min_radius = 0.2
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] +=math.pi/n_angles
angles[:, 1::2] +=np.pi /n_angles

x = (radii*np.cos(angles)).flatten()
y = (radii*np.sin(angles)).flatten()
Expand All@@ -46,10 +45,9 @@ def dipole_potential(x, y):
triang = Triangulation(x, y)

# Mask off unwanted triangles.
xmid = x[triang.triangles].mean(axis=1)
ymid = y[triang.triangles].mean(axis=1)
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
triang.set_mask(mask)
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
y[triang.triangles].mean(axis=1))
< min_radius)

#-----------------------------------------------------------------------------
# Refine data - interpolates the electrical potential V
Expand Down
14 changes: 6 additions & 8 deletionsexamples/images_contours_and_fields/tripcolor_demo.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,6 @@
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
import math

###############################################################################
# Creating a Triangulation without specifying the triangles results in the
Expand All@@ -20,22 +19,21 @@
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2 *math.pi, n_angles, endpoint=False)
angles = np.linspace(0, 2 *np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] +=math.pi / n_angles
angles[:, 1::2] +=np.pi / n_angles

x = (radii * np.cos(angles)).flatten()
y = (radii * np.sin(angles)).flatten()
z = (np.cos(radii) * np.cos(angles *3.0)).flatten()
z = (np.cos(radii) * np.cos(3 *angles)).flatten()

# Create the Triangulation; no triangles so Delaunay triangulation created.
triang = tri.Triangulation(x, y)

# Mask off unwanted triangles.
xmid = x[triang.triangles].mean(axis=1)
ymid = y[triang.triangles].mean(axis=1)
mask = np.where(xmid * xmid + ymid * ymid < min_radius * min_radius, 1, 0)
triang.set_mask(mask)
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
y[triang.triangles].mean(axis=1))
< min_radius)

###############################################################################
# tripcolor plot.
Expand Down
12 changes: 5 additions & 7 deletionsexamples/images_contours_and_fields/triplot_demo.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,6 @@
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
import math

###############################################################################
# Creating a Triangulation without specifying the triangles results in the
Expand All@@ -20,9 +19,9 @@
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2 *math.pi, n_angles, endpoint=False)
angles = np.linspace(0, 2 *np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] +=math.pi / n_angles
angles[:, 1::2] +=np.pi / n_angles

x = (radii * np.cos(angles)).flatten()
y = (radii * np.sin(angles)).flatten()
Expand All@@ -31,10 +30,9 @@
triang = tri.Triangulation(x, y)

# Mask off unwanted triangles.
xmid = x[triang.triangles].mean(axis=1)
ymid = y[triang.triangles].mean(axis=1)
mask = np.where(xmid * xmid + ymid * ymid < min_radius * min_radius, 1, 0)
triang.set_mask(mask)
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
y[triang.triangles].mean(axis=1))
< min_radius)

###############################################################################
# Plot the triangulation.
Expand Down
2 changes: 1 addition & 1 deletionexamples/lines_bars_and_markers/nan_test.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,7 +9,7 @@
importmatplotlib.pyplotasplt

t=np.arange(0.0,1.0+0.01,0.01)
s=np.cos(2*2*np.pi*t)
s=np.cos(2*2*np.pi*t)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious: why isthis the one where you don't have spaces around it?

Copy link
Contributor

Choose a reason for hiding this comment

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

nm...I guess I see it now.

t[41:60]=np.nan

plt.subplot(2,1,1)
Expand Down
2 changes: 1 addition & 1 deletionexamples/misc/multipage_pdf.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,7 +35,7 @@

plt.rc('text', usetex=False)
fig = plt.figure(figsize=(4, 5))
plt.plot(x, x*x, 'ko')
plt.plot(x, x ** 2, 'ko')
plt.title('Page Three')
pdf.savefig(fig) # or you can pass a Figure object to pdf.savefig
plt.close()
Expand Down
4 changes: 2 additions & 2 deletionsexamples/misc/pythonic_matplotlib.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,7 +64,7 @@
fig = figure(1)

ax1 = fig.add_subplot(211)
ax1.plot(t, sin(2*pi*t))
ax1.plot(t, sin(2*pi *t))
ax1.grid(True)
ax1.set_ylim((-2, 2))
ax1.set_ylabel('1 Hz')
Expand All@@ -74,7 +74,7 @@


ax2 = fig.add_subplot(212)
ax2.plot(t, sin(2*2*pi*t))
ax2.plot(t, sin(2 *2*pi *t))
ax2.grid(True)
ax2.set_ylim((-2, 2))
l = ax2.set_xlabel('Hi mom')
Expand Down
2 changes: 1 addition & 1 deletionexamples/misc/table_demo.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,7 +29,7 @@
bar_width = 0.4

# Initialize the vertical-offset for the stacked bar chart.
y_offset = np.array([0.0] *len(columns))
y_offset = np.zeros(len(columns))

# Plot bars and create text labels for the table
cell_text = []
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp