我创建了一些类供其他用户使用和提供便利功能,以便轻松获得一个图,包括图形和轴生成.
import pylab as plt
def plot_something():
fig, ax = plt.subplots()
plt.plot(xrange(10), axes=ax)
return fig, ax
def even_more_impressive_plot():
fig, ax = plt.subplots()
plt.plot([x**2 for x in xrange(10)], axes=ax)
return fig, ax
Run Code Online (Sandbox Code Playgroud)
但是,有一个疯狂的用户,希望能够使用这些情节,但在一个数字并排,我的第一个想法没有成功:
desired_fig, desired_axes = plt.subplots(2)
dummy_fig, dummy_ax = plot_something()
dummy_fig2, dummy_ax2 = even_more_impressive_plot()
desired_axes[0] = dummy_ax
desired_axes[1] = dummy_ax2
plt.show()
Run Code Online (Sandbox Code Playgroud)
但遗憾的是,这使得理想的空闲了.是否有一个简单的方法或我必须拆分这样的方法:
def _plot_something(ax):
plt.plot(xrange(10), axes=ax)
def plot_something2():
fig, ax = plt.subplots()
_plot_something(ax)
return fig, ax
Run Code Online (Sandbox Code Playgroud)
您可以设计绘图函数,使其能够使用fig和ax对象(如果它们作为参数传递):
def plot_something(fig=None, ax=None):
if fig is None and ax is None:
fig, ax = plt.subplots()
elif fig is None:
fig = ax.get_figure()
elif ax is None:
ax = fig.gca()
ax.plot(xrange(10))
return fig, ax
Run Code Online (Sandbox Code Playgroud)
然后,每当你需要使用相同的数字但不同的斧头时,你可以将它们作为参数传递给你的绘图函数:
desired_fig, desired_axes = plt.subplots(2)
dummy_fig, dummy_ax = plot_something(desired_fig, desired_axes[0]) # or desired_axes[1] depending on where you need to plot
Run Code Online (Sandbox Code Playgroud)
或者你可以设计一个不同的功能来使用轴阵列......
| 归档时间: |
|
| 查看次数: |
3291 次 |
| 最近记录: |