简体   繁体   中英

Why plt.plot does not show me the graph?

I'm trying to plot the outcomes of the below calculation in a graph, x being the values of N, and y being the calculated errors, but when I do so I'm unable to see the graph, does anyone know why?

Your kind support would be very helpful.

import numpy as np
import random 
import matplotlib.pyplot as plt 

#enter N to equal 100
N = int(input("Define a value for N: "))


Leibniz_Error = np.zeros(N)
Euler_Error = np.zeros(N)


#Leibniz
sum1 = 0
for k in range (N):       
    sum1= sum1 + 1.0/((4*k + 1)*(4*k + 3))
    Leibniz_Error = np.pi - 8*sum1
sum1 = 8*sum1
Final_Leibniz_Error = abs(np.pi - sum1)
print("Euler=", Final_Leibniz_Error)

#Euler
sum2 = 0
for k in range (1,101):
    sum2 = sum2 + 1/(k**2)
    Euler_Error = np.pi - np.sqrt (6*sum2)
sum2 = 6*sum2
sum2 = np.sqrt(sum2)
Final_Euler_Error = abs(np.pi - sum2)
print("Euler=", Final_Euler_Error)

plt.plot (N, Final_Leibniz_Error)
plt.plot (N, Final_Euler_Error, 'r-')
print (N)

This is what I get when I plot.

在此处输入图像描述

Since you're only plotting point you have to use plt.scatter(N, Final_Leibniz_Error) instead since plot will only draw lines between pairs of points

If you want to plot the errors through the iteration you need to save the progressive errors

import numpy as np
import random 
import matplotlib.pyplot as plt 

#enter N to equal 100
N = int(input("Define a value for N: "))


Leibniz_Error = np.zeros(N)
Euler_Error = np.zeros(N)


#Leibniz
sum1 = 0
leibniz_errors = []
for k in range (N):       
    sum1= sum1 + 1.0/((4*k + 1)*(4*k + 3))
    Leibniz_Error = np.pi - 8*sum1
    leibniz_errors.append(Leibniz_Error)
sum1 = 8*sum1
Final_Leibniz_Error = abs(np.pi - sum1)
print("Euler=", Final_Leibniz_Error)

#Euler
sum2 = 0
euler_errors = []
for k in range (1,101):
    sum2 = sum2 + 1/(k**2)
    Euler_Error = np.pi - np.sqrt (6*sum2)
    euler_errors.append(Euler_Error)
sum2 = 6*sum2
sum2 = np.sqrt(sum2)
Final_Euler_Error = abs(np.pi - sum2)
print("Euler=", Final_Euler_Error)

plt.plot([i for i in range(N)], leibniz_errors)
plt.plot([i for i in range(100)], euler_errors, 'r-')
plt.show()
print (N, Final_Leibniz_Error)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM