# plot grids in a figure Dear all,

I want to plot grids in my plot.

I couldn't find any keyword like 'grid' in the help document of 'plot()' <function sage.plot.plot.plot="">, and hence I tried NUMERICAL PLOTTING:

sage: from pylab import *
sage: t = arange(0.0, 2.0, 0.01)
sage: s = sin(2*pi*t)
sage: P = plot(t, s, linewidth=1.0)
sage: xl = xlabel('time (s)')
sage: yl = ylabel('voltage (mV)')
sage: t = title('About as simple as it gets, folks')
sage: grid(True)
sage: savefig(os.path.join(SAGE_TMP, 'sage.png'))
sage: P
[<matplotlib.lines.Line2D at 0x7e0a050>] # stored in RAM?


but nothing came out... OK, how can I check if SAGE can call Matplotlib in my OS (openSuSE 12.3) correctly? I have installed and updated 'matplotlib' for Python 2.7 via YaST2.

edit retag close merge delete

Sort by » oldest newest most voted

Matplotlib is already shipped in Sage, and Sage uses it when it deals with plots. You can define a grid as follows:

sage: grid = Graphics()
sage: for i in range(-10,10,1):
....:     grid += line([(i,-10),(i,10)])
....:     grid += line([(-10,i),(10,i)])
sage: grid


Then you can add plots as follows:

sage: t = var('t')
sage: P = plot(sin(2*pi*t), (t,-10,10))
sage: P
sage: P + grid


EDIT: actually, there are gridlines and gridlinesstyle options, see the documentation. Example (without tuning):

sage: plot(sin(2*pi*t), (t,-10,10), gridlines=True)

more

Thanks! But is there any automatic method like 'grid on' in MATLAB? I want to add grids on a loglog or semilog plot.

var('t'); plot(sin(2*pi*t), (t,-10,10), gridlines=True, scale='loglog') But you do have to do it on a plot by plot basis.

Thanks! I am really confused where is the matplot object? <matplotlib.lines.line2d at="" 0x7e0a050=""> what does it mean? I am now learning to plot using matplotlib http://matplotlib.org/users/pyplot_tutorial.html.......... I could not let the example work in SAGE.

Use the gridlines option to any plot. Search the page here under Graphics for the term "gridlines".

more

Thanks! I am really confused where is the matplot object? <matplotlib.lines.line2d at="" 0x7e0a050=""> what does it mean? I am now learning to plot using matplotlib http://matplotlib.org/users/pyplot_tutorial.html.......... I could not let the example work in SAGE.