发布网友 发布时间:2022-04-24 04:56
共2个回答
热心网友 时间:2022-04-18 04:55
import matplotlib.pyplot as plt
plt.plot([1,2,4,1], label='line1')
plt.plot([3,1,2,3], label='line2')
plt.legend()
可以通过plot函数的label参数来设置,
然后需要调用一下legend()函数。
热心网友 时间:2022-04-18 06:13
from math import factorial
import numpy as <a class="-highlight " target="_blank" href="https://www.baidu.com/s?wd=np&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YvP1R3rAR3ujwBPAn4P1m30ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7gPYpyq8Q1TYPHc1rjn1rjfdP1RsPWfdn6">np</a>
import matplotlib.pyplot as plt
#阶乘
def fact(n):
return factorial(n)
#Catalan公式
def cat_direct(n):
return fact(2*n) // fact(n + 1) // fact(n)
<a class="-highlight " target="_blank" href="https://www.baidu.com/s?wd=max&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YvP1R3rAR3ujwBPAn4P1m30ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7gPYpyq8Q1TYPHc1rjn1rjfdP1RsPWfdn6">max</a> = 20
nList = range(25)
valList = []
print "Enter the limit for Catalan numbers to be printed: 10000000000"
for i in nList:
if i <= max:
val = cat_direct(i)
valList.append(val)
print "C %s is:"%i, val
else:
print "C %s is:"%i, 10000000000
valList.append(10000000000)
#---生成曲线
plt.plot(nList,valList, 'ro')
plt.axis([0, 25, 0, 10000000000])
plt.xlabel("n")
plt.ylabel("Catalan")
plt.title("Cn+1 = 2*(2n+1)*Cn/(n+2)")
plt.show()