问题
When using the bezier library, the Curve.plot() function returns an AxesSubplot object
nodes1 = np.array([[0.0, 0.0],[0.625, .75], [1.0, 1.0]])
curve1 = bezier.Curve(nodes1, degree=2)
ax = curve1.plot(num_pts=256)
print ax
returns an
AxesSubplot(0.125,0.11;0.775x0.77)
I know the typical way of creating a subplot is with
fig = pyplot.figure()
ax = fig.add_subplot(111)
but I can't find any documentation on adding an already created subplot to a figure.
I can display the subplot with plt.show() but can't access the figure. If I try to create a figure with plt.figure(), two different figures (in different windows) are displayed.
回答1:
On many Python third party libraries (including matplotlib), you can look at the source and see what is going on behind the scenes and often you can use that as a guide for what you want to do - maybe even an opportunity for a subclass to implement your customization or. bezier.Curve.plot source is pretty straightforward and if your intent is to just plot the curve you can use its code, in your own (bezier.Curve.evaluate_multi
returns numpy.ndarray: The points on the curve. As a two dimensional NumPy array, with the rows corresponding to each *s* value and the columns to the dimension.
nodes1 = np.array([[0.0, 0.0],[0.625, .75], [1.0, 1.0]])
curve1 = bezier.Curve(nodes1, degree=2)
# if you need to create x values.
s_vals = np.linspace(0.0, 1.0, num_pts)
points = curve1.evaluate_multi(s_vals)
Where the x values are points[:, 0]
and the y values are points[:, 1]
. Depending on what you actually need... (from pylab_examples example code: subplots_demo.py
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(points[:, 0], points[:, 1])
ax1.set_title('Sharing Y axis')
ax2.scatter(points[:, 0], points[:, 1])
I haven't actually tried this - lacking bezier
on this machine.
回答2:
If the aim is to plot a curve to an existing axes ax
, use the ax
argument of the plotting function, bezier.Curve.plot(..., ax)
:
nodes1 = np.array([[0.0, 0.0],[0.625, .75], [1.0, 1.0]])
curve1 = bezier.Curve(nodes1, degree=2)
# create axes with matplotlib
fig, ax = plt.subplots()
# plot curve to existing axes
curve1.plot(num_pts=256, ax=ax)
Alternatively, if you have problems using the bezier package, creating a Bezier curve is not actually that hard. So you may just do it manually:
import numpy as np
from scipy.special import binom
import matplotlib.pyplot as plt
bernstein = lambda n, k, t: binom(n,k)* t**k * (1.-t)**(n-k)
def bezier(points, num=200):
N = len(points)
t = np.linspace(0, 1, num=num)
curve = np.zeros((num, 2))
for i in range(N):
curve += np.outer(bernstein(N - 1, i, t), points[i])
return curve
nodes1 = np.array([[0.0, 0.0],[0.625, .75], [1.0, 1.0]])
curve1 = bezier(nodes1, num=256)
fig, ax = plt.subplots()
ax.plot(curve1[:,0], curve1[:,1])
plt.show()
回答3:
First, to avoid another window to open you must "close" the plotting:
pyplot.close()
Then you can execute your
ax = curve1.plot(num_pts=256)
Now, I don't know why do you bring in subplot(111), which uses the whole window anyway (single figure). But if you still want to use a smaller subplot (e.g. 221), you can do the following:
pyplot.close()
fig = pyplot.figure()
#fig = pyplot.gcf(); # This will also do
ax = fig.add_subplot(221); curve.plot(num_pts=256,ax=ax)
(Note: I just tested both cases and they work. Only I used 2D nodes instead of 3D.)
来源:https://stackoverflow.com/questions/46901622/adding-bezier-axes-to-matplotlib-figure