I want to plot things with Mathematica, inside an ipynb Python notebook. I can easily plot functions that are expressed in the Wolfram Language, e.g. with

from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wlexpr, wl
from PIL import Image
import io

with WolframLanguageSession() as session:
    plot = session.evaluate(wlexpr('Plot[Sin[x], {x,0, Pi}]'))
    img_data = session.evaluate(wl.ExportByteArray(plot, 'PNG'))
    img = Image.open(io.BytesIO(img_data))

but what I really want to do is to plot certain outputs of my machine learning model. Let's say I wrap that into some python function like

def compute_something(x: float, y: float) -> float:
    # ...

and now I want to create a ContourPlot of this function. Something like this:

    'ContourPlot[computeSomething[x, y], {x, 0, 1}, {y, 0, 1}]'

But how do I pass the numeric python function compute_something to the session so it is represented as computeSomething in Mathematica? Remember that I am in an iPython notebook with some Python kernel already running and holding this function.

(P.S.: I really really don't want to use matplotlib.)

  • $\begingroup$ I suspect something like computeSomething = ExternalFunction[session, "compute_something"] should work, but what is the session here, when I started the Mathematica Kernel from Python? $\endgroup$
    – JEM_Mosig
    Apr 9, 2022 at 11:32

1 Answer 1


Assuming we have a sample function in python:

def computeSomething(x,y):
    return x+y

Here are three possible solutions:

1 - Easiest

Generate data points in Python, export the table, and use ListContourPlot (assuming you have numpy):

In Python

import numpy as np

# generate a table
data=[[x,y,computeSomething(x,y)] for x in np.arange(0,1,.2) for y in np.arange(0,1,.25)]

# export the data
np.savetxt(r'C:\file.csv',d, fmt='%s', delimiter=",")

In Mathematica

data = Import["C:\\file.csv", "CSV"];



enter image description here

2 - Advanced (blocking)

Use a socket to communicate results from Python to Wolfram. We'll use 0MQ instead of the plain python TCP socket. You should install pyzmq (the same library Mathematica uses for Python ExternalEvaluate)

In Python

Now, we'll create a socket and bind your function, so on receiving input, output would be calculated and the result would be sent back.

import zmq

HOST = ""  # localhost
PORT = 65435  # non-privileged ports are > 1023

context = zmq.Context()
socket = context.socket(zmq.REP)

with socket:
    while True:
        message = socket.recv().decode()
        if message=='quit':

        # print (f"[LOG] Received request: \"{message}\"")

        x,y=message.split(' ')

In Mathematica

We'll connect to the socket and define a connector function:

socket = SocketConnect["tcp://", {"ZMQ", "Request"}]


computeSomething[x_?NumberQ, y_?NumberQ] := (WriteString[socket, ToString[x] <> " " <> ToString[y]];


ContourPlot[computeSomething[x, y], {x, 0, 1}, {y, 0, 1}, 
  MaxRecursion -> 0, PlotPoints -> 2, 
  PerformanceGoal -> "Speed"]


enter image description here

The Python section is running and is waiting for input, so If you're done with your Python side, send "quit" to stop that.

WriteString[socket, "quit"]

And close Mathematica socket:


3 - Advanced (non-blocking)

With my limited knowledge, here is another approach that uses IPython IOLoop.

In Python

import zmq
from zmq.eventloop.future import Context
from zmq.eventloop.zmqstream import ZMQStream

HOST = ""  # localhost
PORT = 65435  # non-privileged ports are > 1023
context = Context.instance()
socket = context.socket(zmq.REP)
stream = ZMQStream(socket)

def temp(message):
    x,y=message.result()[0].decode().split(' ')


# ioloop.IOLoop.current().start() is not needed because IPython is already started a loop, so it'll raise error

Use Solution 2 Mathematica code to connect to the server and plot the result.


  • I used minimal quality in the Advance section and it took on average under 1 second (sometimes the output was not right, re-evaluating will help)
  • On higher quality, it'll take a long time to calculate (you can mix the above solutions to create the table inside Mathematica)
  • Internal StringToMReal was used to convert String to Real, it's undocumented and is not guaranteed to work on all versions (in fact it was renamed for consistency)
  • $\begingroup$ This looks good! But it requires the event loop and the call to Mathematica to run synchronously. It would be great to show some code for how to do this (e.g. with asyncio). $\endgroup$
    – JEM_Mosig
    Apr 10, 2022 at 8:12
  • 1
    $\begingroup$ @JEM_Mosig Non-blocking method gets added. $\endgroup$
    – Ben Izd
    Apr 10, 2022 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.