As my experimentation continues, I wanted to get Visual Studio Code
installed on a mac, and wanted to use python as the language of choice - main reason for the mac is to understand and explore the #ML libraries, runtimes, and their support on a mac (both natively and in containers - docker).
Now, Microsoft has a very nice tutorial
to get VSCode setup and running on a mac, including some basic configuration (e.g. touchbar support). But when it comes to getting
python setup
, and running, that is a different matter. Whilst the tutorial is good, it doesn’t actually work and errors out.
Below is the code that Microsoft outlines
in the tutorial for python. It essentially is the HelloWorld using packages and is quite simple; but this will fail and won’t work.
importmatplotlib.pyplotaspltimportnumpyasnpx = np.linspace(0, 20, 100) # Create a list of evenly-spaced numbers over the rangeplt.plot(x, np.sin(x)) # Plot the sine of each x pointplt.show() # Display the plot
When you run this, you will see an error that is something like the one outlined below.
The main reason this fails is that one has to be a little more explicit with matplot (the library that we are trying to use). Matplot has this concept of backends
, which essentially is the runtime dependencies needed to support various execution environments - including both interactive and non-interactive environments.
For matplot to work on a mac, the raster graphics c++ library that it uses is based on something called Anti-Grain Geometry (AGG)
. And for the library to render, we need to be explicit on which agg to use (there are multiple raster libraries).
In addition on a mac OS X there is a limitation when rendering in OSX windows (presently lacks blocking show() behavior when matplotlib is in non-interactive mode).
To get around this, we explicitly tell matplot to use the specific agg (“TkAgg in our case) and then it will all work. I have a updated code sample below, which adds more points, and also waits for the console input, so one can see what the output looks like.
importmatplotlibmatplotlib.use("TkAgg")
frommatplotlibimport pyplot as plt
importnumpyasnpdefwaitforuser():
input("Press enter to continue ...")
returnx = np.linspace(0, 50, 200) # Create a list of evenly-spaced numbers over the rangey = np.sin(x)
print(x)
waitforuser()
print(y)
waitforuser()
plt.plot(x,y)
plt.show()
And incase you are wondering what it looks like, below are a few screenshots showing the output.