IPython/matplotlib:从函数返回子批
在IPython笔记本中使用Matplotlib,我想创建一个图形,其中包含从函数返回的子图:IPython/matplotlib:从函数返回子批,python,matplotlib,ipython,Python,Matplotlib,Ipython,在IPython笔记本中使用Matplotlib,我想创建一个图形,其中包含从函数返回的子图: import matplotlib.pyplot as plt %matplotlib inline def create_subplot(data): more_data = do_something_on_data() bp = plt.boxplot(more_data) # return boxplot? return bp # make figure
import matplotlib.pyplot as plt
%matplotlib inline
def create_subplot(data):
more_data = do_something_on_data()
bp = plt.boxplot(more_data)
# return boxplot?
return bp
# make figure with subplots
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(10,5))
ax1 -> how can I get the plot from create_subplot() and put it on ax1?
ax1 -> how can I get the plot from create_subplot() and put it on ax2?
我知道我可以直接将绘图添加到轴:
ax1.boxplot(data)
但是如何从函数返回绘图并将其用作子绘图?通常,您会执行以下操作:
def create_subplot(data, ax=None):
if ax is None:
ax = plt.gca()
more_data = do_something_on_data()
bp = ax.boxplot(more_data)
return bp
# make figure with subplots
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(10,5))
create_subplot(data, ax1)
您不会“从函数返回绘图并将其用作子绘图”。相反,您需要在子地块中的轴上绘制箱线图
如果ax为None,则
部分正好在那里,因此传入显式轴是可选的(如果不是,则将使用当前的pyplot轴,与调用plt.boxplot
相同)。如果您愿意,可以省略它并要求指定特定的轴。太好了,可以了!图形和轴对象的“性质”以及绘图命令背后的逻辑在一开始可能很难理解。“相反,你需要在子地块中的轴上绘制箱线图。”这就是我需要改变思维的地方。谢谢