Ask Your Question
0

same ymin and ymax accross many seperate plots?

asked 2019-04-15 09:14:28 -0500

stockh0lm gravatar image

I have a sage script that generates 12 plots with functions, and of course so far all the y-axis are scaled separately. In order to make the plots easier to compare i would like to have the same ymin and ymax across all the plots. How can i do that?

The script is not beautiful, and I am not proud of it. actually I would be happy to get suggestions how to structure it better, so here it goes... there are some not-so-easy-to-automate differences about the (very similar in structure) parts which could be put into functions, with very long function calls.

dU,a, x, d_e, epsilon_m, epsilon_s, v_ges, U =var('dU a x d_e epsilon_m epsilon_s v_ges U')
d_k = var('d_k')
d_m = var('d_m')

#========= dielectric constants ==================================
epsilon_0 = var('epsilon_0', latex_name=r'\varepsilon_0')
#sediment
epsilon_s = var('epsilon_s', latex_name=r'\varepsilon_s')
# seawater
epsilon_m = var('epsilon_m', latex_name=r'\varepsilon_m')

epsilon_Ersatz = var('epsilon_Ersatz', latex_name=r'\varepsilon_{Ersatz}')
epsilon_Ersatz = d_k * ( (epsilon_m/d_m) + (epsilon_s/(d_k-d_m) ) )
#===========================================


#============ electric potential field ===============================

v(epsilon,Q,r) = Q/(4*pi*epsilon*r)
p_1 = v(epsilon_0*epsilon_m,       1, abs(a))
p_2 = v(epsilon_0*epsilon_m,      -1, abs(d_e-a))
p_3 = v(epsilon_0*epsilon_Ersatz, -1, sqrt((2*d_k)^2+a^2))
p_4 = v(epsilon_0*epsilon_Ersatz,  1, sqrt((2*d_k)^2+(d_e -a)^2))
p_fix_rhs = p_1 + p_2 + p_3 + p_4

d = var ('d')
d = (U/p_fix_rhs).subs({
    d_e : 1,
    x : -.25,
    d_m : .075,
    epsilon_m : 76,
    epsilon_0 : 8.854187817e-12,
    epsilon_s : RIF(3.5,40).n(),
    a   : 0.01
})

v_1 = v(epsilon_0*epsilon_m,       1, abs(x))
v_2 = v(epsilon_0*epsilon_m,      -1, abs(d_e-x))
v_3 = v(epsilon_0*epsilon_Ersatz, -1, sqrt((2*d_k)^2+x^2))
v_4 = v(epsilon_0*epsilon_Ersatz,  1, sqrt((2*d_k)^2+(d_e -x)^2))
v_ges_rhs = dU + d*( v_1 + v_2 + v_3 + v_4)
show(v_ges_rhs)
#==================================================

#==================================================
# one boundary condition is that the electric potential is U at a given point x = r_0. (radius of the electrode)
# I need to scale the electric charge Q accordingly.


print "d_m without error"
tiefe = v_ges_rhs.subs({
    dU  : RIF(-0.01,0.01),
    U   : RIF(9.99,10.01),
    d_e : RIF(.999,1.001),
    x : -.25,
    d_m : 0.075,
    epsilon_m : RIF(75,77),
    epsilon_s : RIF(3.5,40),
    epsilon_0 : 8.854187817e-12
})

f = lambda x: tiefe.subs({d_k: x})
f_lower = lambda x: RIF(tiefe.subs({d_k : x})).lower()
f_upper = lambda x: RIF(tiefe.subs({d_k : x})).upper()
plot([f, f_lower, f_upper],(x,0.2,0.99), color=['blue', 'red', 'green'], legend_label=['without error',  'lower margin of error', 'upper margin of error'], axes_labels=['cable\'s burial depth in [m]','Potential in [V]'], figsize=8, axes_labels_size=1, title='Distance to seabed without error', title_pos=(0.5,1.1)).save('d_m-noerror.png')

print "epsilon_m without error"
tiefe = v_ges_rhs.subs({
    dU  : RIF(-0.01,0.01),
    U   : RIF(9.99,10.01),
    d_e : RIF(.999,1.001),
    x : -.25,
    d_m : RIF(.05,.1),
    epsilon_m : 76,
    epsilon_s : RIF(3.5,40),
    epsilon_0 : 8.854187817e-12
})
f = lambda x: tiefe.subs({d_k: x})
f_lower = lambda x: RIF(tiefe.subs({d_k : x})).lower()
f_upper = lambda x: RIF(tiefe.subs({d_k : x})).upper()
plot([f, f_lower, f_upper],(x,0.2,0.99), color=['blue', 'red', 'green'], legend_label=['without error',  'lower margin of error', 'upper margin of error'], axes_labels=['cable\'s burial depth in [m]','Potential in [V]'], figsize=8, axes_labels_size=1, title='$\epsilon_m$ without error', title_pos=(0.5,1.1)).save('epsilon_m-noerror.png')

