uncalculated rhythms

tags

  • theoretical theology (79)
  • tips (2)
  • apocalypse (1)
  • book review (1)
  • end times (1)
  • linux (1)
  • plotting (1)
  • python (1)
  • quantum mechanics (1)
  • sage (1)

quick links (chrono-blog)

  • oldest theory
  • start of the Job arc
  • start of the HORN saga
  • begin Mark
  • begin Indignant

archive

  • ►  2020 (1)
    • ►  January (1)
  • ►  2018 (7)
    • ►  December (1)
    • ►  August (1)
    • ►  June (2)
    • ►  January (3)
  • ►  2017 (10)
    • ►  December (2)
    • ►  July (1)
    • ►  May (2)
    • ►  February (2)
    • ►  January (3)
  • ►  2016 (12)
    • ►  December (1)
    • ►  November (1)
    • ►  October (2)
    • ►  September (2)
    • ►  July (1)
    • ►  June (1)
    • ►  May (3)
    • ►  April (1)
  • ►  2015 (30)
    • ►  December (1)
    • ►  November (1)
    • ►  October (1)
    • ►  September (3)
    • ►  August (1)
    • ►  July (2)
    • ►  June (4)
    • ►  May (1)
    • ►  March (4)
    • ►  February (7)
    • ►  January (5)
  • ►  2014 (22)
    • ►  December (7)
    • ►  November (7)
    • ►  October (4)
    • ►  August (3)
    • ►  April (1)
  • ▼  2013 (1)
    • ▼  August (1)
      • How to make a nice plot in Sage

Friday, August 23, 2013

How to make a nice plot in Sage

Started trying out sage (sagemath.org), a mostly python-based open-source alternative to Mathematica.  After launching into the "notebook()" interface, I was quite pleased to see how nicely everything worked (check out their first-steps tutorial).

However, I found it somewhat frustrating to make a nice-looking plot, which is one of those Mathematica features they should avoid replicating.  Currently it seems impossible to independently change the fontsize for the axis labels and for the tick labels, and this just isn't right.  After some hacking away, I am somewhat satisfied with the result:


And the code:

## preliminaries...
lxloc=0.5 #legend x location, absolute reference
lyloc=0.3 #legend y location (in terms of the plot)

titlelabel='This is a graph'
ylabel='$y$' #y-axis label
xlabel='$x$' #x-axis label

pxmin=0 #xmin for the plot
pxmax=2 #xmax for the plot

g(x)=log(x)
tg(x)=taylor(g(x),x,1,6) # 6th order taylor expansion about x=1

## plot those two functions with different styles
p=plot(g,(x,pxmin,pxmax),color='black',legend_label='$\ln(x)$')
p+=plot(tg,(x,pxmin,pxmax),color='red',linestyle='--', thickness=2, legend_label='6 ord taylor')

## here is where we add our own custom labels, to our own desired font-size.
p+= text(ylabel,(-0.06,0.5),axis_coords=True,fontsize=24,rotation=90,rgbcolor=(0,0,0))
p+= text(xlabel,(0.5,-0.08),axis_coords=True,fontsize=24,rgbcolor=(0,0,0))
p+= text(titlelabel,(0.5,1.06),axis_coords=True,fontsize=30,rgbcolor=(0,0,0))

## after adding the text labels something goes strange when "showing" the plot,
## so need to grab this to use later
pymin=p.ymin()
pymax=p.ymax()

## add in x, y axes if desired
p+=line([[pxmin,0],[pxmax,0]],color='black',linestyle=':')
p+=line([[0,pymin],[0,pymax]],color='black',linestyle=':')

## make the legend pretty
p.set_legend_options(font_size=20,back_color='white',handlelength=2,markerscale=1.3,borderpad=0.3,handletextpad=0.5,font_family='serif',loc=(lxloc,lyloc))
## you could also set loc='best' if you want matplotlib to figure it out

## here's where we put the pxmin and pymin back in to fix label strangeness
p.show(xmin=pxmin, xmax=pxmax, ymin=pymin, ymax=pymax, fontsize=14, frame=true, axes=false)
## the fontsize directly above controls the tick label size.

p.save('graph.png', xmin=pxmin, xmax=pxmax, ymin=pymin, ymax=pymax, fontsize=14, frame=true, axes=false)


Afterwards, I had to find the file via
find ~/.sage -name "graph.png"
on the command line.


You may prefer to use .eps or .pdf.
Posted by ible at 11:55 AM No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels: plotting, python, sage, tips
Newer Posts Home
Subscribe to: Posts (Atom)

About Me

ible
View my complete profile
Copyright (2014-2015) Lucas Wagner. Some rights reserved. Powered by Blogger.