在我的Ipython Notebook
中,我有一个脚本生成一系列多重图形,如下所示:
问题是,这些数字take too much space
,我正在产生许多这样的组合。这使我很难在这些数字之间导航。
我想把一些情节写在同一行。我该怎么做?
更新:
感谢fjarri的建议,我已经修改了代码,这可以在同一行绘制。
现在,我想让它们以不同的行(默认选项)打印。我该怎么办?我试过一些,但不确定这是不是正确的方法。
def custom_plot1(ax = None):
if ax is None:
fig, ax = plt.subplots()
x1 = np.linspace(0.0, 5.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
ax.plot(x1, y1, 'ko-')
ax.set_xlabel('time (s)')
ax.set_ylabel('Damped oscillation')
def custom_plot2(ax = None):
if ax is None:
fig, ax = plt.subplots()
x2 = np.linspace(0.0, 2.0)
y2 = np.cos(2 * np.pi * x2)
ax.plot(x2, y2, 'r.-')
ax.set_xlabel('time (s)')
ax.set_ylabel('Undamped')
# 1. Plot in same line, this would work
fig = plt.figure(figsize = (15,8))
ax1 = fig.add_subplot(1,2,1, projection = '3d')
custom_plot1(ax1)
ax2 = fig.add_subplot(1,2,2)
custom_plot2(ax2)
# 2. Plot in different line, default option
custom_plot1()
custom_plot2()
只需使用子块。
(这段代码不应该起作用,重要的只是它背后的想法)
对于数字2,同样的事情:使用子块:
这不会显示两个不同的图形(这是我从“不同的行”中了解到的),而是将两个图形(一个在另一个之上)放在一个图形中。
现在,解释子块参数:
subplot(rows, cols, axnum)
rows
将是图形划分的行数。cols
将是图形划分的列数。axnum
将是您要绘制的分区。在您的例子中,如果您希望两个图形并排,则需要一行两列-->;
subplot(1,2,...)
在第二种情况下,如果希望两个图形一个在另一个上面,则需要两行一列-->;
subplot(2,1,...)
对于更复杂的分布,请使用
gridspec
http://matplotlib.org/users/gridspec.html相关问题 更多 >
编程相关推荐