print "epsilon_s without error"
tiefe = v_ges_rhs.subs({
    dU  : RIF(-0.01,0.01),
    U   : RIF(9.99,10.01),
    d_e : RIF(.999,1.001),
    x : -.25,
    d_m : RIF(.05,.1),
    epsilon_m : RIF(75,77),
    epsilon_s : 21.75,
    epsilon_0 : 8.854187817e-12
})
f = lambda x: tiefe.subs({d_k: x})
f_lower = lambda x: RIF(tiefe.subs({d_k : x})).lower()
f_upper = lambda x: RIF(tiefe.subs({d_k : x})).upper()
plot([f, f_lower, f_upper],(x,0.2,0.99), color=['blue', 'red', 'green'], legend_label=['without error',  'lower margin of error', 'upper margin of error'], axes_labels=['cable\'s burial depth in [m]','Potential in [V]'], figsize=8, axes_labels_size=1, title='$\epsilon_s$ without error', title_pos=(0.5,1.1)).save('epsilon_s-noerror.png')

print "d_e without error"
tiefe = v_ges_rhs.subs({
    dU  : RIF(-0.01,0.01),
    U   : RIF(9.99,10.01),
    d_e : 1,
    x : -.25,
    d_m : RIF(.05,.1),
    epsilon_m : RIF(75,77),
    epsilon_s : RIF(3.5,40),
    epsilon_0 : 8.854187817e-12
})
f = lambda x: tiefe.subs({d_k: x})
f_lower = lambda x: RIF(tiefe.subs({d_k : x})).lower()
f_upper = lambda x: RIF(tiefe.subs({d_k : x})).upper()
plot([f, f_lower, f_upper],(x,0.2,0.99), color=['blue', 'red', 'green'], legend_label=['without error',  'lower margin of error', 'upper margin of error'], axes_labels=['cable\'s burial depth in [m]','Potential in [V]'], figsize=8, axes_labels_size=1, title='Distance of elektrodes witout error', title_pos=(0.5,1.1)).save('d_e-noerror.png')

print "only error is d_m"
tiefe = v_ges_rhs.subs({
    dU  : 0,
    U   : 10,
    d_e : 1,
    x : -.25,
    d_m : RIF(.05,.1),
    epsilon_m : 76,
    epsilon_s : 21.75,
    epsilon_0 : 8.854187817e-12
})
f = lambda x: tiefe.subs({d_k: x})
f_lower = lambda x: RIF(tiefe.subs({d_k : x})).lower()
f_upper = lambda x: RIF(tiefe.subs({d_k : x})).upper()
plot([f, f_lower, f_upper],(x,0.2,0.99), color=['blue', 'red', 'green'], legend_label=['without error',  'lower margin of error', 'upper margin of error'], axes_labels=['cable\'s burial depth in [m]','Potential in [V]'], figsize=8, axes_labels_size=1, title='Only Error is distance to seabed', title_pos=(0.5,1.1)).save('d_m-error.png')

print "only error is d_e"
tiefe = v_ges_rhs.subs({
    dU  : 0,
    U   : 10,
    d_e : RIF(.999,1.001),
    x : -.25,
    d_m : .075,
    epsilon_m : 76,
    epsilon_s : 21.75,
    epsilon_0 : 8.854187817e-12
})
f = lambda x: tiefe.subs({d_k: x})
f_lower = lambda x: RIF(tiefe.subs({d_k : x})).lower()
f_upper = lambda x: RIF(tiefe.subs({d_k : x})).upper()
plot([f, f_lower, f_upper],(x,0.2,0.99), color=['blue', 'red', 'green'], legend_label=['without error',  'lower margin of error', 'upper margin of error'], axes_labels=['cable\'s burial depth in [m]','Potential in [V]'], figsize=8, axes_labels_size=1, title='Distance of electrodes with error', title_pos=(0.5,1.1)).save('d_e-error.png')

print "only error is epsilon_m"
tiefe = v_ges_rhs.subs({
    dU  : 0,
    U   : 10,
    d_e : 1,
    x : -.25,
    d_m : .075,
    epsilon_m : RIF(75,77),
    epsilon_s : 21.75,
    epsilon_0 : 8.854187817e-12
})
f = lambda x: tiefe.subs({d_k: x})
f_lower = lambda x: RIF(tiefe.subs({d_k : x})).lower()
f_upper = lambda x: RIF(tiefe.subs({d_k : x})).upper()
plot([f, f_lower, f_upper],(x,0.2,0.99), color=['blue', 'red', 'green'], legend_label=['without error',  'lower margin of error', 'upper margin of error'], axes_labels=['cable\'s burial depth in [m]','Potential in [V]'], figsize=8, axes_labels_size=1, title='$\epsilon_m$ with error', title_pos=(0.5,1.1)).save('epsilon_m-error.png')

