Ask Your Question
0

plot grids in a figure

asked 2013-11-26 10:40:36 +0200

gundamlh gravatar image

updated 2017-01-08 12:18:10 +0200

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 10:56:38 +0200

tmonteil gravatar image

updated 2013-11-26 11:30:11 +0200

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 11:15:27 +0200 )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 23:35:52 +0200 )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 14:37:44 +0200 )edit

Oh. My. God. Really? Draw grid by hand?

Rulatir gravatar imageRulatir ( 2022-11-07 16:41:38 +0200 )edit

Again, you can also use the gridlines keyword - see ppurka's answer, for instance.

kcrisman gravatar imagekcrisman ( 2022-12-02 01:08:09 +0200 )edit
0

answered 2013-11-26 11:28:19 +0200

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 14:37:31 +0200 )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 10:40:36 +0200

Seen: 5,081 times

Last updated: Nov 26 '13