Ask Your Question
0

plot grids in a figure

asked 2013-11-26 03:40:36 -0500

gundamlh gravatar image

updated 2017-01-08 05:18:10 -0500

FrédéricC gravatar image

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.

Thanks in advance!

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
0

answered 2013-11-26 03:56:38 -0500

tmonteil gravatar image

updated 2013-11-26 04:30:11 -0500

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)
edit flag offensive delete link more

Comments

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

gundamlh gravatar imagegundamlh ( 2013-11-26 04:15:27 -0500 )edit

`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.

kcrisman gravatar imagekcrisman ( 2013-11-26 16:35:52 -0500 )edit

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.

gundamlh gravatar imagegundamlh ( 2013-11-28 07:37:44 -0500 )edit
0

answered 2013-11-26 04:28:19 -0500

ppurka gravatar image

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

edit flag offensive delete link more

Comments

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.

gundamlh gravatar imagegundamlh ( 2013-11-28 07:37:31 -0500 )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: 2013-11-26 03:40:36 -0500

Seen: 698 times

Last updated: Nov 26 '13