print "only error is epsilon_s"
tiefe = v_ges_rhs.subs({
    dU  : 0,
    U   : 10,
    d_e : 1,
    x : -.25,
    d_m : .075,
    epsilon_m : 76,
    epsilon_s : RIF(3.5,40),
    epsilon_0 : 8.854187817e-12
})
f = lambda x: tiefe.subs({d_k: x})
f_lower = lambda x: RIF(tiefe.subs({d_k : x})).lower()
f_upper = lambda x: RIF(tiefe.subs({d_k : x})).upper()
plot([f, f_lower, f_upper],(x,0.2,0.99), color=['blue', 'red', 'green'], legend_label=['without error',  'lower margin of error', 'upper margin of error'], axes_labels=['cable\'s burial depth in [m]','Potential in [V]'], figsize=8, axes_labels_size=1, title='$\epsilon_s$ with error', title_pos=(0.5,1.1)).save('epsilon_s-error.png')

print "only error is dU"
tiefe = v_ges_rhs.subs({
    dU  : RIF(-0.01,0.01),
    U   : 10,
    d_e : 1,
    x : -.25,
    d_m : .075,
    epsilon_m : 76,
    epsilon_s : RIF(3.5,40),
    epsilon_0 : 8.854187817e-12
})
f = lambda x: tiefe.subs({d_k: x})
f_lower = lambda x: RIF(tiefe.subs({d_k : x})).lower()
f_upper = lambda x: RIF(tiefe.subs({d_k : x})).upper()
plot([f, f_lower, f_upper],(x,0.2,0.99), color=['blue', 'red', 'green'], legend_label=['without error',  'lower margin of error', 'upper margin of error'], axes_labels=['cable\'s burial depth in [m]','Potential in [V]'], figsize=8, axes_labels_size=1, title='$\Delta U$ with error', title_pos=(0.5,1.1)).save('dU-error.png')

print "only error is U"
tiefe = v_ges_rhs.subs({
    dU  : 0,
    U   : RIF(9.99,10.01),
    d_e : 1,
    x : -.25,
    d_m : .075,
    epsilon_m : 76,
    epsilon_s : RIF(3.5,40),
    epsilon_0 : 8.854187817e-12
})
f = lambda x: tiefe.subs({d_k: x})
f_lower = lambda x: RIF(tiefe.subs({d_k : x})).lower()
f_upper = lambda x: RIF(tiefe.subs({d_k : x})).upper()
plot([f, f_lower, f_upper],(x,0.2,0.99), color=['blue', 'red', 'green'], legend_label=['without error',  'lower margin of error', 'upper margin of error'], axes_labels=['cable\'s burial depth in [m]','Potential in [V]'], figsize=8, axes_labels_size=1, title='$U$ with error', title_pos=(0.5,1.1)).save('U-error.png')

print "all values have errors"
tiefe = v_ges_rhs.subs({
    dU  : RIF(-0.01,0.01),
    U   : RIF(9.99,10.01),
    d_e : RIF(.999,1.001),
    x : -.25,
    d_m : RIF(.05,.1),
    epsilon_m : RIF(75,77),
    epsilon_s : RIF(3.5,40),
    epsilon_0 : 8.854187817e-12
})
f = lambda x: tiefe.subs({d_k: x})
f_lower = lambda x: RIF(tiefe.subs({d_k : x})).lower()
f_upper = lambda x: RIF(tiefe.subs({d_k : x})).upper()
plot([f, f_lower, f_upper],(x,0.2,0.99), color=['blue', 'red', 'green'], legend_label=['without error',  'lower margin of error', 'upper margin of error'], axes_labels=['cable\'s burial depth in [m]','Potential in [V]'], figsize=8, axes_labels_size=1, title='All values have errors', title_pos=(0.5,1.1)).save('all-error.png')

print "dU is without error"
tiefe = v_ges_rhs.subs({
    dU  : 0,
    U   : RIF(9.99,10.01),
    d_e : RIF(.999,1.001),
    x : -.25,
    d_m : RIF(.05,.1),
    epsilon_m : RIF(75,77),
    epsilon_s : RIF(3.5,40),
    epsilon_0 : 8.854187817e-12
})
f = lambda x: tiefe.subs({d_k: x})
f_lower = lambda x: RIF(tiefe.subs({d_k : x})).lower()
f_upper = lambda x: RIF(tiefe.subs({d_k : x})).upper()
plot([f, f_lower, f_upper],(x,0.2,0.99), color=['blue', 'red', 'green'], legend_label=['without error',  'lower margin of error', 'upper margin of error'], axes_labels=['cable\'s burial depth in [m]','Potential in [V]'], figsize=8, axes_labels_size=1, title='$\Delta U$ without error', title_pos=(0.5,1.1)).save('dU-noerror.png')

