Removing numpy meshgrid points outside of a Shapely polygon - numpy

I have a 10 x 10 grid that I would like to remove points outside of a shapely Polygon:
import numpy as np
from shapely.geometry import Polygon, Point
from descartes import PolygonPatch
gridX, gridY = np.mgrid[0.0:10.0, 0.0:10.0]
poly = Polygon([[1,1],[1,7],[7,7],[7,1]])
#plot original figure
fig = plt.figure()
ax = fig.add_subplot(111)
polyp = PolygonPatch(poly)
ax.add_patch(polyp)
ax.scatter(gridX,gridY)
plt.show()
Here is the resulting figure:
And what I want the end result to look like:
I know that I can reshape the array to a 100 x 2 array of grid points:
stacked = np.dstack([gridX,gridY])
reshaped = stacked.reshape(100,2)
I can see if the point lies within the polygon easily:
for i in reshaped:
if Point(i).within(poly):
print True
But I am having trouble taking this information and modifying the original grid

You're pretty close already; instead of printing True, you could just append the points to a list.
output = []
for i in reshaped:
if Point(i).within(poly):
output.append(i)
output = np.array(output)
x, y = output[:, 0], output[:, 1]
It seems that Point.within doesn't consider points that lie on the edge of the polygon to be "within" it though.

Related

Extract x/y data from PolyCollection from fill_between

How can I extract the x/y data from the resulting PolyCollection from a fill_between plot?
polyCollection = ax.fill_between(x,ylo,yhi)
Now how do I get the data back from polyCollection?
For other Collection objects, I use x, y = artist.get_offsets().T, but here that returns just zeros for some reason.
For "Line" type objects, I use x, y = artist.get_xdata(), artist.get_ydata().
(I use this information in a callback to locally auto-zoom the y-axis to fit the data within a certain x-range.)
polyCollection.get_paths() gives a list of paths. In this case a list with one element. From there you can get the vertices as an Nx2 numpy array, and there the x and y:
from matplotlib import pyplot as plt
import numpy as np
N = 20
polyCollection = plt.fill_between(np.arange(0, N),
5 + np.random.normal(size=N).cumsum(),
10 + np.random.normal(size=N).cumsum(), color='lightblue', alpha=0.3)
points = polyCollection.get_paths()[0].vertices
xs = points[:, 0]
ys = points[:, 1]
plt.scatter(xs, ys, marker='o', color='crimson')

Get data from mplot3d graph

I can’t find out how to get data from an mplot3d graph. Something similar to the 2D style:
line.get_xdata()
Is it possible?
Line3D
You can get get the original data from the (private) _verts3d attribute
xdata, ydata, zdata = line._verts3d
print(xdata)
Complete example
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
# Prepare arrays x, y, z
x = np.arange(5)
y = np.arange(5)*4
z = np.arange(5)*100
line, = ax.plot(x, y, z, label='curve')
fig.canvas.draw()
xdata, ydata, zdata = line._verts3d
print(xdata) # This prints [0 1 2 3 4]
plt.show()
Some explanation: The problem with get_data or get_xdata is that it will return the projected coordinates once the figure is drawn. So while before drawing the figure, line.get_xdata() would indeed return the correct values, after drawing, it would return something like
[ -6.14413090e-02 -3.08824862e-02 -3.33066907e-17 3.12113190e-02 6.27567511e-02]
in the above example, which is the x component of the 3D coordinates projected onto 2D.
There is a pull request to matplotlib, which would allow to get the data via methods get_data_3d. This is still not merged, but might allow the above to be done without using private arguments in a future version of matplotlib.
Poly3DCollection
For a plot_surface plot this looks similar, except that the attribute to look at is the ._vec
surf = ax.plot_surface(X, Y, Z)
xdata, ydata, zdata, _ = surf._vec
print(xdata)
This issue was filed on Github and there is contribution that adds new get_data_3d and set_data_3d methods. Unfortunately, these changes is likely not yet available in distributions. So you might have to continue using private variable line._verts3d.
See more here: https://github.com/matplotlib/matplotlib/issues/8914

Map a colorbar based on plot instead of imshow

