Ask Your Question
2

Using matplotlib inside Sage

asked 2016-08-22 20:27:30 +0100

Federico gravatar image

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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-08-23 21:17:59 +0100

ndomes gravatar image

Try

plt.savefig('mychart.png')

instead of

plt.show()
edit flag offensive delete link more

Comments

And if you want to see the image file:

from PIL import Image
image = Image.open('mychart.png')
image.show()
paulmasson gravatar imagepaulmasson ( 2016-08-23 22:22:41 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2016-08-22 20:27:30 +0100

Seen: 2,076 times

Last updated: Aug 23 '16