0
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.

Daniel Junglas
  • 5,830
  • 1
  • 5
  • 22
suresh_chinthy
  • 377
  • 2
  • 12
  • I think that `Visu.show()` will eventually call `plt.show` (where `plt` is `import matplotlib.pyplot as plt`). So probably you can call `plt.savefig('image.png')` after `Visu.show()` returns to write the current figure to an image. You can find a discussion about saving a `plt` to a file here: https://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib – Daniel Junglas Aug 07 '20 at 05:53
  • Hi @DanielJunglas There is still a problem,Only saving a white space only.(Checked the stack overflow comments to resolve this,ut didnt work,Would it be possible to have a look on this.) – suresh_chinthy Aug 10 '20 at 10:53
  • I tried and the strategy described by me works like a charme, at least from the command line (I did not try from a notebook). I have posted an answer that describes how to modify one of the shipped examples. If this does not work for you then you will have to show your full code to give us a chance to figure out the problem. Also make sure that you don't see any error messages in your output. – Daniel Junglas Aug 10 '20 at 13:13
  • Looks Like my code is not working @DanielJunglas.I used te job shop example,Ran from the cmd as well,But still no luck – suresh_chinthy Aug 11 '20 at 05:27
  • Well, like I said, unless you show *exactly* what code you are using, how you are running it and what the output is, we won't be able to help. – Daniel Junglas Aug 11 '20 at 08:42
  • @DanielJunglas sorry previous comment I couldnt specify it,It is from IBM github page https://ibmdecisionoptimization.github.io/tutorials/html/Scheduling_Tutorial.html – suresh_chinthy Aug 12 '20 at 09:17
  • Again, please post here the *exact* code you are executing. If possible, reduce it to a minimal working example. Then include the exact command line you are executing and all the output you get. – Daniel Junglas Aug 12 '20 at 10:22

2 Answers2

0

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.

Daniel Junglas
  • 5,830
  • 1
  • 5
  • 22
0

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. enter image description here

Olivier OUDOT
  • 186
  • 2
  • 5