Search code examples
pythoncodespaces

Matplotlib figures not generating in GitHub CodeSpaces


I just started using Codespaces. In my python file I have this code:

import matplotlib.pyplot as plt
import pandas as pd 

print("Hello")

titanic_data = pd.read_csv("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv")
titanic_data = titanic_data[titanic_data['Age'].notnull()]
titanic_data['Fare'] = titanic_data['Fare'].fillna(titanic_data['Fare'].mean())
titanic_data = titanic_data.drop_duplicates()
plt.scatter(titanic_data['Age'], titanic_data['Fare'])
plt.show()

print("Goodbye")

When I run this on my local machine, this works perfectly. I can see the console logs, and the figure appears as a new window:

plotted figure in local environment

However, when I run this in Codespaces, I can see all of the code running without any errors, but it does not show the figure. Is this a known limitation or a feature that is not yet supported? Is there another way I can plot figures in Codespaces?

They mention this in the docs docs:

The default container image that's used by GitHub Codespaces includes a set of machine learning libraries that are preinstalled in your codespace. For example, Numpy, pandas, SciPy, Matplotlib, seaborn, scikit-learn, Keras, PyTorch, Requests, and Plotly.

It sounds like it should be supported out of the box. Is additional configuration required?


Solution

  • Based on the experimentation I have done thus far, plotting these diagrams as one would do in a local dev environment is not (yet?) possible.

    For this specific case, the next best solution was to create a new GitHub Codespace from this repo: https://github.com/education/codespaces-teaching-template-py

    Clone into Codespace

    Once the repo has been cloned into the Codespace, navigate to an existing .ipynb file or create your own.

    Inside there you'll be able to run chunks of custom code and plot figures.

    Matplotlib in Codespace

    The big limitation I see is that the figure cannot be interacted with the same way that one would be able to on a local machine (zooming, panning, etc).

    As always, don't forget to shut your Codespace down when you're done using it!