import docplex.cp.utils_visu as visu
在约束编程 docplex 代码中,我已经能够使用visu
.I 显示甘特图。我可以使用
Visu.show()
我需要将此输出另存为.png file
. 有什么我可以做的吗?
如果您以公共visu_flow_shop.py示例为例并替换该行
visu.show()
经过
visu.show()
import matplotlib.pyplot as plt
plt.savefig('figure.png')
然后您将在指定的 png 文件中获得绘图。
或者,如果您必须更频繁地执行此操作,您可以使用我在此处提供的答案:创建一个临时重新路由plt.show()
函数的上下文管理器:
import matplotlib.pyplot as plt
class Show:
'''Simple context manager to temporarily reroute plt.show().
This context manager temporarily reroutes the plt.show() function to
plt.savefig() in order to save the figure to the file specified in the
constructor rather than displaying it on the screen.'''
def __init__(self, name):
self._name = name
self._orig = None
def _save(self):
plt.savefig(self._name)
if False:
# Here we could show the figure as well
self._orig()
def __enter__(self):
self._orig = plt.show
plt.show = lambda: self._save()
return self
def __exit__(self, type, value, traceback):
if self._orig is not None:
plt.show = self._orig
self._orig = None
有了这个你就可以做到
with Show('figure.png'):
visu.show()
将图形写入文件而不是显示它。
最好的选择可能是向 docplex 提交更改请求并请求类/包savefig()
中的函数。visu