Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How do I adjust the position of my figure's title and remove the extra y labels?

I tried to solve this problem myself (http://ask.sagemath.org/question/26807/how-do-i-adjust-the-size-of-axes-labels-and-figure-titles-in-plots/) using this code (which itself is largely copied from http://ask.sagemath.org/answers/15011/revisions/):

from sage.calculus.desolvers import desolve_odeint
y,dy = var('y,dy');
g    = 9.8; 
l    = 1; 
f    = [dy,-g/l*cos(y)];
v    = [y,dy];
t    = srange(0,5,0.01);
ci   = [0,0];
sol  = desolve_odeint(f,ci,t,v,rtol=1e-15, atol=1e-10,h0=1e-4,hmax=1e-2,hmin=1e-6,mxstep=10000)
p    = line(zip(t,sol[:,0]));
p2   = line(zip(t,sol[:,1]));

import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid.axislines import SubplotZero

def mplot(sage_plot, equal_scale = False, edit_res = False):
    """
    This function convert sage_plot, created at sage, in matplotlib graph.
    """
    plt.clf()
    fig = plt.figure()
    plt.xlabel(r"$t$")
    plt.ylabel(r"$\theta$")
    plt.title(r"$\frac{d^{2}\theta}{dt^2} = -\frac{g}{l} \cos{\theta}$")
    ax = SubplotZero(fig, 111)
    fig.add_subplot(ax)
    L = sage_plot.matplotlib().gca().lines
    for t in L:
        data = t.get_data()
        ax.add_line(mpl.lines.Line2D(data[0], data[1]))
        ax.autoscale_view()
    if equal_scale:
        ax.axis('equal')
    for direction in ["xzero", "yzero"]:
        ax.axis[direction].set_axisline_style("-|>", size = 2)
        ax.axis[direction].set_visible(True)
    for direction in ["left", "right", "bottom", "top"]:
        ax.axis[direction].set_visible(False)
    ax.axis["yzero"].set_axis_direction("right")
    ax.minorticks_on()    
    ax.grid()
    if edit_res:
        return(fig)
    else:
        plt.savefig('')
        plt.close()

mplot(p,equal_scale = True)

but it gives this output: image description.

My problem with this plot is that the title (i.e., the differential equation) is partially cut off and for some reason there's two alternate numbering schemes along the y axis (one from 0 to 1, the other being the real y coordinates which go from -pi to 0)

How do I adjust the position of my figure's title and remove the extra y labels?set of coordinates shown?

I tried to solve this problem myself (http://ask.sagemath.org/question/26807/how-do-i-adjust-the-size-of-axes-labels-and-figure-titles-in-plots/) using this code (which itself is largely copied from http://ask.sagemath.org/answers/15011/revisions/):

from sage.calculus.desolvers import desolve_odeint
y,dy = var('y,dy');
g    = 9.8; 
l    = 1; 
f    = [dy,-g/l*cos(y)];
v    = [y,dy];
t    = srange(0,5,0.01);
ci   = [0,0];
sol  = desolve_odeint(f,ci,t,v,rtol=1e-15, atol=1e-10,h0=1e-4,hmax=1e-2,hmin=1e-6,mxstep=10000)
p    = line(zip(t,sol[:,0]));
p2   = line(zip(t,sol[:,1]));

import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid.axislines import SubplotZero

def mplot(sage_plot, equal_scale = False, edit_res = False):
    """
    This function convert sage_plot, created at sage, in matplotlib graph.
    """
    plt.clf()
    fig = plt.figure()
    plt.xlabel(r"$t$")
    plt.ylabel(r"$\theta$")
    plt.title(r"$\frac{d^{2}\theta}{dt^2} = -\frac{g}{l} \cos{\theta}$")
    ax = SubplotZero(fig, 111)
    fig.add_subplot(ax)
    L = sage_plot.matplotlib().gca().lines
    for t in L:
        data = t.get_data()
        ax.add_line(mpl.lines.Line2D(data[0], data[1]))
        ax.autoscale_view()
    if equal_scale:
        ax.axis('equal')
    for direction in ["xzero", "yzero"]:
        ax.axis[direction].set_axisline_style("-|>", size = 2)
        ax.axis[direction].set_visible(True)
    for direction in ["left", "right", "bottom", "top"]:
        ax.axis[direction].set_visible(False)
    ax.axis["yzero"].set_axis_direction("right")
    ax.minorticks_on()    
    ax.grid()
    if edit_res:
        return(fig)
    else:
        plt.savefig('')
        plt.close()

mplot(p,equal_scale = True)

but it gives this output: image description.

My problem with this plot is that the title (i.e., the differential equation) is partially cut off and for some reason there's two alternate numbering schemes along the y x and axis (one (two set from 0 to 1, the other others being the real x & y coordinates which go from -pi to 0)