Generating matplot figure

Is it possible to generate a matplot plot using a daisi function?
For this daisi: Multi-Depot Package Allocation
GitHub Repo: https://github.com/soul0101/multi-depot-package-allocation-daisi
I wanted the user to be able to generate a matplot figure from the result,
The code I am using

def get_allocations_plot_matplot(ax, allocation_result):
    """
    Returns a Matplotlib Figure
    """
    return helper.plot_allocation_result_matplot(ax, allocation_result)

helper.py

def plot_allocation_result_matplot(ax, allocation_result):
    color = cm.Dark2(np.linspace(0, 1, 100))

    x_depot = [] 
    y_depot = []
    depot_ids = []
    x_drop = []
    y_drop = []
    drop_ids = []

    for depot in allocation_result:
        c = random.choice(color)
        depot_location = depot["depot_location"]
        drops = depot["drops"]
        x_depot.append(depot_location[0])
        y_depot.append(depot_location[1])
        depot_ids.append(depot["depot_id"])

        if len(drops) > 0:
            for drop in drops:
                drop_location = drop["drop_location"]
                x_drop.append(drop_location[0])
                y_drop.append(drop_location[1])
                drop_ids.append(drop["drop_id"])
                ax.plot([depot_location[0], drop_location[0]], [depot_location[1], drop_location[1]], c = c, alpha=0.7, zorder=1)
    
    ax.scatter(x=x_depot, y=y_depot, color='r', s=100, zorder=2, label='Service Centers')
    ax.scatter(x=x_drop, y=y_drop, color='b', zorder=0, alpha = 0.7, label='Drops')
    
    return ax

Trying to use this as:

import pydaisi as pyd
import matplotlib.pyplot as plt
import helper

multi_depot_package_allocation = pyd.Daisi("soul0101/Multi-Depot Package Allocation")
"""
Loading data from local csv file
"""
[depot_locations, drop_locations, depot_ids, drop_ids, depot_capacity] = helper.read_data()

result = multi_depot_package_allocation.allocate_packages(depot_locations, drop_locations, depot_ids, drop_ids, depot_capacity).value
fig, ax = plt.subplots()
ax = multi_depot_package_allocation.get_allocations_plot_matplot(ax, result).value
plt.show()

For now I am using plotly which works as intended but was wondering if this was extendable to matplot too

It should be possible. I think that it will work if your return a matplotlib fig object, instead of ax

I did try that as well, but that isn’t working :confused:
I think its because for matplot the plt.show() method is not bound to fig or ax but to the environment in which it is being plot? I might be wrong about this

I have it working inside a Jupyter notebook.
For instance the following works:

%matplotlib notebook

import numpy as np
import matplotlib.pyplot as plt

import pydaisi as pyd
primary_cracking = pyd.Daisi("laiglejm/Primary Cracking")

result = primary_cracking.compute_cracking(OF='A', HI=500, alpha=2.0).value
# Should display the figure automatically

That said I can reproduce the problem to display the figure when executing a Python script. I am looking into it…

Hi @soul0101 !

I have this minimal example working fine.
It uses this Daisi : figtest

In a Python script, the following works:

import pydaisi as pyd
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('MacOSX')

figtest = pyd.Daisi("laiglejm/figtest")

fig = figtest.get_fig().value

plt.show()

Note that I have to set explicitly the matplotlib backend to be able to render the plot.
I am going to look now more in detail into your example and will keep you posted.

Hey, thanks a lot for replying!
I tried running the same code in my machine and initially I received the following error:

AttributeError: Can't get attribute '_picklable_class_constructor' on <module 'matplotlib.cbook' from 'C:\\Users\\soul\\anaconda3\\envs\\face\\lib\\site-packages\\matplotlib\\cbook\\__init__.py'>

Reviewing this StackOverflow question I though maybe it is because of the version mismatch of matplotlib on my machine and on the daisi
After upgrading the matplot module on my machine I receive the following error:

Traceback (most recent call last):
  File "c:\Users\soul\Documents\Coding\Hackathons\daisi\test.py", line 10, in <module>
    fig = figtest.get_fig().value
  File "C:\Users\soul\anaconda3\envs\face\lib\site-packages\pydaisi\pydaisi.py", line 175, in value
    return trio.run(self.fetch_result)
  File "C:\Users\soul\anaconda3\envs\face\lib\site-packages\trio\_core\_run.py", line 1946, in run
    raise runner.main_task_outcome.error
  File "C:\Users\soul\anaconda3\envs\face\lib\site-packages\pydaisi\pydaisi.py", line 270, in fetch_result
    self._result = _load_dill_string(self._result)
  File "C:\Users\soul\anaconda3\envs\face\lib\site-packages\pydaisi\pydaisi.py", line 33, in _load_dill_string
    return dill.loads(codecs.decode(s.encode(), "base64"))
  File "C:\Users\soul\anaconda3\envs\face\lib\site-packages\dill\_dill.py", line 387, in loads
    return load(file, ignore, **kwds)
  File "C:\Users\soul\anaconda3\envs\face\lib\site-packages\dill\_dill.py", line 373, in load
    return Unpickler(file, ignore=ignore, **kwds).load()
  File "C:\Users\soul\anaconda3\envs\face\lib\site-packages\dill\_dill.py", line 646, in load
    obj = StockUnpickler.load(self)
  File "C:\Users\soul\anaconda3\envs\face\lib\site-packages\matplotlib\figure.py", line 2911, in __setstate__
    mgr = plt._backend_mod.new_figure_manager_given_figure(num, self)
AttributeError: 'NoneType' object has no attribute 'new_figure_manager_given_figure'

That being said I have to remove the following line since I am on a windows machine

matplotlib.use('MacOSX')

Else throws me the error:

ImportError: cannot import name '_macosx' from 'matplotlib.backends' (C:\Users\soul\anaconda3\envs\face\lib\site-packages\matplotlib\backends\__init__.py)

The backend my system uses by default is ‘TkAgg’

I tried the accepted answer of this StackOverflow but looks like the error is at parsing the response from the daisi itself.