Matplotlib:检查空图
Matplotlib: Check for empty plot
我有一个循环加载和绘制一些数据,像这样:
import os
import numpy as np
import matplotlib.pyplot as plt
for filename in filenames:
plt.figure()
if os.path.exists(filename):
x, y = np.loadtxt(filename, unpack=True)
plt.plot(x, y)
plt.savefig(filename + '.png')
plt.close()
现在,如果文件不存在,则不会加载或绘制数据,但仍会保存一个(空)图形。在上面的例子中,我可以简单地通过在 if
语句中包含所有 plt
调用来纠正这个问题。我的实际用例涉及更多,所以我正在寻找一种方法来询问 matplotlib
/plt
/figure/the 轴 figure/axis 是否完全空与否。像
for filename in filenames:
plt.figure()
if os.path.exists(filename):
x, y = np.loadtxt(filename, unpack=True)
plt.plot(x, y)
if not plt.figure_empty(): # <-- new line
plt.savefig(filename + '.png')
plt.close()
检查带有fig.get_axes()
的图中是否有坐标轴是否符合您的目的?
fig = plt.figure()
if fig.get_axes():
# Do stuff when the figure isn't empty.
如您所说,显而易见的解决方案是在 if
语句
中包含保存
for filename in filenames:
plt.figure()
if os.path.exists(filename):
x, y = np.loadtxt(filename, unpack=True)
plt.plot(x, y)
plt.savefig(filename + '.png') # <-- indentation here
plt.close()
否则,这将取决于 "empty" 的真正含义。如果是一个图形不包含任何坐标轴,
for filename in filenames:
fig = plt.figure()
if os.path.exists(filename):
x, y = np.loadtxt(filename, unpack=True)
plt.plot(x, y)
if len(fig.axes) > 0:
plt.savefig(filename + '.png')
plt.close()
然而,这些在某种程度上是解决方法。我认为您真的想自己执行逻辑步骤。
for filename in filenames:
plt.figure()
save_this = False
if os.path.exists(filename):
x, y = np.loadtxt(filename, unpack=True)
plt.plot(x, y)
save_this = True
if save_this:
plt.savefig(filename + '.png')
plt.close()
检查斧头是否有使用plot()
绘制的数据:
if ax.lines:
如果它们是使用 scatter()
绘制的:
if ax.collections:
我有一个循环加载和绘制一些数据,像这样:
import os
import numpy as np
import matplotlib.pyplot as plt
for filename in filenames:
plt.figure()
if os.path.exists(filename):
x, y = np.loadtxt(filename, unpack=True)
plt.plot(x, y)
plt.savefig(filename + '.png')
plt.close()
现在,如果文件不存在,则不会加载或绘制数据,但仍会保存一个(空)图形。在上面的例子中,我可以简单地通过在 if
语句中包含所有 plt
调用来纠正这个问题。我的实际用例涉及更多,所以我正在寻找一种方法来询问 matplotlib
/plt
/figure/the 轴 figure/axis 是否完全空与否。像
for filename in filenames:
plt.figure()
if os.path.exists(filename):
x, y = np.loadtxt(filename, unpack=True)
plt.plot(x, y)
if not plt.figure_empty(): # <-- new line
plt.savefig(filename + '.png')
plt.close()
检查带有fig.get_axes()
的图中是否有坐标轴是否符合您的目的?
fig = plt.figure()
if fig.get_axes():
# Do stuff when the figure isn't empty.
如您所说,显而易见的解决方案是在 if
语句
for filename in filenames:
plt.figure()
if os.path.exists(filename):
x, y = np.loadtxt(filename, unpack=True)
plt.plot(x, y)
plt.savefig(filename + '.png') # <-- indentation here
plt.close()
否则,这将取决于 "empty" 的真正含义。如果是一个图形不包含任何坐标轴,
for filename in filenames:
fig = plt.figure()
if os.path.exists(filename):
x, y = np.loadtxt(filename, unpack=True)
plt.plot(x, y)
if len(fig.axes) > 0:
plt.savefig(filename + '.png')
plt.close()
然而,这些在某种程度上是解决方法。我认为您真的想自己执行逻辑步骤。
for filename in filenames:
plt.figure()
save_this = False
if os.path.exists(filename):
x, y = np.loadtxt(filename, unpack=True)
plt.plot(x, y)
save_this = True
if save_this:
plt.savefig(filename + '.png')
plt.close()
检查斧头是否有使用plot()
绘制的数据:
if ax.lines:
如果它们是使用 scatter()
绘制的:
if ax.collections: