问题描述
五个月以来,我每天都在使用 matplotlib,但我仍然发现创建新数字令人困惑.
I've been using matplotlib for five months now on a daily basis, and I still find creation of new figures confusing.
通常,我使用诸如以下的东西来创建一个具有2x2子图的图形:
Usually I create a figure with 2x2 subplots using, for example, somthing like:
import matplotlib.pyplot as plt
import itertools as it
fig,axes = plt.subplots(2,2)
axit = (ax for ax in it.chain(*axes))
for each of four data series I want to plot:
ax = next(axit)
ax.plot(...)
我现在的问题是:如何完全独立于 pyplot 进行操作,即,我如何创建一个图形,用绘图填充它,进行样式更改,并且只告诉该图形在我想要的确切时刻出现出现.这是我遇到的麻烦:
The question I have now is: how can operate completely independently of pyplot, ie, how can I create a figure, populate it with plots, make style changes, and only tell that figure to appear at the exact moment I want it to appear. Here is what I am having trouble with:
import matplotlib as mpl
gs = gridspec.GridSpec(2,2)
fig = mpl.figure.Figure()
ax1 = fig.add_subplot(gs[0])
ax1.plot([1,2,3])
ax2 = fig.add_subplot(gs[1])
ax2.plot([3,2,1])
运行上述命令后,唯一想到的就是使用:
After running the above, the only thing that comes to mind would be to use:
plt.draw()
但这行不通.使带有图的图形出现的缺失是什么?另外,是
But this does not work. What is missing to make the figure with the plots appear? Also, is
fig = mpl.figure.Figure()
我需要做的就是在没有 pyplot 的情况下创建图形?
all I have to do to create the figure without pyplot?
推荐答案
您可以附加适当的后端手动显示您的身材,然后显示它:
You could attach a suitable backend to your figure manually and then show it:
from matplotlib.backends import backend_qt4agg # e.g.
backend_qt4agg.new_figure_manager_given_figure(1, fig)
fig.show()
...但为什么不使用 pyplot
?
... but why not use pyplot
?
这篇关于如何在不使用 pyplot 的情况下在 matplotlib 中创建绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!