Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Using matplotlib inside Sage

I have difficulties in using matplotlib commands inside Sage.

I took a demo from matplotlib's website:

"""
Simple demo of a horizontal bar chart.
"""
import matplotlib.pyplot as plt
plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt


# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))

plt.barh(y_pos, performance, xerr=error, align='center', alpha=0.4)
plt.yticks(y_pos, people)
plt.xlabel('Performance')
plt.title('How fast do you want to go today?')

plt.show()

If I run it as python code (e.g. %run namefile.py inside ipython) it works and shows a bar chart on the screen; if I run it inside sage or sage -python namefile.py, then I see no result. I presume that Sage does some wrapping of matplotlib's output that interacts with the plotting?

How can I properly use matplotlib inside Sage code?