I'm trying to get a colorbar for the following minimal example of my code.
g1 = gridspec.GridSpec(1, 1)
f, ((ax0)) = plt.subplots(1, 1)
ax0 = subplot(g1[0])
cmap = matplotlib.cm.get_cmap('viridis')
for i in linspace(0,1,11):
x = [-1,0,1]
y = [i,i,i]
rgba = cmap(i)
im = ax0.plot(x,y,color=rgba)
f.colorbar(im)
I also tried f.colorbar(cmap)
Probably pretty obvious, but I get errors such as
'ListedColormap' object has no attribute 'autoscale_None'
In reality, the value defining i is more complex, but I think this should do the trick. My data is plotted with plot and not with imshow (for which I know how to make the colormap).
The answers so far seem overly complicated. fig.colorbar() expects a ScalarMappable as its first argument. Often ScalarMappables are produced by imshow or contourplots and are readily avaible.
In this case you would need to define your custom ScalarMappable to provide to the colorbar.
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
cmap = plt.cm.get_cmap('viridis')
for i in np.linspace(0,1,11):
x = [-1,0,1]
y = [i,i,i]
rgba = cmap(i)
im = ax.plot(x,y,color=rgba)
sm = plt.cm.ScalarMappable(cmap=cmap)
sm.set_array([])
fig.colorbar(sm)
plt.show()
You should pass an Image or ContourSet when you call colorbar on a Figure.
You can make an image of the data points by calling plt.imshow with the data. You can start with this:
data = []
for i in np.linspace(0,1,11):
x = [-1,0,1]
y = [i,i,i]
rgba = cmap(i)
ax0.plot(x,y,color=rgba)
data.append([x, y])
image = plt.imshow(data)
figure.colorbar(image)
plt.show()
Reference:
https://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.colorbar
Oluwafemi Sule's solution almost works, but it plots the matrix into the same figure as the lines. Here a solution that opens a second figure, does the imshow call on that second figure, uses the result to draw the colorbar in the first figure, and then closes the second figure before calling plt.show():
import matplotlib
from matplotlib import pyplot as plt
from matplotlib import gridspec
import numpy as np
cmap = matplotlib.cm.get_cmap('viridis')
g1 = gridspec.GridSpec(1, 1)
f0, ((ax0)) = plt.subplots(1, 1)
f1, ((ax1)) = plt.subplots(1, 1)
for i in np.linspace(0,1,11):
x = [-1,0,1]
y = [i,i,i]
rgba = cmap(i)
ax0.plot(x,y,color=rgba)
data = np.linspace(0,1,100).reshape((10,10))
image = ax1.imshow(data)
f0.colorbar(image)
plt.close(f1)
plt.show()
The result looks like this:

Get pixels inside a patch

In matplotlib, it's possible to get the pixels inside a polygon using matplotlib.nxutils.points_inside_poly, as long as you have vertices defined beforehand.
How can you get the points inside a patch, e.g. an ellipse?
The problem: if you define a matplotlib ellipse, it has a .get_verts() method, but this returns the vertices in figure (instead of data) units.
One could do:
# there has to be a better way to do this,
# but this gets xy into the form used by points_inside_poly
xy = np.array([(x,y) for x,y in zip(pts[0].ravel(),pts[1].ravel())])
inds = np.array([E.contains_point((x,y)) for x,y in xy], dtype='bool')
However, this is very slow since it's looping in python instead of C.
use ax.transData.transform() to transform your points, and then use points_inside_poly():
import pylab as pl
import matplotlib.patches as mpatches
from matplotlib.nxutils import points_inside_poly
import numpy as np
fig, ax = pl.subplots(1, 1)
ax.set_aspect("equal")
e = mpatches.Ellipse((1, 2), 3, 1.5, alpha=0.5)
ax.add_patch(e)
ax.relim()
ax.autoscale()
p = e.get_path()
points = np.random.normal(size=(1000, 2))
polygon = e.get_verts()
tpoints = ax.transData.transform(points)
inpoints = points[points_inside_poly(tpoints, polygon)]
sx, sy = inpoints.T
ax.scatter(sx, sy)
result:

Matplotlib histogram with errorbars

I have created a histogram with matplotlib using the pyplot.hist() function. I would like to add a Poison error square root of bin height (sqrt(binheight)) to the bars. How can I do this?
The return tuple of .hist() includes return[2] -> a list of 1 Patch objects. I could only find out that it is possible to add errors to bars created via pyplot.bar().
Indeed you need to use bar. You can use to output of hist and plot it as a bar:
import numpy as np
import pylab as plt
data = np.array(np.random.rand(1000))
y,binEdges = np.histogram(data,bins=10)
bincenters = 0.5*(binEdges[1:]+binEdges[:-1])
menStd = np.sqrt(y)
width = 0.05
plt.bar(bincenters, y, width=width, color='r', yerr=menStd)
plt.show()
Alternative Solution
You can also use a combination of pyplot.errorbar() and drawstyle keyword argument. The code below creates a plot of the histogram using a stepped line plot. There is a marker in the center of each bin and each bin has the requisite Poisson errorbar.
import numpy
import pyplot
x = numpy.random.rand(1000)
y, bin_edges = numpy.histogram(x, bins=10)
bin_centers = 0.5*(bin_edges[1:] + bin_edges[:-1])
pyplot.errorbar(
bin_centers,
y,
yerr = y**0.5,
marker = '.',
drawstyle = 'steps-mid-'
)
pyplot.show()
My personal opinion
When plotting the results of multiple histograms on the the same figure, line plots are easier to distinguish. In addition, they look nicer when plotting with a yscale='log'.