Jonny
Jonny

Reputation: 1368

Scaling of plot in Matplotlib, Python

I wrote the following program that plots the shape as shown after the code:

length = 100
a = 50
b = 0
n = 2
alpha = np.radians(25.)
d = 18

x_nose = np.linspace(0,a,50)

r_nose = (0.5*d*(1. - ((x_nose-a)/a)**2.)**(1./n))

x_mid = np.linspace(a,(a+b),2)

r_mid = (((d/2) * np.ones(2)))

x_tail = np.linspace((a+b),length,50)

l_tail = (length - a - b)

r_tail = (0.5*d - ((3*d)/(2*l_tail**2) - np.tan(alpha)/l_tail)*(x_tail-a-b)**2 + (d/l_tail**3 - np.tan(alpha)/l_tail**2)*(x_tail-a-b)**3)

line1 = plt.plot(x_nose,r_nose,'k',x_mid,r_mid,'k',x_tail,r_tail,'k')
line2 = plt.plot(x_nose,-r_nose,'k',x_mid,-r_mid,'k',x_tail,-r_tail,'k')
plt.axis('equal')
plt.setp([line1,line2], linewidth=2)  
plt.show()

enter image description here

The formulas used to calculate the shape are however defined for length = 100. How can I scale the entire figure for a variable length?

Upvotes: 0

Views: 472

Answers (2)

gboffi
gboffi

Reputation: 25023

You can apply a scaling factor, or even a linear transform, to each of the vectors that you're plotting

ax = 0.1 ; bx = -5.0 ; fx = lambda x: ax*x+bx
plt.plot(fx(x_head), r_head, fx(x_mid), r_mid, fx(x_tail), r_tail)
...

In the example above, I've just applied the scaling so that the x-axis limits are [-5, 5] and I didn't change the scaling on r, but you can apply the same scaling, or a different one, to the r vectors as you please.

Upvotes: 1

Sleepyhead
Sleepyhead

Reputation: 1021

You can use plt.xlim in the following way:

plt.xlim(-a*length, b*length)

where a and b are some coefficients of your choice (i.e. a=0, b=1)

Upvotes: 0

Related Questions