Ask Your Question
0

modifying xmin value and frame of a plot

asked 2015-04-22 15:00:42 +0100

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Would you please let me now how to modify min value of this plot? I want to make x axis begin from 13, but I can't modify the minimum value of the frame. Now I have no idea what to do....

var('I, r, L_s, z, g, f1, f2, f3, f4, c1, c2, c3, g1, g2, g3, Lg1, Lg2, Lg3, Lg4, Lg5, cp1, cp2, cp3')
f1=1/r^2*exp(-r/50);
f2=2.4/r^2*exp(-r/50);
f3=4/r^2*exp(-r/50);
f4=5/r^2*exp(-r/50);
f5=15/r^2*exp(-r/50);


Lg1=plot(f1, (x, 13, 200),  ticks=[[log(13)/log(10), 10^2],[0.000005, 1/5, 10^-5, 10^-4, 10^-3, 10^-2, 10^-1]], scale=('loglog', 10), ymin=0, ymax=0.1, axes_labels=['Distance between led and camera [m]','Illuminace on camera sensor[lux]'],axes=True, legend_label='I=1 cd', frame=True, color='blue', title='L_s(scattering length)=50m');
Lg2=plot(f2, (x, 13, 200), ymin=10^-4, ymax=0.3, axes_labels=['Distance between led and camera [m]','Illuminace on camera sensor[lux]'],axes=False, legend_label='I=2.4 cd', frame=True, color='black');
Lg3=plot(f3, (x, 13, 200), ymin=10^-4, ymax=0.3, axes_labels=['Distance between led and camera [m]','Illuminace on camera sensor[lux]'],axes=False, legend_label='I=4 cd', frame=True, color='red'); 
Lg4=plot(f4, (x, 13, 200), ymin=10^-4, ymax=0.3, axes_labels=['Distance between led and camera [m]','Illuminace on camera sensor[lux]'],axes=False, legend_label='I=5 cd', frame=True, color='green'); 
Lg5=plot(f5, (x, 13, 200), ymin=10^-4, ymax=0.3, axes_labels=['Distance between led and camera [m]','Illuminace on camera sensor[lux]'],axes=False, legend_label='I=15 cd', frame=True, color='violet'); 
cp4=plot(0.000001, (x, 13, 200), ymin=10^-4, ymax=0.3, axes_labels=['Distance between led and camera [m]','Illuminace on camera sensor[lux]'],axes=False, frame=True, color='gray', linestyle='dashed'); 
cp5=plot(0.0005, (x, 13, 200), ymin=10^-4, ymax=0.3, axes_labels=['Distance between led and camera [m]','Illuminace on camera sensor[lux]'],axes=False, frame=True, color='gray', linestyle='dashed'); 
Lg1.set_legend_options(loc=1);
cp6=plot(0.00008, (x, 13, 200), ymin=10^-4, ymax=0.3, axes_labels=['Distance between led and camera [m]','Illuminace on camera sensor[lux]'],axes=False, frame=True, color='gray', linestyle='dashed'); 
cp3=plot(0.2, (x, 13, 200), ymin=0, ymax=0.1,  axes_labels=['Distance between led and camera [m]','Illuminace on camera sensor[lux]'],axes=False, frame=True, color='red', linestyle='dashed'); 

g=line([(20,0), (20, 0.1)], color='black')
z= g + Lg1 + Lg2 + Lg3 + Lg4 + Lg5 + cp3 + cp4 + cp5 + cp6;
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2015-04-24 15:22:23 +0100

slelievre gravatar image

Thanks for your question.

I tried to rephrase the essence of your question more concisely at ask-sage question 26637.

I am taking the opportunity here to make a few comments on the code you provided.

  • You declared a bunch of variables for nothing, you needed only declare r since it's the only one that you are using as a symbolic variable.

  • You can even dispense with declaring variables at all, if you use x in your function definitions (since x is defined when you open Sage).

  • You are creating a lot of plots with mostly the same options, so it would be worth making a dictionary out of those, and using that dictionary each time.

    This way, if you spot a typo (eg Illuminace > Illuminance), or decide to use SI units (lux > lx), and so on, you only have to make the change in one place, and you don't risk forgetting one of the occurrences.

    By the way it looks like this is what happened with your ymin and ymax, which are 1e-4 to 0.3 in some places, and 0 to 0.1 in other places.

  • Apparently the line doesn't work well in log scale, that would be worth a question in itself. A workaround is to use a parametric plot.

  • You don't need semi-colons all over the place.

  • You have some ticks outside the y-range.

Putting all these remarks into use, here is how I would rewrite your code.

f1 =  1.0 / x^2 * exp(-x/50.)
f2 =  2.4 / x^2 * exp(-x/50.)
f3 =  4.0 / x^2 * exp(-x/50.)
f4 =  5.0 / x^2 * exp(-x/50.)
f5 = 15.0 / x^2 * exp(-x/50.)

x_range = (x,13,200)

gen_options = {
    'ymin': 1e-4,
    'ymax': 3e-1,
    'scale': ('loglog', 10),
    'axes': False,
    'frame': True,
    'ticks': ([13, 1e2, 200],
              [1e-4, 1e-3, 1e-2, 1e-1]),
    'axes_labels': ('Distance between led and camera [m]',
                    'Illuminance on camera sensor [lx]'),
    }

title_options = {
    'title': 'L_s (scattering length) = 50 m',
    }

def lplot(f, title=False, **options):
    loc_options = gen_options.copy()
    loc_options.update(options)
    if title: loc_options.update(title_options)
    return plot(f, x_range, **loc_options)

z = Graphics()
z.set_legend_options(loc=1)

Lg1 = lplot(f1, color='blue',   legend_label='I = 1.0 cd', title=True)
Lg2 = lplot(f2, color='black',  legend_label='I = 2.4 cd')
Lg3 = lplot(f3, color='red',    legend_label='I = 4.0 cd')
Lg4 = lplot(f4, color='green',  legend_label='I = 5.0 cd')
Lg5 = lplot(f5, color='violet', legend_label='I = 15. cd')

cp3 = lplot(2e-1, linestyle='dashed', color='red')
cp4 = lplot(1e-6, linestyle='dashed', color='gray')
cp5 = lplot(5e-4, linestyle='dashed', color='gray')
cp6 = lplot(8e-5, linestyle='dashed', color='gray')

# g = line(((20,0), (20, 0.1)), color='black') # seems broken in log scale

# workaround for `line` in log scale: use parametric_plot
# (setting aspect_ratio to auto -- it defaults to 1 in parametric_plot)
g = parametric_plot((20,x), (x,0,0.1), color='black',
                    aspect_ratio='automatic', **gen_options)

z += g + Lg1 + Lg2 + Lg3 + Lg4 + Lg5 + cp3 + cp4 + cp5 + cp6
z
edit flag offensive delete link more

Comments

Thank you for such a nice code and advices. I would learn with this code.

Nownuri1 gravatar imageNownuri1 ( 2015-05-28 21:16:18 +0100 )edit
0

answered 2015-05-28 21:15:04 +0100

Nownuri1 gravatar image

I solved this problem with the followings. axes=None axes_pad=False

edit flag offensive delete link more

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: 2015-04-22 15:00:42 +0100

Seen: 532 times

Last updated: May 28 '15