print "U is without error"
tiefe = v_ges_rhs.subs({
    dU  : RIF(-0.01,0.01),
    U   : 10,
    d_e : RIF(.999,1.001),
    x : -.25,
    d_m : RIF(.05,.1),
    epsilon_m : RIF(75,77),
    epsilon_s : RIF(3.5,40),
    epsilon_0 : 8.854187817e-12
})
f = lambda x: tiefe.subs({d_k: x})
f_lower = lambda x: RIF(tiefe.subs({d_k : x})).lower()
f_upper = lambda x: RIF(tiefe.subs({d_k : x})).upper()
plot([f, f_lower, f_upper],(x,0.2,0.99), color=['blue', 'red', 'green'], legend_label=['without error',  'lower margin of error', 'upper margin of error'], axes_labels=['cable\'s burial depth in [m]','Potential in [V]'], figsize=8, axes_labels_size=1, title='$U$ is without error', title_pos=(0.5,1.1)).save('U-noerror.png')

print "all values have errors. but this is without seawater, just sediment"
tiefe = v_ges_rhs.subs({
    dU  : RIF(-0.01,0.01),
    U   : RIF(9.99,10.01),
    d_e : RIF(.999,1.001),
    x : -.25,
    d_m : 0.0001,
    epsilon_m : 75,
    epsilon_s : RIF(3.5,40),
    epsilon_0 : 8.854187817e-12
})
f = lambda x: tiefe.subs({d_k: x})
f_lower = lambda x: RIF(tiefe.subs({d_k : x})).lower()
f_upper = lambda x: RIF(tiefe.subs({d_k : x})).upper()
plot([f, f_lower, f_upper],(x,0.2,0.99), color=['blue', 'red', 'green'], legend_label=['without error',  'lower margin of error', 'upper margin of error'], axes_labels=['cable\'s burial depth in [m]','Potential in [V]'], figsize=8, axes_labels_size=1, title='all values have errors. but this is without seawater, just sediment', title_pos=(0.5,1.1)).save('JustSediment.png')
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-04-15 10:40:48 -0500

Juanjo gravatar image

You could put your plots in a list or matrix, iterate to get their dimensions, compute the max and min values for the y axis in all the plots and draw together the plots using graphics_array. As a proof of concept, consider the following example:

p = []
for i in range(3):
    plt = plot([random()*sin(x),random()*cos(x)],(x,-pi,pi), color=["red","green"])
    p.append(plt)

ym = 1e+20
yM = 1e-20
for i in range(len(p)):
    bounds = p[i].get_minmax_data()
    ym = min(ym, bounds["ymin"])
    yM = max(yM, bounds["ymax"])

graphics_array(p).show(frame=True) # each plot uses its own vertical scale
graphics_array(p).show(ymin=ym,ymax=yM, frame=True) # same scale for all plots
edit flag offensive delete link more

Comments

i used that, and of course the many plots didnt display correctly. and since i wanted them pairwise side by side i went for this:

ym = 1e+20 
yM = 1e-20 
for i in range(len(l)):
    bounds = l[i].get_minmax_data()
    ym = min(ym, bounds["ymin"])
    yM = max(yM, bounds["ymax"])

gpairs = list(zip(l[::2], l[1::2]))
     graphics_array(gpairs).show(frame=True)
\# each plot uses its own vertical scale
graphics_array(gpairs).show(ymin=ym,ymax=yM, frame=True) # same scale for all
plots

meaning, it reorderd the list. but the graphs are still a complete mess. it says:

/usr/lib/python2.7/dist-packages/matplotlib/tight_layout.py:209: UserWarning: tight_layout cannot make axes height small enough to accommodate all axes decorations
stockh0lm gravatar imagestockh0lm ( 2019-04-15 14:11:04 -0500 )edit

i would actually like to use a long, narrow frame, to include in my latex document. how can i get that?

stockh0lm gravatar imagestockh0lm ( 2019-04-15 14:13:28 -0500 )edit

it seems to work if i give a big figsize=(8,24).

stockh0lm gravatar imagestockh0lm ( 2019-04-15 15:08:57 -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

1 follower

Stats

Asked: 2019-04-15 09:14:28 -0500

Seen: 26 times

Last updated: Apr 15