简体   繁体   English

为什么 plt.plot 不显示图表?

[英]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?我正在尝试 plot 图表中以下计算的结果,x 是 N 的值,y 是计算出的误差,但是当我这样做时,我看不到图表,有人知道为什么吗?

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.这就是我在 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由于您只是在绘制点,因此您必须使用plt.scatter(N, Final_Leibniz_Error)代替,因为 plot 只会在点对之间绘制线

If you want to plot the errors through the iteration you need to save the progressive errors如果你想 plot 错误通过迭代你需要保存渐进式错误

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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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