简体   繁体   中英

Save Visu gantt chart output to a .png file

import docplex.cp.utils_visu as visu

In a Constraint programming docplex code I have been able to show a gantt chart using visu .I can show that gantt using

Visu.show()

I need to save this output as a .png file .Is there any thing I can do.

If you take for example the public visu_flow_shop.py example and replace the line

visu.show()

by

visu.show()
import matplotlib.pyplot as plt
plt.savefig('figure.png')

then you will get the plot in the specified png file.

Alternatively, in case you have to do this more frequently, you can use the answer I provided here : Create a context manager that temporarily reroutes the plt.show() function:

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 this you can then do

with Show('figure.png'):
    visu.show()

to write the figure to a file rather than display it.

The best option would probably to file a change request with docplex and ask for a savefig() function in the visu class/package.

The window that is displayed when visu.show() is called contains a button that allows you to save the image as a PNG file. Advantage is that you can first resize the window to the appropriate size for a better display. The image saving will follow the required size. 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM