Is there a way to remove the access rectangle at the end of an open plot when using this methon to claculate the upper and lower sum of a function?

Viewed 26

When using this bit of code to calculate the upper and lower sum of a function (using the Archimedes strip method) and the plot doesn't end on y = 0 but "in the air", an access rectangle at the end appears, what would be a way to remove this and make it look cleaner? Please excuse me if I made some stupid mistake, I am kinda new to this stuff :) (took me a way too long time to realize the artefacts I was experiencing could be fixed with a simple +1

x = np.linspace(a, b, N+1)

Anyways, here is the code:

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return 0.5*x*x

a = 0 #lower bound
b = np.pi #upper bound

N = 30 #number of rectangles

#width of each rectangle
width = (b-a)/N

#x-coordinates of the left edge of each rectangle
x = np.linspace(a, b, N+1)

#y-coordinates of the bottom edge of each rectangle
y = f(x)

#height of each rectangle
height = y

#calculate the area using the formula: area = width * height
area = width * height

#calculate the lower sum
lower_sum = np.sum(area)

#x-coordinates of the midpoint of each rectangle
x_mid = x + width/2

#y-coordinates of the midpoint of each rectangle
y_mid = f(x_mid)

#height of each rectangle
mid_height = y_mid

#calculate the area using the formula: area = width * height
mid_area = width * mid_height

#calculate the upper sum
upper_sum = np.sum(mid_area)

print("The lower sum is:", lower_sum)
print("The upper sum is:", upper_sum)

lower_bar = x - width * 0.5
upper_bar = x + width * 0.5

#plot the graph of the function
plt.plot(x, y, 'b-')
plt.bar(lower_bar, y, width = width, alpha = 0.5, color = 'blue')
plt.bar(upper_bar, y, width = width, alpha = 0.5, color = 'red')
plt.savefig('plot.pdf')

Thank you in advance :D

1 Answers

When you print your x-axis values:

print(lower_bar)
print(upper_bar)

You'll notice that they are shifted:

[-0.05235988  0.05235988  0.15707963  0.26179939  0.36651914  0.4712389
  0.57595865  0.68067841  0.78539816  0.89011792  0.99483767  1.09955743
  1.20427718  1.30899694  1.41371669  1.51843645  1.6231562   1.72787596
  1.83259571  1.93731547  2.04203522  2.14675498  2.25147474  2.35619449
  2.46091425  2.565634    2.67035376  2.77507351  2.87979327  2.98451302
  3.08923278]
[0.05235988 0.15707963 0.26179939 0.36651914 0.4712389  0.57595865
 0.68067841 0.78539816 0.89011792 0.99483767 1.09955743 1.20427718
 1.30899694 1.41371669 1.51843645 1.6231562  1.72787596 1.83259571
 1.93731547 2.04203522 2.14675498 2.25147474 2.35619449 2.46091425
 2.565634   2.67035376 2.77507351 2.87979327 2.98451302 3.08923278
 3.19395253]

'shifted' meaning that the 1st value of lower_bar = -0.05235988 isn't present in upper_bar, and the 2nd value of lower_bar is the 1st value of upper_bar.
And vice versa the last value of upper_bar = 3.19395253 isn't present in lower_bar, and the last value of lower_bar is the penultimate value of upper_bar.

The -0.05235988 lower_bar value is only very suttle recognizable in the plot as its y value is small.
However the 3.19395253 upper_bar value is the noticable rectangle 'at the end' as its y value is large.

Assuming your calculations are as intended a fix for the plot is to slice the np arrays:

plt.bar(lower_bar[1:], y[1:], width = width, alpha = 0.5, color = 'blue')
plt.bar(upper_bar[:-1], y[:-1], width = width, alpha = 0.5, color = 'red')

print(lower_bar[1:]) ... as well to see the effect in the arrays.
Note y needs to be sliced as well to have respectively the same array size of x and y.

enter image description here

Related