Visualizing an interaction with Mathematica and Python

After first blogs on how to launch Stata and visualize high-frequency data in a Jupyter Notebook. In the following example, you will first see that Mathematica offers a very simple syntax for visualizing interactions terms. In a second step, you will see that you can launch Python within Mathematica to produce the same figure.

Suppose that your empirical estimate gives the following result:

y = (-0.1610) x + (-2.0514) z + 0.0449 (x*z)

It might be a little tricky to interpret and to find the turning points for this nonlinear equation, especially when one of the explanatory variables can take negative values.

In Mathematica, enter the following commands to produce the figure (the “;” allows hiding the output for a particular line):

y = (-0.1610) x + (-2.0514) z + 0.0449 (x*z);
m = Plot3D[y, {x, 44, 48}, {z, 2, -5}, ColorFunction -> "Rainbow", 
  AxesLabel -> {"Reserves", "FX Inter.", "dFX"}, 
  FormatType -> StandardForm, PlotLegends -> Automatic, Mesh -> 3, 
  ImageSize -> Large, BoxRatios -> {1, 1, 0.5}]

Before saving the figure in PNG format (I recommend PNG format with a large ‘ImageSize’), you can find your working directory with the following commands:

Export[NotebookDirectory[] <> "3Dplot.png", m, ImageSize -> 1600]

Now, you need to find the folder where Python is installed in your computer:


On my computer, I have multiple installations, so I choose the one in the Anaconda folder:

RegisterExternalEvaluator["Python", "C:\\ProgramData\\anaconda3\\python.exe"]

Then, I launch an External Session of Python from my Mathematica notebook:

session = StartExternalSession["Python"]

Choose an “External Language” cell in the toolbar and then enter the following Python code:

import numpy as np
import matplotlib.pyplot as plt

# Define the range for x and z
x_vals = np.linspace(44, 48, 100)
z_vals = np.linspace(-5, 2, 100)

x, z = np.meshgrid(x_vals, z_vals)
y = (-0.1610 * x) + (-2.0514 * z) + 0.0449 * (x * z) 

fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')

# Use colormap as "rainbow" and add labels to the axes
surf = ax.plot_surface(x, z, y, cmap='rainbow', edgecolor='none')

# Adjust box ratio
ax.set_box_aspect([1, 1, 0.5])  # Adjust the box ratio here

# Add a smaller color bar
colorbar = fig.colorbar(surf, ax=ax, pad=0.1, shrink=0.5, aspect=20)  # Adjust the shrink value

# Add labels
ax.set_ylabel("FX Inter.")

# adjust the view angle

ax.view_init(elev=20, azim=-42)

# Apply tight layout

# Specify the folder path to save the plot
save_folder = 'C:\\Users\\jamel\\Dropbox\\Documents\\Wolfram Mathematica\\Webinars\\WolframLanguageForPythonUsers\\WolframLanguageForPythonUsers\\'

# Save the plot as a PNG image in the specified folder
plt.savefig(save_folder + '3d_surface_plot.png', dpi=300)  # Specify the file name and DPI

# Display the plot

Finally, I produced a similar picture as with the Mathematica notebook:

You have seen in a few steps that visualizing an interaction is accessible using Mathematica and Python. Besides, it could help you to understand the implications of your nonlinear model.

Exchange rate interventions are more powerful for large holders of International Reserves (above around 46% of Reserves-to-GDP ratio).

More information about the variables used in this example:


  • Rashad Ahmed, Joshua Aizenman, Jamel Saadaoui, Gazi Salah Uddin (July 2023), On the Effectiveness of Foreign Exchange Reserves During the 2021-22 U.S. Monetary Tightening Cycle. NBER Working Paper Series, 30935,

1 Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.