[英]Creating and referencing separate matplotlib plots
What's the best practice to create multiple separate plots using matplotlib, so they can be called later or output into a pdf report?使用 matplotlib 创建多个单独的图的最佳实践是什么,以便稍后调用它们或输出到 pdf 报告中? I'm a bit unclear as to how to do this in a way that retains each plot in memory (like we could with dataframes) for later reference.
我有点不清楚如何以一种将每个图保留在内存中的方式(就像我们可以使用数据框一样)以供以后参考。
Suppose we have this code:假设我们有这样的代码:
%pylab inline
x1 = np.random.randn(50)*100
y1 = np.random.randn(50)*100
x2 = np.random.randn(50)*100
y2 = np.random.randn(50)*100
and the intent is to create 2 separate plots of (x1,y1) and (x2,y2) and 'save' them in some way to be referenced later.目的是创建 (x1,y1) 和 (x2,y2) 的 2 个独立图,并以某种方式“保存”它们以供以后参考。 the intent is to be able to output these into a PDF (perhaps via reportlab).
目的是能够将这些输出到 PDF 中(可能通过 reportlab)。 the relationship between "figures", "subplots" and "axes" is confusing to me and not sure what is optimal for this purpose.
“数字”、“子图”和“轴”之间的关系让我感到困惑,不知道什么是最佳的。 i started with an approach like:
我从一种方法开始:
plt.figure(1, figsize=(8, 6))
plt.subplot(211)
plt.scatter(x1, y1, c = 'r', alpha = 0.3)
plt.subplot(212)
plt.scatter(x2, y2, c = 'k', alpha = 0.7)
plt.show()
which does technically work, but i'm not sure how i can refer to these later.这在技术上确实有效,但我不确定以后如何参考这些。 also, i am using a small example here for illustration, but in practice i may have many more of these.
另外,我在这里使用一个小例子来说明,但实际上我可能有更多这样的例子。
With the implicit style that the question uses (where the figure object is not saved in a variable, and where plotting commands apply to the current figure), you can easily make a previous figure the current figure:使用问题使用的隐式样式(图形对象未保存在变量中,并且绘图命令适用于当前图形),您可以轻松地将上一个图形设为当前图形:
plt.figure(1)
will thus reactivate figure 1. plt.savefig()
can then be used, additional plots can be made in it, etc.因此将重新激活图 1。然后可以使用
plt.savefig()
,可以在其中制作其他绘图等。
Furthermore, you can also give a name to your figure when you create it, and then refer to it:此外,您还可以在创建图形时为其命名,然后引用它:
plt.figure("growth", figsize=…)
…
plt.figure("counts", figsize=…)
…
plt.figure("growth") # This figure becomes the active one again
(the figure reference parameter is called num
, but it doesn't have to be a number and can be a string , which makes for a clearer code). (图形参考参数称为
num
,但它不必是数字, 也可以是 string ,这样代码更清晰)。
Things might make more sense if you start to use the object-oriented interface to matplotlib.如果您开始使用面向对象的接口到 matplotlib,事情可能会更有意义。 In that case, you could do something like:
在这种情况下,您可以执行以下操作:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8, 6))
ax1 = fig.add_subplot(211)
ax1.scatter(x1, y1, c = 'r', alpha = 0.3)
ax2 = fig.add_subplot(212)
ax2.scatter(x2, y2, c = 'k', alpha = 0.7)
plt.show()
In this way, its easy to see that ax1
and ax2
belong to the figure instance, fig
.这样,很容易看出
ax1
和ax2
属于图形实例fig
。 You can then later refer back to ax1
and ax2
, to plot more data on them, or adjust the axis limits, or add labels, etc., etc.然后您可以稍后参考
ax1
和ax2
,在它们上绘制更多数据,或调整轴限制,或添加标签等。
You can also add another figure, with its own set of subplots:您还可以添加另一个图形,它有自己的一组子图:
fig2 = plt.figure(figsize=(8, 6))
ax3 = fig2.add_subplot(211)
and then you can save the given figures at any point, and know that you are always referring to the correct figure:然后您可以随时保存给定的数字,并且知道您始终指的是正确的数字:
fig.savefig('figure1.png')
fig2.savefig('figure2.png')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.